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.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-07-26 21:21:30 +01:00
parent 5a4fecbae0
commit e1b6814efd
2 changed files with 10 additions and 2 deletions

View File

@ -28,6 +28,14 @@ gcodeFileSchema.pre('validate', async function () {
const sku = await mongoose.model('filamentSku').findById(this.filamentSku).select('filament').lean(); const sku = await mongoose.model('filamentSku').findById(this.filamentSku).select('filament').lean();
if (sku?.filament) this.filament = sku.filament; 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' }); gcodeFileSchema.index({ name: 'text', gcodeFileName: 'text' });

View File

@ -96,7 +96,7 @@ export const getGCodeFileRouteHandler = async (req, res) => {
const result = await getObject({ const result = await getObject({
model: gcodeFileModel, model: gcodeFileModel,
id, id,
populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'], populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.part', 'parts.partSku'],
}); });
if (result?.error) { if (result?.error) {
logger.warn(`GCodeFile not found with supplied id.`); logger.warn(`GCodeFile not found with supplied id.`);
@ -143,7 +143,7 @@ export const editGCodeFileRouteHandler = async (req, res) => {
id, id,
updateData, updateData,
user: req.user, user: req.user,
populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'], populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.part', 'parts.partSku'],
}); });
if (result.error) { if (result.error) {