diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-19 23:57:18 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-19 23:57:18 +0300 |
commit | ad530510a4aba72f3074f1a7dec82cc9018db7b4 (patch) | |
tree | 5f684dce9eee4b67f18f20864bc26d83a3a4a075 /src/services/tranfers.service.ts | |
parent | 3109e25f2cabb85bbccf4ba509e112e49f5c0120 (diff) | |
download | commercel-api-ad530510a4aba72f3074f1a7dec82cc9018db7b4.tar.gz |
feat: add Transfers service
Diffstat (limited to 'src/services/tranfers.service.ts')
-rw-r--r-- | src/services/tranfers.service.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/services/tranfers.service.ts b/src/services/tranfers.service.ts new file mode 100644 index 0000000..e36e4f9 --- /dev/null +++ b/src/services/tranfers.service.ts @@ -0,0 +1,81 @@ +import { Application } from '@feathersjs/express'; +import { HookContext } from '@feathersjs/feathers'; +import service from 'feathers-mongoose'; +import { populate, alterItems } from 'feathers-hooks-common'; +import Bluebird from 'bluebird'; +import _ from 'lodash'; +import moment from 'moment'; +import Model from '../models/waybill/waybill.model'; +import { WaybillSchema } from '../models/waybill/waybill.schema'; + +const waybills = service({ Model }); + +const populateSchema = { + include: [ + { + service: 'contractors', + nameAs: 'contractor', + parentField: 'contractorId', + childField: '_id' + }, + { + service: 'products', + nameAs: 'product', + parentField: 'productId', + childField: '_id' + }, + ] +}; + +const reflectStatus = async (context: HookContext): Promise<HookContext> => { + const { status } = context.data; + if (['cancelled', 'executed'].includes(status) && context.id) { + const waybill: WaybillSchema = await context.service.get(context.id); + + 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 => { + return context.app.service('products').patch(record.productId, { + $inc: { + quantity: record.quantity * signMultiplier + } + }); + }); + + await context.app.service('contractors').patch(waybill.contractorId, { + $inc: { + debt: total * signMultiplier * (-1) + } + }); + } + return context; +}; + + + +const addFields = (item: WaybillSchema) => { + const { operation, records, date } = item; + const total = item.records.reduce((sum, record) => sum + record.price * record.quantity, 0); + const op = operation === 'in' ? 'приход' : 'расход'; + const name = `Накладная: ${op} $${total}`; + const formattedDate = moment(date).format('YYYY-MM-DD'); + return { ...item, name, total, date: formattedDate }; +}; + +export default (app: Application): void => { + app.use('/waybills', waybills); + + app.service('waybills').hooks({ + after: { + all: [ + populate({ schema: populateSchema }), + alterItems(addFields) + ], + }, + before: { + patch: reflectStatus, + }, + }) +}; + |