summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-20 21:15:09 +0300
committereug-vs <eug-vs@keemail.me>2021-03-20 21:15:09 +0300
commit4940f77145a6b3bfb7c3abb14dd35d1e5e306148 (patch)
treecf059e5fa38b39bba136c5a28ccdc806d44f867e
parent42cc6be0fc083ff5f7897a9e214918d750cb59ff (diff)
downloadcommercel-api-4940f77145a6b3bfb7c3abb14dd35d1e5e306148.tar.gz
feat: make use of stashBefore hook
-rw-r--r--src/services/transfers.service.ts25
-rw-r--r--src/services/waybills.service.ts8
2 files changed, 15 insertions, 18 deletions
diff --git a/src/services/transfers.service.ts b/src/services/transfers.service.ts
index c6fdad9..e2c70e4 100644
--- a/src/services/transfers.service.ts
+++ b/src/services/transfers.service.ts
@@ -1,7 +1,7 @@
import { Application } from '@feathersjs/express';
import { HookContext } from '@feathersjs/feathers';
import service from 'feathers-mongoose';
-import { populate, alterItems, getItems } from 'feathers-hooks-common';
+import { populate, alterItems, stashBefore, getItems } from 'feathers-hooks-common';
import Model from '../models/transfer/transfer.model';
import formatDate from '../hooks/formatDate';
@@ -26,26 +26,19 @@ const addFields = (item: any) => {
};
const applyTransfer = async (context: HookContext): Promise<HookContext> => {
- const { contractorId, amount, operation, _id } = getItems(context);
+ const { contractorId, amount, operation } = getItems(context);
- let inc;
+ // If amount is not supplied, do nothing
+ if (!amount) return context;
- // After Create / Delete
- if (context.method === 'create') inc = amount;
- if (context.method === 'remove') inc = -amount;
-
- // Before Patch / Update
- else if (['patch', 'update'].includes(context.method)) {
- const oldTransfer = await context.service.get(_id);
- inc = amount ? (amount - oldTransfer.amount) : 0;
- };
+ const inc = amount - (context.params.before?.amount || 0);
+ const sign = (operation === 'in' ? -1 : 1) * (context.method === 'remove' ? -1 : 1);
if (inc) await context.app.service('contractors').patch(contractorId, {
$inc: {
- debt: inc * (operation === 'in' ? -1 : 1)
+ debt: inc * sign
}
});
-
return context;
};
@@ -63,8 +56,8 @@ export default (app: Application): void => {
remove: applyTransfer,
},
before: {
- patch: applyTransfer,
- update: applyTransfer,
+ patch: [stashBefore(), applyTransfer],
+ update: [stashBefore(), applyTransfer],
}
})
};
diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts
index 824be4b..8fb6ef4 100644
--- a/src/services/waybills.service.ts
+++ b/src/services/waybills.service.ts
@@ -1,7 +1,7 @@
import { Application } from '@feathersjs/express';
import { HookContext } from '@feathersjs/feathers';
import service from 'feathers-mongoose';
-import { populate, alterItems } from 'feathers-hooks-common';
+import { populate, stashBefore, alterItems } from 'feathers-hooks-common';
import Bluebird from 'bluebird';
import _ from 'lodash';
import Model from '../models/waybill/waybill.model';
@@ -29,6 +29,10 @@ const populateSchema = {
const reflectStatus = async (context: HookContext): Promise<HookContext> => {
const { status } = context.data;
+
+ // If status has not changed, do nothing
+ if (status === context.params.before.status) return context;
+
if (['cancelled', 'executed'].includes(status) && context.id) {
const waybill: WaybillSchema = await context.service.get(context.id);
@@ -74,7 +78,7 @@ export default (app: Application): void => {
],
},
before: {
- patch: reflectStatus,
+ patch: [stashBefore(), reflectStatus],
},
})
};