summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-17 02:47:50 +0300
committereug-vs <eug-vs@keemail.me>2021-03-17 02:47:50 +0300
commit920ff53d1a9a4840fb6ad9198c5db176d70c7edf (patch)
treeb550f70f3099c114e22b6a88b1fb482a6ff8efb4
parenta76ec7c41ebc5c208d7b31c81f8a659bd1be159c (diff)
downloadcommercel-api-920ff53d1a9a4840fb6ad9198c5db176d70c7edf.tar.gz
feat: allow multiple products in a waybill
-rw-r--r--src/models/waybill/waybill.schema.ts32
-rw-r--r--src/services/waybills.service.ts55
2 files changed, 52 insertions, 35 deletions
diff --git a/src/models/waybill/waybill.schema.ts b/src/models/waybill/waybill.schema.ts
index ce14525..bbebcd4 100644
--- a/src/models/waybill/waybill.schema.ts
+++ b/src/models/waybill/waybill.schema.ts
@@ -2,29 +2,41 @@ import { Document, Schema, Types } from 'mongoose';
export interface WaybillSchema extends Document {
operation: 'in' | 'out';
- productId: string;
- quantity: number;
+ records: [{
+ productId: string;
+ price: number;
+ quantity: number;
+ }];
contractorId: string;
status: 'waiting' | 'executed' | 'cancelled';
}
-export const waybillSchema = new Schema({
- operation: {
- type: String,
- required: true,
- },
- contractorId: {
+
+const recordSchema = new Schema({
+ productId: {
type: Types.ObjectId,
required: true,
},
- productId: {
- type: Types.ObjectId,
+ price: {
+ type: Number,
required: true,
},
quantity: {
type: Number,
min: 1,
},
+});
+
+export const waybillSchema = new Schema({
+ operation: {
+ type: String,
+ required: true,
+ },
+ contractorId: {
+ type: Types.ObjectId,
+ required: true,
+ },
+ records: [recordSchema],
status: {
type: String,
default: 'waiting',
diff --git a/src/services/waybills.service.ts b/src/services/waybills.service.ts
index 5007fab..cf4bd7e 100644
--- a/src/services/waybills.service.ts
+++ b/src/services/waybills.service.ts
@@ -1,10 +1,11 @@
import { Application } from '@feathersjs/express';
import { HookContext } from '@feathersjs/feathers';
import service from 'feathers-mongoose';
-import { populate } from 'feathers-hooks-common';
+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 Contractor from '../models/contractor/contractor.model';
import Product from '../models/product/product.model';
@@ -33,35 +34,36 @@ const performWaybillOperation = async (id: string, cancel = false) => {
waybill.status = cancel ? 'cancelled' : 'executed';
const signMultiplier = (waybill.operation === 'in' ? 1 : -1) * (cancel ? -1 : 1);
+ const total = waybill.records.reduce((sum, record) => sum + record.price * record.quantity, 0);
- const product = await Product.findById(waybill.productId);
- if (!product) return;
- product.quantity += waybill.quantity * signMultiplier;
- if (product.quantity < 0) console.log('ВНИМАНИЕ: Недостаточно товара на складе');
+ await Bluebird.map(waybill.records, record => Product.updateOne(
+ { _id: record.productId },
+ {
+ $inc: {
+ quantity: record.quantity * signMultiplier
+ }
+ }
+ ));
- const contractor = await Contractor.findById(waybill.contractorId);
- if (!contractor) return;
- contractor.debt -= waybill.quantity * product.price * signMultiplier;
+ await Contractor.updateOne(
+ { _id: waybill.contractorId },
+ {
+ $inc: {
+ debt: total * signMultiplier * (-1)
+ }
+ }
+ );
- return Bluebird.all([waybill.save(), product.save(), contractor.save()]);
+ return waybill.save();
};
-const addName = (context: HookContext): HookContext => {
- const { result } = context;
-
- const addNameSingle = (item: any): void => {
- const { operation, product, quantity } = item;
- const op = operation === 'in' ? 'приход' : 'расход';
- const name = `Накладная: ${product?.name} ${op} ${quantity} шт.`
- return { name, ...item };
- };
- if (Array.isArray(result)) {
- context.result = result.map(addNameSingle);
- } else {
- context.result = addNameSingle(result);
- }
- return context;
+const addFields = (item: WaybillSchema) => {
+ const { operation, records } = item;
+ const total = item.records.reduce((sum, record) => sum + record.price * record.quantity, 0);
+ const op = operation === 'in' ? 'приход' : 'расход';
+ const name = `Накладная: ${op} $${total}`;
+ return { name, total, ...item };
};
export default (app: Application): void => {
@@ -79,7 +81,10 @@ export default (app: Application): void => {
app.service('waybills').hooks({
after: {
- all: [populate({ schema: populateSchema }), addName],
+ all: [
+ populate({ schema: populateSchema }),
+ alterItems(addFields)
+ ],
},
})
};