From 5cba4b888ce86350b306bd65e7929e9a4f146b01 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 24 Apr 2021 21:26:33 +0300 Subject: feat: add currency to account --- src/models/account/account.schema.ts | 5 +++++ src/services/uploads.service.ts | 41 ++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/models/account/account.schema.ts b/src/models/account/account.schema.ts index 3ce3983..732ccc7 100644 --- a/src/models/account/account.schema.ts +++ b/src/models/account/account.schema.ts @@ -4,6 +4,7 @@ export interface AccountSchema extends Document { name: string; code: string; balance: number; + currency: 'BYN' | 'USD' | 'EUR'; } export const accountSchema = new Schema({ @@ -19,5 +20,9 @@ export const accountSchema = new Schema({ type: Number, default: 0, }, + currency: { + type: String, + default: 'BYN' + }, }, { timestamps: true }); diff --git a/src/services/uploads.service.ts b/src/services/uploads.service.ts index 3235f4c..90737e5 100644 --- a/src/services/uploads.service.ts +++ b/src/services/uploads.service.ts @@ -25,6 +25,25 @@ const parsePdfItems = async (fileName: string): Promise => { }); }; +const parseAccountId = (rows: any[], context: HookContext) => { + const name = rows.find(row => row[0]?.startsWith('Наименование'))?.[0].slice(13); + const row = rows.find(row => row[0]?.startsWith('Счет клиента')); + const match = new RegExp(/Счет клиента (\w+) (\w+)/g).exec(row?.[0]); + const code = match?.[1]; + const currency = match?.[2]; + + return context.app + .service('accounts') + .find({ code }) + .then((results: any[]) => { + if (results.length) return results[0]; + return context.app + .service('accounts') + .create({ code, name, currency }); + }) + .then((account: any) => account._id); +}; + const parseTransfersBill = async (context: HookContext): Promise => { const { id } = context.result; @@ -34,9 +53,10 @@ const parseTransfersBill = async (context: HookContext): Promise => const hash = _.groupBy(items, 'y') const rows = _.map(hash, (elements: any[]) => _.map(elements, 'text')); - const accountName = rows.find(row => row[0]?.startsWith('Наименование'))?.[0].slice(13); - const accountCodeRow = rows.find(row => row[0]?.startsWith('Счет клиента')); - const accountCode = new RegExp(/Счет клиента (\w+)/g).exec(accountCodeRow?.[0])?.[1]; + // At this point we can remove the file + context.service.remove(id); + + const accountId = await parseAccountId(rows, context); const transfers = rows.reduce((acc: any, cols: string[]) => { if (cols[0]?.startsWith('УНП')) { @@ -55,21 +75,6 @@ const parseTransfersBill = async (context: HookContext): Promise => return acc; }, []); - - // At this point we can remove the file - context.service.remove(id); - - const accountId = await context.app - .service('accounts') - .find({ code: accountCode }) - .then((results: any[]) => { - if (results.length) return results[0]; - return context.app - .service('accounts') - .create({ code: accountCode, name: accountName }); - }) - .then((account: any) => account._id); - context.result = await Bluebird.mapSeries(transfers, async (transfer: any) => { const { date, vatId, name } = transfer; const debet = parseFloat(transfer.debet.replace(/ /g, '')); -- cgit v1.2.3