summaryrefslogtreecommitdiff
path: root/src/services/waybills.service.ts
blob: e947f0ab0dbf3d62dfa22e8312d90a2e6ecf1641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 });

const populateSchema = {
  include: [
    {
      service: 'contractors',
      nameAs: 'contractor',
      parentField: 'contractorId',
      childField: '_id'
    },
    {
      service: 'products',
      nameAs: 'product',
      parentField: 'productId',
      childField: '_id'
    },
  ]
};

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 }), addName],
    },
  })
};