From 3b0e3280421c94089cd86e833246daebe5424fc9 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 15 Mar 2021 21:27:35 +0300 Subject: feat: compute waybill name --- package.json | 1 + src/services/waybills.service.ts | 10 +++++++++- yarn.lock | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ef74095..254abd0 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "devDependencies": { "@types/bluebird": "^3.5.33", "@types/cors": "^2.8.10", + "@types/lodash": "^4.14.168", "@types/mongoose": "^5.10.3" } } diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts index 0744bee..e947f0a 100644 --- a/src/services/waybills.service.ts +++ b/src/services/waybills.service.ts @@ -1,6 +1,8 @@ import { Application } from '@feathersjs/express'; +import { HookContext } from '@feathersjs/feathers'; import service from 'feathers-mongoose'; import { populate } from 'feathers-hooks-common'; +import _ from 'lodash'; import Model from '../models/waybill/waybill.model'; const waybills = service({ Model }); @@ -22,11 +24,17 @@ const populateSchema = { ] }; +const addName = (context: HookContext): HookContext => { + const { result: { operation, product, quantity } } = context; + const name = `Накладная: ${product?.name} ${operation === 'in' ? 'приход' : 'расход' } ${quantity} шт.` + return _.set(context, 'result.name', name); +}; + export default (app: Application): void => { app.use('/waybills', waybills); app.service('waybills').hooks({ after: { - all: populate({ schema: populateSchema }), + all: [populate({ schema: populateSchema }), addName], }, }) }; diff --git a/yarn.lock b/yarn.lock index e7be360..4cd23ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -131,6 +131,11 @@ "@types/qs" "*" "@types/serve-static" "*" +"@types/lodash@^4.14.168": + version "4.14.168" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008" + integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q== + "@types/mime@^1": version "1.3.2" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a" -- cgit v1.2.3