const DEFAULT_CURRENCY_CODE = 'ZAR';

async function resolveCurrencyId(db, providedCurrencyId, transaction) {
  if (providedCurrencyId) {
    return providedCurrencyId;
  }

  const [currency] = await db.currencies.findOrCreate({
    where: { code: DEFAULT_CURRENCY_CODE },
    defaults: {
      code: DEFAULT_CURRENCY_CODE,
      name: 'South African Rand',
      symbol: 'R',
      minor_unit: 2,
      importHash: 'qweli-default-zar',
    },
    transaction,
  });

  return currency ? currency.id : null;
}

module.exports = {
  DEFAULT_CURRENCY_CODE,
  resolveCurrencyId,
};
