diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/models/waybill/waybill.schema.ts | 32 | ||||
| -rw-r--r-- | src/services/waybills.service.ts | 55 | 
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) +      ],      },    })  };  |