diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-17 03:19:27 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-17 03:19:27 +0300 |
commit | 486c3cb828414dac71188bf94448524a91ad4928 (patch) | |
tree | 053f0323bd0f4f35e1dc73fc9b56192e322a0e62 | |
parent | ed70c5fa9fd5bfbe564ab99d4e13ca6d47cb4661 (diff) | |
download | commercel-api-486c3cb828414dac71188bf94448524a91ad4928.tar.gz |
feat: use services inside of the hook
-rw-r--r-- | src/services/waybills.service.ts | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts index 8c8c718..ce820bc 100644 --- a/src/services/waybills.service.ts +++ b/src/services/waybills.service.ts @@ -6,8 +6,6 @@ import Bluebird from 'bluebird'; import _ from 'lodash'; import Model from '../models/waybill/waybill.model'; import { WaybillSchema } from '../models/waybill/waybill.schema'; -import Contractor from '../models/contractor/contractor.model'; -import Product from '../models/product/product.model'; const waybills = service({ Model }); @@ -36,23 +34,19 @@ const reflectStatus = async (context: HookContext): Promise<HookContext> => { const signMultiplier = (waybill.operation === 'in' ? 1 : -1) * (status === 'cancelled' ? -1 : 1); const total = waybill.records.reduce((sum, record) => sum + record.price * record.quantity, 0); - await Bluebird.map(waybill.records, record => Product.updateOne( - { _id: record.productId }, - { + await Bluebird.map(waybill.records, record => { + return context.app.service('products').patch(record.productId, { $inc: { quantity: record.quantity * signMultiplier } - } - )); + }); + }); - await Contractor.updateOne( - { _id: waybill.contractorId }, - { - $inc: { - debt: total * signMultiplier * (-1) - } + await context.app.service('contractors').patch(waybill.contractorId, { + $inc: { + debt: total * signMultiplier * (-1) } - ); + }); } return context; }; |