refactor: migrate credit note and vendor credit services to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-29 18:37:33 +02:00
parent 9f9b75cd31
commit caf235e2b5
107 changed files with 7396 additions and 109 deletions

View File

@@ -0,0 +1,67 @@
import { Service, Inject } from 'typedi';
import { sumBy } from 'lodash';
import events from '@/subscribers/events';
import {
IApplyCreditToInvoicesCreatedPayload,
IApplyCreditToInvoicesDeletedPayload,
} from '@/interfaces';
import CreditNoteApplySyncCredit from '../commands/CreditNoteApplySyncCredit.service';
@Service()
export default class CreditNoteApplySyncCreditSubscriber {
@Inject()
syncInvoicedAmountWithCredit: CreditNoteApplySyncCredit;
/**
*
* @param bus
*/
attach(bus) {
bus.subscribe(
events.creditNote.onApplyToInvoicesCreated,
this.incrementCreditedAmountOnceApplyToInvoicesCreated
);
bus.subscribe(
events.creditNote.onApplyToInvoicesDeleted,
this.decrementCreditedAmountOnceApplyToInvoicesDeleted
);
}
/**
* Increment credited amount of credit note transaction once the transaction created.
* @param {IApplyCreditToInvoicesCreatedPayload} payload -
*/
private incrementCreditedAmountOnceApplyToInvoicesCreated = async ({
trx,
creditNote,
tenantId,
creditNoteAppliedInvoices,
}: IApplyCreditToInvoicesCreatedPayload) => {
const totalCredited = sumBy(creditNoteAppliedInvoices, 'amount');
await this.syncInvoicedAmountWithCredit.incrementCreditNoteInvoicedAmount(
tenantId,
creditNote.id,
totalCredited,
trx
);
};
/**
* Decrement credited amount of credit note transaction once the transaction deleted.
* @param {IApplyCreditToInvoicesDeletedPayload} payload -
*/
private decrementCreditedAmountOnceApplyToInvoicesDeleted = async ({
tenantId,
creditNote,
creditNoteAppliedToInvoice,
trx,
}: IApplyCreditToInvoicesDeletedPayload) => {
await this.syncInvoicedAmountWithCredit.decrementCreditNoteInvoicedAmount(
tenantId,
creditNote.id,
creditNoteAppliedToInvoice.amount,
trx
);
};
}