diff options
Diffstat (limited to 'src/services/waybills.service.ts')
-rw-r--r-- | src/services/waybills.service.ts | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts index 5007fab..cf4bd7e 100644 --- a/src/services/waybills.service.ts +++ b/src/services/waybills.service.ts @@ -1,10 +1,11 @@ import { Application } from '@feathersjs/express'; import { HookContext } from '@feathersjs/feathers'; import service from 'feathers-mongoose'; -import { populate } from 'feathers-hooks-common'; +import { populate, alterItems } from 'feathers-hooks-common'; 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'; @@ -33,35 +34,36 @@ const performWaybillOperation = async (id: string, cancel = false) => { waybill.status = cancel ? 'cancelled' : 'executed'; const signMultiplier = (waybill.operation === 'in' ? 1 : -1) * (cancel ? -1 : 1); + const total = waybill.records.reduce((sum, record) => sum + record.price * record.quantity, 0); - const product = await Product.findById(waybill.productId); - if (!product) return; - product.quantity += waybill.quantity * signMultiplier; - if (product.quantity < 0) console.log('ВНИМАНИЕ: Недостаточно товара на складе'); + await Bluebird.map(waybill.records, record => Product.updateOne( + { _id: record.productId }, + { + $inc: { + quantity: record.quantity * signMultiplier + } + } + )); - const contractor = await Contractor.findById(waybill.contractorId); - if (!contractor) return; - contractor.debt -= waybill.quantity * product.price * signMultiplier; + await Contractor.updateOne( + { _id: waybill.contractorId }, + { + $inc: { + debt: total * signMultiplier * (-1) + } + } + ); - return Bluebird.all([waybill.save(), product.save(), contractor.save()]); + return waybill.save(); }; -const addName = (context: HookContext): HookContext => { - const { result } = context; - - const addNameSingle = (item: any): void => { - const { operation, product, quantity } = item; - const op = operation === 'in' ? 'приход' : 'расход'; - const name = `Накладная: ${product?.name} ${op} ${quantity} шт.` - return { name, ...item }; - }; - if (Array.isArray(result)) { - context.result = result.map(addNameSingle); - } else { - context.result = addNameSingle(result); - } - return context; +const addFields = (item: WaybillSchema) => { + const { operation, records } = item; + const total = item.records.reduce((sum, record) => sum + record.price * record.quantity, 0); + const op = operation === 'in' ? 'приход' : 'расход'; + const name = `Накладная: ${op} $${total}`; + return { name, total, ...item }; }; export default (app: Application): void => { @@ -79,7 +81,10 @@ export default (app: Application): void => { app.service('waybills').hooks({ after: { - all: [populate({ schema: populateSchema }), addName], + all: [ + populate({ schema: populateSchema }), + alterItems(addFields) + ], }, }) }; |