From e1b6814efd3faad0495ef75b36a5a33d1f2d0ef5 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 26 Jul 2026 21:21:30 +0100 Subject: [PATCH] Enhanced gcodeFile schema to populate part information during validation, improving data integrity for parts. Updated service functions to reflect changes in population logic for parts, ensuring accurate data retrieval. --- src/database/schemas/production/gcodefile.schema.js | 8 ++++++++ src/services/production/gcodefiles.js | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/database/schemas/production/gcodefile.schema.js b/src/database/schemas/production/gcodefile.schema.js index a30eac2..964f619 100644 --- a/src/database/schemas/production/gcodefile.schema.js +++ b/src/database/schemas/production/gcodefile.schema.js @@ -28,6 +28,14 @@ gcodeFileSchema.pre('validate', async function () { const sku = await mongoose.model('filamentSku').findById(this.filamentSku).select('filament').lean(); if (sku?.filament) this.filament = sku.filament; } + if (this.parts?.length) { + for (const partItem of this.parts) { + if (!partItem.part && partItem.partSku) { + const sku = await mongoose.model('partSku').findById(partItem.partSku).select('part').lean(); + if (sku?.part) partItem.part = sku.part; + } + } + } }); gcodeFileSchema.index({ name: 'text', gcodeFileName: 'text' }); diff --git a/src/services/production/gcodefiles.js b/src/services/production/gcodefiles.js index 6c9d884..843fd03 100644 --- a/src/services/production/gcodefiles.js +++ b/src/services/production/gcodefiles.js @@ -96,7 +96,7 @@ export const getGCodeFileRouteHandler = async (req, res) => { const result = await getObject({ model: gcodeFileModel, id, - populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'], + populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.part', 'parts.partSku'], }); if (result?.error) { logger.warn(`GCodeFile not found with supplied id.`); @@ -143,7 +143,7 @@ export const editGCodeFileRouteHandler = async (req, res) => { id, updateData, user: req.user, - populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'], + populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.part', 'parts.partSku'], }); if (result.error) {