diff --git a/index.ts b/index.ts index db96399..83bc94c 100644 --- a/index.ts +++ b/index.ts @@ -46,29 +46,35 @@ export class AmplidPlugin implements OnApplicationBootstrap { this.eventBus.registerBlockingEventHandler(DocumentLineItemStatusChangedEvent, { id: 'amplid-plugin-document-line-item-status-changed', handler: async (event) => { - if (event.newStatus == DocumentStatusType.COMPLETED) { - if (!event.dli) { - this.logger.error(`DocumentLineItem not found in event`, "AMPLID PLUGIN"); - return; - } + try { - let bestellteMenge = +event.dli.customFields?.[this.customFieldForBestellteMenge]; + if (event.newStatus == DocumentStatusType.COMPLETED) { - if (bestellteMenge && bestellteMenge > 0) { - 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 }); + if (!event.dli) { + this.logger.error(`DocumentLineItem not found in event`, "AMPLID PLUGIN"); + return; + } + + let bestellteMenge = +event.dli.customFields?.[this.customFieldForBestellteMenge]; + + if (bestellteMenge && bestellteMenge > 0) { + 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 { - this.logger.info(`Keeping status as completed for DocumentLineItem ${event.dli.id} because bestelltemenge ${bestellteMenge} is not set`); - } + } catch (error: any) { + this.logger.error(error); } }, }); @@ -76,31 +82,35 @@ export class AmplidPlugin implements OnApplicationBootstrap { this.eventBus.registerBlockingEventHandler(EntitySavedEvent, { id: 'amplid-plugin-post-entity-saved', handler: async (event) => { - if (event.entity === DocumentLineItem.name) { + try { + if (event.entity === DocumentLineItem.name) { - let dli = event.input as DocumentLineItem; - let bestellteMenge = +dli.customFields?.[this.customFieldForBestellteMenge]; + let dli = event.input as DocumentLineItem; + let bestellteMenge = +dli.customFields?.[this.customFieldForBestellteMenge]; - if (bestellteMenge && bestellteMenge > 0) { - if (bestellteMenge == dli.quantity && dli.openQuantity == 0) { - 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}`); - dli.status = DocumentStatusType.COMPLETED; - await DocumentLineItem.update(dli.id, { status: DocumentStatusType.COMPLETED }); + if (bestellteMenge && bestellteMenge > 0) { + if (bestellteMenge == dli.quantity && dli.openQuantity == 0) { + 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}`); + dli.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 { - - 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 }); - } + this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestelltemenge ${bestellteMenge} is not set`); } - } 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); } }, });