From 766f4722ea5dbe054d0c5b5bc0fb852ae98a66f6 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 26 Jul 2026 19:00:30 +0100 Subject: [PATCH] Refactor stock event schema for improved readability and consistency - Cleaned up the stock event schema by removing unnecessary line breaks and ensuring consistent formatting across the code. - Enhanced readability of function parameters and return statements for better maintainability. - Made minor adjustments to comments and structure to align with coding standards. --- .../schemas/inventory/stockevent.schema.js | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/database/schemas/inventory/stockevent.schema.js b/src/database/schemas/inventory/stockevent.schema.js index 8809575..eaf6896 100644 --- a/src/database/schemas/inventory/stockevent.schema.js +++ b/src/database/schemas/inventory/stockevent.schema.js @@ -6,13 +6,13 @@ const { Schema } = mongoose; const parentStockModelNames = { filamentStock: 'filamentStock', partStock: 'partStock', - productStock: 'productStock', + productStock: 'productStock' }; const initialStockStates = { filamentStock: 'unconsumed', partStock: 'new', - productStock: 'posted', + productStock: 'posted' }; const getStartingAmount = (parentType, parentStock) => { @@ -23,7 +23,12 @@ const getStartingAmount = (parentType, parentStock) => { return parentStock.startingQuantity ?? 0; }; -const buildParentState = (parentType, parentStock, currentAmount, startingAmount) => { +const buildParentState = ( + parentType, + parentStock, + currentAmount, + startingAmount +) => { if (parentStock.state?.type === 'draft') { return undefined; } @@ -43,8 +48,6 @@ const buildParentState = (parentType, parentStock, currentAmount, startingAmount const progress = currentAmount / startingAmount; - console.log('progress', progress); - if (currentAmount === startingAmount) { return { ...parentStock.state, type: fullState, progress: 1 }; } @@ -60,18 +63,20 @@ const getStockEventTotal = async (parentId, parentType) => { if (!parentId) return null; const objectId = - parentId instanceof mongoose.Types.ObjectId ? parentId : new mongoose.Types.ObjectId(parentId); + parentId instanceof mongoose.Types.ObjectId + ? parentId + : new mongoose.Types.ObjectId(parentId); const [result] = await mongoose .model('stockEvent') .aggregate([ { $match: { parent: objectId, parentType } }, - { $group: { _id: null, total: { $sum: '$value' }, count: { $sum: 1 } } }, + { $group: { _id: null, total: { $sum: '$value' }, count: { $sum: 1 } } } ]); return { total: result?.total ?? 0, - count: result?.count ?? 0, + count: result?.count ?? 0 }; }; @@ -108,7 +113,8 @@ const HISTORY_RATE_LIMIT_MS = 3000; const isWithinHistoryRateLimit = (lastEntry, timestamp = new Date()) => { if (!lastEntry?.timestamp) return false; - const elapsed = new Date(timestamp).getTime() - new Date(lastEntry.timestamp).getTime(); + const elapsed = + new Date(timestamp).getTime() - new Date(lastEntry.timestamp).getTime(); return elapsed < HISTORY_RATE_LIMIT_MS; }; @@ -116,7 +122,9 @@ const getLastParentHistoryValue = (parentType, history = []) => { const lastEntry = history.at(-1); if (!lastEntry) return undefined; - return parentType === 'filamentStock' ? lastEntry.currentWeight : lastEntry.currentQuantity; + return parentType === 'filamentStock' + ? lastEntry.currentWeight + : lastEntry.currentQuantity; }; const parentValuesEqual = (parentType, a, b) => { @@ -147,7 +155,9 @@ const appendParentHistoryIfChanged = ( const history = parentStock.history || []; const lastEntry = history.at(-1); const currentValue = - parentType === 'filamentStock' ? updateData.currentWeight : updateData.currentQuantity; + parentType === 'filamentStock' + ? updateData.currentWeight + : updateData.currentQuantity; const lastHistoryValue = getLastParentHistoryValue(parentType, history); if (parentValuesEqual(parentType, currentValue, lastHistoryValue)) { @@ -160,7 +170,10 @@ const appendParentHistoryIfChanged = ( return { ...updateData, - history: [...history, buildParentHistoryEntry(parentType, currentValue, timestamp)], + history: [ + ...history, + buildParentHistoryEntry(parentType, currentValue, timestamp) + ] }; }; @@ -174,7 +187,7 @@ const recalculateParentStock = async (parentType, parentId, user) => { const parentStock = await getObject({ model: parentModel, id: parentId, - cached: true, + cached: true }); if (!parentStock || parentStock.error) return; @@ -190,7 +203,7 @@ const recalculateParentStock = async (parentType, parentId, user) => { buildParentUpdateData(parentType, parentStock, events) ), user, - recalculate: false, + recalculate: false }); }; @@ -202,30 +215,30 @@ const stockEventSchema = new Schema( parent: { type: Schema.Types.ObjectId, refPath: 'parentType', - required: true, + required: true }, parentType: { type: String, required: true, - enum: ['filamentStock', 'partStock', 'productStock'], // Add other models as needed + enum: ['filamentStock', 'partStock', 'productStock'] // Add other models as needed }, owner: { type: Schema.Types.ObjectId, refPath: 'ownerType', - required: true, + required: true }, ownerType: { type: String, required: true, - enum: ['user', 'subJob', 'stockAudit', 'stockTransfer'], + enum: ['user', 'subJob', 'stockAudit', 'stockTransfer'] }, history: [ { value: { type: Number, required: true }, - timestamp: { type: Date, default: Date.now }, - }, + timestamp: { type: Date, default: Date.now } + } ], - timestamp: { type: Date, default: Date.now }, + timestamp: { type: Date, default: Date.now } }, { timestamps: true } ); @@ -239,15 +252,18 @@ stockEventSchema.statics.recalculate = async function (stockEvent, user) { const currentValue = stockEvent.value; const timestamp = stockEvent.timestamp || new Date(); - if (currentValue !== lastHistoryValue && !isWithinHistoryRateLimit(lastEntry, timestamp)) { + if ( + currentValue !== lastHistoryValue && + !isWithinHistoryRateLimit(lastEntry, timestamp) + ) { await editObject({ model: this, id: stockEvent._id, updateData: { - history: [...history, { value: currentValue, timestamp }], + history: [...history, { value: currentValue, timestamp }] }, user, - recalculate: false, + recalculate: false }); }