diff options
author | eug-vs <eug-vs@keemail.me> | 2021-04-24 21:26:33 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-04-24 21:26:33 +0300 |
commit | 5cba4b888ce86350b306bd65e7929e9a4f146b01 (patch) | |
tree | d5262db150ca3158ffa9f33e7691f7e53337620f | |
parent | 0bc7ef4bd32e5e65db502eec63f9723f345a66d7 (diff) | |
download | commercel-api-5cba4b888ce86350b306bd65e7929e9a4f146b01.tar.gz |
feat: add currency to account
-rw-r--r-- | src/models/account/account.schema.ts | 5 | ||||
-rw-r--r-- | 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<any[]> => { }); }; +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<HookContext> => { const { id } = context.result; @@ -34,9 +53,10 @@ const parseTransfersBill = async (context: HookContext): Promise<HookContext> => 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<HookContext> => 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, '')); |