summaryrefslogtreecommitdiff
path: root/src/services/transfers.service.ts
blob: 1d7bdafa46270769fb98bfff97481a3c7323e346 (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
import { Application } from '@feathersjs/express';
import service from 'feathers-mongoose';
import { populate, alterItems } from 'feathers-hooks-common';
import Model from '../models/transfer/transfer.model';
import formatDate from '../hooks/formatDate';

const transfers = service({ Model });

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

const addFields = (item: any) => {
  const { operation, contractor, amount } = item;
  const op = operation === 'in' ? 'приход' : 'расход';
  const name = `Перевод: ${op} $${amount} от ${contractor.name}`;
  return { ...item, name };
};

export default (app: Application): void => {
  app.use('/transfers', transfers);

  app.service('transfers').hooks({
    after: {
      all: [
        populate({ schema: populateSchema }),
        alterItems(addFields),
        formatDate(),
      ],
    },
  })
};