summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-04-24 21:11:10 +0300
committereug-vs <eug-vs@keemail.me>2021-04-24 21:11:10 +0300
commit0bc7ef4bd32e5e65db502eec63f9723f345a66d7 (patch)
tree759c687bf85f8a797b11ab3501fc410ab7dec1b4
parent96a2ac822ff72fabcd25d2f06a272ecac902c77d (diff)
downloadcommercel-api-0bc7ef4bd32e5e65db502eec63f9723f345a66d7.tar.gz
feat: change balance when applying transfer
-rw-r--r--src/services/transfers.service.ts28
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;
};