diff options
Diffstat (limited to 'src/services/transfers.service.ts')
-rw-r--r-- | src/services/transfers.service.ts | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/services/transfers.service.ts b/src/services/transfers.service.ts index d835844..1f8aaf5 100644 --- a/src/services/transfers.service.ts +++ b/src/services/transfers.service.ts @@ -26,19 +26,33 @@ const addFields = (item: any) => { }; const applyTransfer = async (context: HookContext): Promise<HookContext> => { - const { contractorId, amount, operation } = getItems(context); + const { contractorId, accountId, amount, operation } = getItems(context); // If amount is not supplied, do nothing if (!amount) return context; const inc = amount - (context.params.before?.amount || 0); - const sign = (operation === 'in' ? -1 : 1) * (context.method === 'remove' ? -1 : 1); + const sign = (operation === 'in' ? 1 : -1) * (context.method === 'remove' ? -1 : 1); + + if (inc) { + // TODO: start transaction + await context.app + .service('accounts') + .patch(accountId, { + $inc: { + balance: inc * sign + } + }); + + await context.app + .service('contractors') + .patch(contractorId, { + $inc: { + debt: inc * sign * (-1) + } + }); + } - if (inc) await context.app.service('contractors').patch(contractorId, { - $inc: { - debt: inc * sign - } - }); return context; }; |