UNPKG

@accounter/server

Version:

75 lines 3.17 kB
import { ChargesProvider } from '../../charges/providers/charges.provider.js'; import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js'; import { FinancialEntitiesProvider } from '../../financial-entities/providers/financial-entities.provider.js'; import { formatCurrency, formatFinancialAmount } from '../../../shared/helpers/index.js'; const missingInfoSuggestions = async (RawDocument, _, { injector }) => { const response = {}; if (RawDocument.charge_id) { const charge = await injector .get(ChargesProvider) .getChargeByIdLoader.load(RawDocument.charge_id); if (charge?.business_id) { response.counterpartyId = charge.business_id; } if (charge?.owner_id) { response.ownerId = charge.owner_id; } if (charge?.transactions_event_amount) { const amount = Number(charge.transactions_event_amount); if (!Number.isNaN(amount)) { response.isIncome = Number(charge.transactions_event_amount) > 0; } } if (charge?.transactions_event_amount && charge?.transactions_currency) { // use transactions info, if exists response.amount = { amount: charge.transactions_event_amount, currency: formatCurrency(charge.transactions_currency), }; } else if (charge?.documents_event_amount && charge?.documents_currency) { // Use parallel documents (if exists) as documents_event_amount is based on invoices OR receipts response.amount = { amount: String(charge.documents_event_amount), currency: formatCurrency(charge.documents_currency), }; } } return response; }; export const documentSuggestionsResolvers = { Invoice: { missingInfoSuggestions: missingInfoSuggestions, }, Receipt: { missingInfoSuggestions: missingInfoSuggestions, }, InvoiceReceipt: { missingInfoSuggestions: missingInfoSuggestions, }, CreditInvoice: { missingInfoSuggestions: missingInfoSuggestions, }, Proforma: { missingInfoSuggestions: missingInfoSuggestions, }, DocumentSuggestions: { counterparty: (suggestion, _, { injector }) => suggestion.counterpartyId ? injector .get(FinancialEntitiesProvider) .getFinancialEntityByIdLoader.load(suggestion.counterpartyId) .then(res => res ?? null) : null, owner: (suggestion, _, { injector }) => suggestion.ownerId ? injector .get(BusinessesProvider) .getBusinessByIdLoader.load(suggestion.ownerId) .then(res => res ?? null) : null, amount: suggestion => suggestion.amount ? formatFinancialAmount(suggestion.amount.amount, suggestion.amount.currency) : null, isIncome: suggestion => (suggestion.isIncome == null ? null : suggestion.isIncome), }, }; //# sourceMappingURL=document-suggestions.resolver.js.map