Enhance gcode file schema to populate part information from partSku
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- 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.
This commit is contained in:
Tom Butcher 2026-07-26 21:21:35 +01:00
parent 652d62c758
commit f3a3d3914a

View File

@ -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' });