diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-20 00:12:40 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-20 00:12:40 +0300 |
commit | cd284d9e4c1f28bf7c31c52d71e76878a3ff5f4a (patch) | |
tree | ed6b97b4e9bc85c921869de004dfff8f5e2066dc | |
parent | ad530510a4aba72f3074f1a7dec82cc9018db7b4 (diff) | |
download | commercel-api-cd284d9e4c1f28bf7c31c52d71e76878a3ff5f4a.tar.gz |
feat: move formatDate to hooks
-rw-r--r-- | src/hooks/formatDate.ts | 11 | ||||
-rw-r--r-- | src/services/index.ts | 2 | ||||
-rw-r--r-- | src/services/tranfers.service.ts | 81 | ||||
-rw-r--r-- | src/services/transfers.service.ts | 32 | ||||
-rw-r--r-- | src/services/waybills.service.ts | 63 |
5 files changed, 101 insertions, 88 deletions
diff --git a/src/hooks/formatDate.ts b/src/hooks/formatDate.ts new file mode 100644 index 0000000..ada8502 --- /dev/null +++ b/src/hooks/formatDate.ts @@ -0,0 +1,11 @@ +import { HookContext } from '@feathersjs/feathers'; +import { populate, alterItems } from 'feathers-hooks-common'; +import moment from 'moment'; +import _ from 'lodash'; + +const formatDate = (fieldName = 'date') => alterItems(item => { + const date = _.get(item, fieldName); + return _.set(item, fieldName, moment(date).format('YYYY-MM-DD')); +}); + +export default formatDate; diff --git a/src/services/index.ts b/src/services/index.ts index 120b98a..5ecbc15 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -2,7 +2,7 @@ import { Application } from '@feathersjs/express'; import Products from './products.service'; import Contractors from './contractors.service'; import Waybills from './waybills.service'; -import Tranfers from './tranfers.service'; +import Tranfers from './transfers.service'; import Spreadsheets from './spreadsheets.service'; export default (app: Application): void => { diff --git a/src/services/tranfers.service.ts b/src/services/tranfers.service.ts deleted file mode 100644 index e36e4f9..0000000 --- a/src/services/tranfers.service.ts +++ /dev/null @@ -1,81 +0,0 @@ -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, - }, - }) -}; - diff --git a/src/services/transfers.service.ts b/src/services/transfers.service.ts new file mode 100644 index 0000000..cf6a490 --- /dev/null +++ b/src/services/transfers.service.ts @@ -0,0 +1,32 @@ +import { Application } from '@feathersjs/express'; +import service from 'feathers-mongoose'; +import { populate } 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' + } + ] +}; + +export default (app: Application): void => { + app.use('/transfers', transfers); + + app.service('transfers').hooks({ + after: { + all: [ + populate({ schema: populateSchema }), + formatDate(), + ], + }, + }) +}; + diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts index 02ab2de..824be4b 100644 --- a/src/services/waybills.service.ts +++ b/src/services/waybills.service.ts @@ -1,9 +1,14 @@ import { Application } from '@feathersjs/express'; +import { HookContext } from '@feathersjs/feathers'; import service from 'feathers-mongoose'; -import { populate } from 'feathers-hooks-common'; -import Model from '../models/transfer/transfer.model'; +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 formatDate from '../hooks/formatDate'; -const transfers = service({ Model }); +const waybills = service({ Model }); const populateSchema = { include: [ @@ -12,19 +17,65 @@ const populateSchema = { 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}`; + return { ...item, name, total }; +}; + export default (app: Application): void => { - app.use('/transfers', transfers); + app.use('/waybills', waybills); - app.service('transfers').hooks({ + app.service('waybills').hooks({ after: { all: [ populate({ schema: populateSchema }), + alterItems(addFields), + formatDate(), ], }, + before: { + patch: reflectStatus, + }, }) }; |