@accounter/server
Version:
Accounter GraphQL server
61 lines • 2.81 kB
JavaScript
import { GraphQLError } from 'graphql';
import { getCurrencySymbol } from '../../../shared/helpers/index.js';
export function validateExchangeRate(businessId, ledgerRecords, amount, defaultLocalCurrency) {
try {
const exchangeAmount = calculateExchangeRate(businessId, ledgerRecords, defaultLocalCurrency);
if ((!exchangeAmount && !!amount) ||
(exchangeAmount && Math.abs(exchangeAmount - amount) > 0.005)) {
return 'Exchange rate error';
}
return true;
}
catch (e) {
return e.message;
}
}
function updateAmounts(amounts, currency, defaultLocalCurrency, amount, localAmount) {
if (localAmount) {
if (!(defaultLocalCurrency in amounts)) {
amounts[defaultLocalCurrency] = 0;
}
amounts[defaultLocalCurrency] += localAmount;
}
if (amount) {
if (!(currency in amounts)) {
amounts[currency] = 0;
}
amounts[currency] += amount;
}
}
export function calculateExchangeRate(businessId, ledgerRecords, defaultLocalCurrency) {
const amounts = {};
for (const record of ledgerRecords) {
if (record.creditAccountID1 === businessId) {
updateAmounts(amounts, record.currency, defaultLocalCurrency, record.creditAmount1, record.localCurrencyCreditAmount1);
}
if (record.creditAccountID2 === businessId) {
updateAmounts(amounts, record.currency, defaultLocalCurrency, record.creditAmount2, record.localCurrencyCreditAmount2);
}
if (record.debitAccountID1 === businessId) {
updateAmounts(amounts, record.currency, defaultLocalCurrency, -(record.debitAmount1 ?? 0), -record.localCurrencyDebitAmount1);
}
if (record.debitAccountID2 === businessId) {
updateAmounts(amounts, record.currency, defaultLocalCurrency, -(record.debitAmount2 ?? 0), -(record.localCurrencyDebitAmount2 ?? 0));
}
}
const currencies = Object.keys(amounts);
if (!(defaultLocalCurrency in amounts)) {
throw new GraphQLError('Local currency amount is required');
}
const foreignCurrencies = currencies.filter(currency => currency !== defaultLocalCurrency);
if (foreignCurrencies.length === 0 && amounts[defaultLocalCurrency] !== 0) {
throw new GraphQLError('Exchange should be 0 as there are no foreign currencies involved');
}
for (const foreignCurrency of foreignCurrencies) {
if (Math.abs(amounts[foreignCurrency] ?? 0) > 0.005) {
throw new GraphQLError(`Exchange rate error - ${foreignCurrency} amount not balanced By ${amounts[foreignCurrency].toFixed(2)}${getCurrencySymbol(foreignCurrency)}`);
}
}
return amounts[defaultLocalCurrency];
}
//# sourceMappingURL=exchange-ledger.helper.js.map