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.
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

This commit is contained in:
Tom Butcher 2026-07-26 21:21:42 +01:00
parent 8c6176b593
commit c04ce32431

View File

@ -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(