import mongoose from 'mongoose'; import { generateId } from '../../utils.js'; const { Schema } = mongoose; import { aggregateRollups, aggregateRollupsHistory, editObject } from '../../database.js'; import { stockEventModel } from './stockevent.schema.js'; const getStockEventTotal = async (stock, parentType) => { const stockId = stock?._id; if (!stockId) return null; const parentId = stockId instanceof mongoose.Types.ObjectId ? stockId : new mongoose.Types.ObjectId(stockId); const [result] = await stockEventModel.aggregate([ { $match: { parent: parentId, parentType } }, { $group: { _id: null, total: { $sum: '$value' }, count: { $sum: 1 } } }, ]); return { total: result?.total ?? 0, count: result?.count ?? 0, }; }; // Define the main filamentStock schema const filamentStockSchema = new Schema( { _reference: { type: String, default: () => generateId()() }, state: { type: { type: String, required: true }, progress: { type: Number, required: false }, }, startingWeight: { net: { type: Number, required: true }, gross: { type: Number, required: true }, }, currentWeight: { net: { type: Number, required: true }, gross: { type: Number, required: true }, }, filament: { type: mongoose.Schema.Types.ObjectId, ref: 'filament', required: true }, filamentSku: { type: mongoose.Schema.Types.ObjectId, ref: 'filamentSku', required: true }, stockLocation: { type: mongoose.Schema.Types.ObjectId, ref: 'stockLocation', required: false, }, }, { timestamps: true } ); filamentStockSchema.pre('validate', async function () { if (!this.filament && this.filamentSku) { const sku = await mongoose .model('filamentSku') .findById(this.filamentSku) .select('filament') .lean(); if (sku?.filament) this.filament = sku.filament; } }); const rollupConfigs = [ { name: 'totalCurrentWeight', filter: {}, rollups: [{ name: 'totalCurrentWeight', property: 'currentWeight.net', operation: 'sum' }], }, ]; filamentStockSchema.statics.stats = async function () { const results = await aggregateRollups({ model: this, rollupConfigs: rollupConfigs, }); return results; }; filamentStockSchema.statics.history = async function (from, to) { const results = await aggregateRollupsHistory({ model: this, startDate: from, endDate: to, rollupConfigs: rollupConfigs, }); // Return time-series data array return results; }; filamentStockSchema.statics.recalculate = async function (filamentStock, user) { const events = await getStockEventTotal(filamentStock, this.modelName); if (!events?.count) return; const net = events.total; const startingNet = filamentStock.startingWeight?.net ?? 0; const startingGross = filamentStock.startingWeight?.gross ?? 0; const gross = startingNet > 0 ? (startingGross * net) / startingNet : net; console.log('Recalculating filament stock'); console.log('events', events); console.log('filamentStock', filamentStock); await editObject({ model: this, id: filamentStock._id, updateData: { currentWeight: { net, gross, }, }, user, recalculate: false, }); }; // Add virtual id getter filamentStockSchema.virtual('id').get(function () { return this._id; }); // Configure JSON serialization to include virtuals filamentStockSchema.set('toJSON', { virtuals: true }); // Create and export the model export const filamentStockModel = mongoose.model('filamentStock', filamentStockSchema);