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
);
};
}

View File

@@ -0,0 +1,61 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import {
IApplyCreditToInvoicesCreatedPayload,
IApplyCreditToInvoicesDeletedPayload,
} from '@/interfaces';
import CreditNoteApplySyncInvoicesCreditedAmount from '../commands/CreditNoteApplySyncInvoices.service';
@Service()
export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber {
@Inject()
private syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount;
/**
* Attaches events with handlers.
*/
public attach(bus) {
bus.subscribe(
events.creditNote.onApplyToInvoicesCreated,
this.incrementAppliedInvoicesOnceCreditCreated
);
bus.subscribe(
events.creditNote.onApplyToInvoicesDeleted,
this.decrementAppliedInvoicesOnceCreditDeleted
);
}
/**
* Increment invoices credited amount once the credit note apply to invoices transaction
* @param {IApplyCreditToInvoicesCreatedPayload} payload -
*/
private incrementAppliedInvoicesOnceCreditCreated = async ({
trx,
tenantId,
creditNoteAppliedInvoices,
}: IApplyCreditToInvoicesCreatedPayload) => {
await this.syncInvoicesWithCreditNote.incrementInvoicesCreditedAmount(
tenantId,
creditNoteAppliedInvoices,
trx
);
};
/**
*
* @param {IApplyCreditToInvoicesDeletedPayload} payload -
*/
private decrementAppliedInvoicesOnceCreditDeleted = async ({
trx,
creditNoteAppliedToInvoice,
tenantId,
}: IApplyCreditToInvoicesDeletedPayload) => {
// Decrement invoice credited amount.
await this.syncInvoicesWithCreditNote.decrementInvoiceCreditedAmount(
tenantId,
creditNoteAppliedToInvoice.invoiceId,
creditNoteAppliedToInvoice.amount,
trx
);
};
}

View File

@@ -0,0 +1,30 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import BaseCreditNotes from '../commands/CommandCreditNoteDTOTransform.service';
import { ICreditNoteCreatedPayload } from '@/interfaces';
@Service()
export default class CreditNoteAutoSerialSubscriber {
@Inject()
creditNotesService: BaseCreditNotes;
/**
* Attaches events with handlers.
*/
public attach(bus) {
bus.subscribe(
events.creditNote.onCreated,
this.autoSerialIncrementOnceCreated
);
}
/**
* Auto serial increment once credit note created.
* @param {ICreditNoteCreatedPayload} payload -
*/
private autoSerialIncrementOnceCreated = async ({
tenantId,
}: ICreditNoteCreatedPayload) => {
await this.creditNotesService.incrementSerialNumber(tenantId);
};
}

View File

@@ -0,0 +1,113 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import {
ICreditNoteCreatedPayload,
ICreditNoteDeletedPayload,
ICreditNoteEditedPayload,
ICreditNoteOpenedPayload,
} from '@/interfaces';
import CreditNoteGLEntries from '../commands/CreditNoteGLEntries';
@Service()
export default class CreditNoteGLEntriesSubscriber {
@Inject()
private creditNoteGLEntries: CreditNoteGLEntries;
/**
* Attaches events with handlers.
* @param bus
*/
public attach(bus) {
bus.subscribe(
events.creditNote.onCreated,
this.writeGlEntriesOnceCreditNoteCreated
);
bus.subscribe(
events.creditNote.onOpened,
this.writeGLEntriesOnceCreditNoteOpened
);
bus.subscribe(
events.creditNote.onEdited,
this.editVendorCreditGLEntriesOnceEdited
);
bus.subscribe(
events.creditNote.onDeleted,
this.revertGLEntriesOnceCreditNoteDeleted
);
}
/**
* Writes the GL entries once the credit note transaction created or open.
* @private
* @param {ICreditNoteCreatedPayload|ICreditNoteOpenedPayload} payload -
*/
private writeGlEntriesOnceCreditNoteCreated = async ({
tenantId,
creditNote,
creditNoteId,
trx,
}: ICreditNoteCreatedPayload | ICreditNoteOpenedPayload) => {
// Can't continue if the credit note is not published yet.
if (!creditNote.isPublished) return;
await this.creditNoteGLEntries.createVendorCreditGLEntries(
tenantId,
creditNoteId,
trx
);
};
/**
* Writes the GL entries once the vendor credit transaction opened.
* @param {ICreditNoteOpenedPayload} payload
*/
private writeGLEntriesOnceCreditNoteOpened = async ({
tenantId,
creditNoteId,
trx,
}: ICreditNoteOpenedPayload) => {
await this.creditNoteGLEntries.createVendorCreditGLEntries(
tenantId,
creditNoteId,
trx
);
};
/**
* Reverts GL entries once credit note deleted.
*/
private revertGLEntriesOnceCreditNoteDeleted = async ({
tenantId,
oldCreditNote,
creditNoteId,
trx,
}: ICreditNoteDeletedPayload) => {
// Can't continue if the credit note is not published yet.
if (!oldCreditNote.isPublished) return;
await this.creditNoteGLEntries.revertVendorCreditGLEntries(
tenantId,
creditNoteId
);
};
/**
* Edits vendor credit associated GL entries once the transaction edited.
* @param {ICreditNoteEditedPayload} payload -
*/
private editVendorCreditGLEntriesOnceEdited = async ({
tenantId,
creditNote,
creditNoteId,
trx,
}: ICreditNoteEditedPayload) => {
// Can't continue if the credit note is not published yet.
if (!creditNote.isPublished) return;
await this.creditNoteGLEntries.editVendorCreditGLEntries(
tenantId,
creditNoteId,
trx
);
};
}

View File

@@ -0,0 +1,98 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import CreditNoteInventoryTransactions from '../commands/CreditNotesInventoryTransactions';
import {
ICreditNoteCreatedPayload,
ICreditNoteDeletedPayload,
ICreditNoteEditedPayload,
} from '@/interfaces';
@Service()
export default class CreditNoteInventoryTransactionsSubscriber {
@Inject()
inventoryTransactions: CreditNoteInventoryTransactions;
/**
* Attaches events with publisher.
*/
public attach(bus) {
bus.subscribe(
events.creditNote.onCreated,
this.writeInventoryTranscationsOnceCreated
);
bus.subscribe(
events.creditNote.onEdited,
this.rewriteInventoryTransactionsOnceEdited
);
bus.subscribe(
events.creditNote.onDeleted,
this.revertInventoryTransactionsOnceDeleted
);
bus.subscribe(
events.creditNote.onOpened,
this.writeInventoryTranscationsOnceCreated
);
}
/**
* Writes inventory transactions once credit note created.
* @param {ICreditNoteCreatedPayload} payload -
* @returns {Promise<void>}
*/
public writeInventoryTranscationsOnceCreated = async ({
tenantId,
creditNote,
trx,
}: ICreditNoteCreatedPayload) => {
// Can't continue if the credit note is open yet.
if (!creditNote.isOpen) return;
await this.inventoryTransactions.createInventoryTransactions(
tenantId,
creditNote,
trx
);
};
/**
* Rewrites inventory transactions once credit note edited.
* @param {ICreditNoteEditedPayload} payload -
* @returns {Promise<void>}
*/
public rewriteInventoryTransactionsOnceEdited = async ({
tenantId,
creditNoteId,
creditNote,
trx,
}: ICreditNoteEditedPayload) => {
// Can't continue if the credit note is open yet.
if (!creditNote.isOpen) return;
await this.inventoryTransactions.editInventoryTransactions(
tenantId,
creditNoteId,
creditNote,
trx
);
};
/**
* Reverts inventory transactions once credit note deleted.
* @param {ICreditNoteDeletedPayload} payload -
*/
public revertInventoryTransactionsOnceDeleted = async ({
tenantId,
creditNoteId,
oldCreditNote,
trx,
}: ICreditNoteDeletedPayload) => {
// Can't continue if the credit note is open yet.
if (!oldCreditNote.isOpen) return;
await this.inventoryTransactions.deleteInventoryTransactions(
tenantId,
creditNoteId,
trx
);
};
}

View File

@@ -0,0 +1,48 @@
import { Inject, Service } from 'typedi';
import { ServiceError } from '@/exceptions';
import TenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import { ICustomerDeletingPayload } from '@/interfaces';
import DeleteCustomerLinkedCreidtNote from '../commands/DeleteCustomerLinkedCreditNote.service';
const ERRORS = {
CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS',
};
@Service()
export default class DeleteCustomerLinkedCreditSubscriber {
@Inject()
tenancy: TenancyService;
@Inject()
deleteCustomerLinkedCredit: DeleteCustomerLinkedCreidtNote;
/**
* Attaches events with handlers.
* @param bus
*/
public attach = (bus) => {
bus.subscribe(
events.customers.onDeleting,
this.validateCustomerHasNoLinkedCreditsOnDeleting
);
};
/**
* Validate vendor has no associated credit transaction once the vendor deleting.
* @param {IVendorEventDeletingPayload} payload -
*/
public validateCustomerHasNoLinkedCreditsOnDeleting = async ({
tenantId,
customerId,
}: ICustomerDeletingPayload) => {
try {
await this.deleteCustomerLinkedCredit.validateCustomerHasNoCreditTransaction(
tenantId,
customerId
);
} catch (error) {
throw new ServiceError(ERRORS.CUSTOMER_HAS_TRANSACTIONS);
}
};
}

View File

@@ -0,0 +1,61 @@
import { Service, Inject } from 'typedi';
import events from '@/subscribers/events';
import RefundCreditNoteGLEntries from '../commands/RefundCreditNoteGLEntries';
import {
IRefundCreditNoteCreatedPayload,
IRefundCreditNoteDeletedPayload,
} from '@/interfaces';
@Service()
export default class RefundCreditNoteGLEntriesSubscriber {
@Inject()
refundCreditGLEntries: RefundCreditNoteGLEntries;
/**
* Attaches events with handlers.
*/
public attach = (bus) => {
bus.subscribe(
events.creditNote.onRefundCreated,
this.writeRefundCreditGLEntriesOnceCreated
);
bus.subscribe(
events.creditNote.onRefundDeleted,
this.revertRefundCreditGLEntriesOnceDeleted
);
};
/**
* Writes refund credit note GL entries once the transaction created.
* @param {IRefundCreditNoteCreatedPayload} payload -
*/
private writeRefundCreditGLEntriesOnceCreated = async ({
trx,
refundCreditNote,
creditNote,
tenantId,
}: IRefundCreditNoteCreatedPayload) => {
await this.refundCreditGLEntries.createRefundCreditGLEntries(
tenantId,
refundCreditNote.id,
trx
);
};
/**
* Reverts refund credit note GL entries once the transaction deleted.
* @param {IRefundCreditNoteDeletedPayload} payload -
*/
private revertRefundCreditGLEntriesOnceDeleted = async ({
trx,
refundCreditId,
oldRefundCredit,
tenantId,
}: IRefundCreditNoteDeletedPayload) => {
await this.refundCreditGLEntries.revertRefundCreditGLEntries(
tenantId,
refundCreditId,
trx
);
};
}

View File

@@ -0,0 +1,62 @@
import { Inject, Service } from 'typedi';
import {
IRefundCreditNoteCreatedPayload,
IRefundCreditNoteDeletedPayload,
} from '@/interfaces';
import events from '@/subscribers/events';
import RefundSyncCreditNoteBalance from '../commands/RefundSyncCreditNoteBalance';
@Service()
export default class RefundSyncCreditNoteBalanceSubscriber {
@Inject()
refundSyncCreditBalance: RefundSyncCreditNoteBalance;
/**
* Attaches events with handlers.
*/
attach(bus) {
bus.subscribe(
events.creditNote.onRefundCreated,
this.incrementRefundedAmountOnceRefundCreated
);
bus.subscribe(
events.creditNote.onRefundDeleted,
this.decrementRefundedAmountOnceRefundDeleted
);
return bus;
}
/**
* Increment credit note refunded amount once associated refund transaction created.
* @param {IRefundCreditNoteCreatedPayload} payload -
*/
private incrementRefundedAmountOnceRefundCreated = async ({
trx,
refundCreditNote,
tenantId,
}: IRefundCreditNoteCreatedPayload) => {
await this.refundSyncCreditBalance.incrementCreditNoteRefundAmount(
tenantId,
refundCreditNote.creditNoteId,
refundCreditNote.amount,
trx
);
};
/**
* Decrement credit note refunded amount once associated refuned transaction deleted.
* @param {IRefundCreditNoteDeletedPayload} payload -
*/
private decrementRefundedAmountOnceRefundDeleted = async ({
trx,
oldRefundCredit,
tenantId,
}: IRefundCreditNoteDeletedPayload) => {
await this.refundSyncCreditBalance.decrementCreditNoteRefundAmount(
tenantId,
oldRefundCredit.creditNoteId,
oldRefundCredit.amount,
trx
);
};
}