diff --git a/index.ts b/index.ts index f3ee380..6c6723d 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'; import { DocumentStatusType } from '@phoenix/common'; -import { DocumentLineItem, DocumentLineItemStatusChangedEvent, EventBus, Logger, PhoenixPlugin, PluginCommonModule } from '@phoenix/core'; +import { DocumentLineItem, DocumentLineItemStatusChangedEvent, EntitySavedEvent, EventBus, Logger, PhoenixPlugin, PluginCommonModule } from '@phoenix/core'; // Example service for the plugin @Injectable() @@ -42,6 +42,7 @@ export class AmplidPlugin implements OnApplicationBootstrap { } setupEvents() { + //this might not be needed anymore, because we are using the EntitySavedEvent now this.eventBus.registerBlockingEventHandler(DocumentLineItemStatusChangedEvent, { id: 'amplid-plugin-document-line-item-status-changed', handler: async (event) => { @@ -71,5 +72,37 @@ export class AmplidPlugin implements OnApplicationBootstrap { } }, }); + + this.eventBus.registerBlockingEventHandler(EntitySavedEvent, { + id: 'amplid-plugin-post-entity-saved', + handler: async (event) => { + if (event.entity === DocumentLineItem.name) { + + 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 }); + } + + } 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 { + this.logger.info(`Keeping status as completed for DocumentLineItem ${dli.id} because bestelltemenge ${bestellteMenge} is not set`); + } + } + }, + }); } } \ No newline at end of file