From f3a3d3914ab7aea25c9115f4030a2b8aac7a3b69 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 26 Jul 2026 21:21:35 +0100 Subject: [PATCH] Enhance gcode file schema to populate part information from partSku - Added logic in the pre-validation hook to populate the `part` field in `parts` array based on the `partSku` if it is not already set. - Improved data integrity by ensuring that part information is correctly retrieved and assigned during validation. --- src/database/schemas/production/gcodefile.schema.js | 8 ++++++++ 1 file changed, 8 insertions(+) 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' });