From 486c3cb828414dac71188bf94448524a91ad4928 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 17 Mar 2021 03:19:27 +0300 Subject: feat: use services inside of the hook --- src/services/waybills.service.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'src/services/waybills.service.ts') 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 => { 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; }; -- cgit v1.2.3