Refactor event handling in AmplidPlugin to improve error handling and streamline status updates for DocumentLineItems

This commit is contained in:
Dennes Schäffler
2026-05-07 09:48:04 +02:00
parent 83eb8b36bd
commit 139fe4bccb

View File

@@ -46,29 +46,35 @@ export class AmplidPlugin implements OnApplicationBootstrap {
this.eventBus.registerBlockingEventHandler(DocumentLineItemStatusChangedEvent, { this.eventBus.registerBlockingEventHandler(DocumentLineItemStatusChangedEvent, {
id: 'amplid-plugin-document-line-item-status-changed', id: 'amplid-plugin-document-line-item-status-changed',
handler: async (event) => { handler: async (event) => {
if (event.newStatus == DocumentStatusType.COMPLETED) {
if (!event.dli) { try {
this.logger.error(`DocumentLineItem not found in event`, "AMPLID PLUGIN");
return;
}
let bestellteMenge = +event.dli.customFields?.[this.customFieldForBestellteMenge]; if (event.newStatus == DocumentStatusType.COMPLETED) {
if (bestellteMenge && bestellteMenge > 0) { if (!event.dli) {
if (bestellteMenge == event.dli.quantity && event.dli.openQuantity == 0) { this.logger.error(`DocumentLineItem not found in event`, "AMPLID PLUGIN");
//we are good, we can keep it being completed return;
this.logger.info(`Keeping status as completed for DocumentLineItem ${event.dli.id} because bestellteMenge ${bestellteMenge} is equal to quantity ${event.dli.quantity} and openQuantity ${event.dli.openQuantity}`); }
} else {
//we need to change the status to inwork let bestellteMenge = +event.dli.customFields?.[this.customFieldForBestellteMenge];
this.logger.info(`Changing status to inwork for DocumentLineItem ${event.dli.id} because bestellteMenge ${bestellteMenge} is not equal to quantity ${event.dli.quantity} and openQuantity ${event.dli.openQuantity}`);
event.dli.status = DocumentStatusType.INWORK; if (bestellteMenge && bestellteMenge > 0) {
await DocumentLineItem.update(event.dli.id, { status: DocumentStatusType.INWORK }); if (bestellteMenge == event.dli.quantity && event.dli.openQuantity == 0) {
//we are good, we can keep it being completed
this.logger.info(`Keeping status as completed for DocumentLineItem ${event.dli.id} because bestellteMenge ${bestellteMenge} is equal to quantity ${event.dli.quantity} and openQuantity ${event.dli.openQuantity}`);
} else {
//we need to change the status to inwork
this.logger.info(`Changing status to inwork for DocumentLineItem ${event.dli.id} because bestellteMenge ${bestellteMenge} is not equal to quantity ${event.dli.quantity} and openQuantity ${event.dli.openQuantity}`);
event.dli.status = DocumentStatusType.INWORK;
await DocumentLineItem.update(event.dli.id, { status: DocumentStatusType.INWORK });
}
}
else {
this.logger.info(`Keeping status as completed for DocumentLineItem ${event.dli.id} because bestelltemenge ${bestellteMenge} is not set`);
} }
} }
else { } catch (error: any) {
this.logger.info(`Keeping status as completed for DocumentLineItem ${event.dli.id} because bestelltemenge ${bestellteMenge} is not set`); this.logger.error(error);
}
} }
}, },
}); });
@@ -76,31 +82,35 @@ export class AmplidPlugin implements OnApplicationBootstrap {
this.eventBus.registerBlockingEventHandler(EntitySavedEvent, { this.eventBus.registerBlockingEventHandler(EntitySavedEvent, {
id: 'amplid-plugin-post-entity-saved', id: 'amplid-plugin-post-entity-saved',
handler: async (event) => { handler: async (event) => {
if (event.entity === DocumentLineItem.name) { try {
if (event.entity === DocumentLineItem.name) {
let dli = event.input as DocumentLineItem; let dli = event.input as DocumentLineItem;
let bestellteMenge = +dli.customFields?.[this.customFieldForBestellteMenge]; let bestellteMenge = +dli.customFields?.[this.customFieldForBestellteMenge];
if (bestellteMenge && bestellteMenge > 0) { if (bestellteMenge && bestellteMenge > 0) {
if (bestellteMenge == dli.quantity && dli.openQuantity == 0) { if (bestellteMenge == dli.quantity && dli.openQuantity == 0) {
if (dli.status != DocumentStatusType.COMPLETED) { if (dli.status != DocumentStatusType.COMPLETED) {
this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestellteMenge ${bestellteMenge} is equal to quantity ${dli.quantity} and openQuantity ${dli.openQuantity}`); this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestellteMenge ${bestellteMenge} is equal to quantity ${dli.quantity} and openQuantity ${dli.openQuantity}`);
dli.status = DocumentStatusType.COMPLETED; dli.status = DocumentStatusType.COMPLETED;
await DocumentLineItem.update(dli.id, { status: DocumentStatusType.COMPLETED }); await DocumentLineItem.update(dli.id, { status: DocumentStatusType.COMPLETED });
}
} else {
if (dli.status != DocumentStatusType.INWORK) {
//we need to change the status to inwork
this.logger.info(`Changing status to inwork for DocumentLineItem ${dli.id} because bestellteMenge ${bestellteMenge} is not equal to quantity ${dli.quantity} and openQuantity ${dli.openQuantity}`);
dli.status = DocumentStatusType.INWORK;
await DocumentLineItem.update(dli.id, { status: DocumentStatusType.INWORK });
}
} }
} else { } else {
this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestelltemenge ${bestellteMenge} is not set`);
if (dli.status != DocumentStatusType.INWORK) {
//we need to change the status to inwork
this.logger.info(`Changing status to inwork for DocumentLineItem ${dli.id} because bestellteMenge ${bestellteMenge} is not equal to quantity ${dli.quantity} and openQuantity ${dli.openQuantity}`);
dli.status = DocumentStatusType.INWORK;
await DocumentLineItem.update(dli.id, { status: DocumentStatusType.INWORK });
}
} }
} else {
this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestelltemenge ${bestellteMenge} is not set`);
} }
} catch (error: any) {
this.logger.error(error);
} }
}, },
}); });