From c04ce3243121359b66db3d6eadbc5230d897ca54 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 26 Jul 2026 21:21:42 +0100 Subject: [PATCH] Enhance part stock and stock event creation in PrinterDatabase. Update part retrieval to include partSku, improve logging for part stock creation, and add stock event creation with detailed information for subjobs. --- src/printer/database.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/printer/database.js b/src/printer/database.js index 1d1d23a..64986d3 100644 --- a/src/printer/database.js +++ b/src/printer/database.js @@ -693,33 +693,48 @@ export class PrinterDatabase { const gcodeFile = await this.socketClient.getObject({ objectType: "gcodeFile", _id: subJob.gcodeFile._id, - populate: ["parts.part"], + populate: ["parts.part", "parts.partSku"], }); const partItems = gcodeFile.parts; for (const partItem of partItems) { + const partSkuId = partItem.partSku?._id || partItem.partSku; logger.info( - `Posting part stock item for part ${ - partItem.part?.name || partItem.part._id + `Posting part stock item for part SKU ${ + partItem.partSku?.name || partSkuId } for subjob ${subJobId} for printer ${this.id}`, ); const quantity = partItem.quantity; - const part = partItem.part; - await this.socketClient.newObject({ + const partStock = await this.socketClient.newObject({ objectType: "partStock", newData: { sourceType: "subJob", - source: subJob, - part: part, - startingQuantity: quantity, + source: subJobId, + partSku: partSkuId, currentQuantity: quantity, state: { type: "new", - progress: 0, + progress: 1, }, }, }); + + logger.info( + `Creating stock event for part stock ${partStock._id} with quantity ${quantity} for subjob ${subJobId} for printer ${this.id}`, + ); + + await this.socketClient.newObject({ + objectType: "stockEvent", + newData: { + value: quantity, + unit: "", + parentType: "partStock", + parent: partStock._id, + owner: subJobId, + ownerType: "subJob", + }, + }); } } catch (error) { logger.error(