72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { generateId } from '../../utils.js';
|
|
const { Schema } = mongoose;
|
|
|
|
const stockTransferLineSchema = new Schema(
|
|
{
|
|
fromStockType: {
|
|
type: String,
|
|
required: true,
|
|
enum: ['filamentStock', 'partStock', 'productStock'],
|
|
},
|
|
fromStock: {
|
|
type: Schema.Types.ObjectId,
|
|
refPath: 'fromStockType',
|
|
required: true,
|
|
},
|
|
quantity: { type: Number, required: true },
|
|
toStockLocation: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'stockLocation',
|
|
required: true,
|
|
},
|
|
toStockType: {
|
|
type: String,
|
|
required: false,
|
|
enum: ['filamentStock', 'partStock', 'productStock'],
|
|
},
|
|
toStock: {
|
|
type: Schema.Types.ObjectId,
|
|
refPath: 'toStockType',
|
|
required: false,
|
|
},
|
|
},
|
|
{ _id: true }
|
|
);
|
|
|
|
const stockTransferSchema = new Schema(
|
|
{
|
|
_reference: { type: String, default: () => generateId()() },
|
|
state: {
|
|
type: { type: String, required: true, default: 'draft' },
|
|
progress: { type: Number, required: false },
|
|
},
|
|
postedAt: { type: Date, required: false },
|
|
lines: { type: [stockTransferLineSchema], default: [] },
|
|
},
|
|
{ timestamps: true }
|
|
);
|
|
|
|
stockTransferSchema.statics.stats = async function () {
|
|
const [draft, posted] = await Promise.all([
|
|
this.countDocuments({ 'state.type': 'draft' }),
|
|
this.countDocuments({ 'state.type': 'posted' }),
|
|
]);
|
|
return {
|
|
draft: { count: draft },
|
|
posted: { count: posted },
|
|
};
|
|
};
|
|
|
|
stockTransferSchema.statics.history = async function () {
|
|
return [];
|
|
};
|
|
|
|
stockTransferSchema.virtual('id').get(function () {
|
|
return this._id;
|
|
});
|
|
|
|
stockTransferSchema.set('toJSON', { virtuals: true });
|
|
|
|
export const stockTransferModel = mongoose.model('stockTransfer', stockTransferSchema);
|