refactor: migrate ledger subscribers to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-31 23:51:24 +02:00
parent a819d6c1ba
commit 3ad34ba56f
21 changed files with 779 additions and 611 deletions

View File

@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { PaymentReceivesController } from './PaymentsReceived.controller';
import { PaymentReceivesApplication } from './PaymentReceived.application';
import { CreatePaymentReceivedService } from './commands/CreatePaymentReceived.serivce';
@@ -8,7 +7,7 @@ import { EditPaymentReceivedService } from './commands/EditPaymentReceived.servi
import { GetPaymentReceivedStateService } from './queries/GetPaymentReceivedState.service';
import { GetPaymentReceivedService } from './queries/GetPaymentReceived.service';
import { GetPaymentReceivedInvoices } from './queries/GetPaymentReceivedInvoices.service';
import GetPaymentReceivedPdf from './queries/GetPaymentReceivedPdf.service';
import { GetPaymentReceivedPdfService } from './queries/GetPaymentReceivedPdf.service';
import { PaymentReceivedValidators } from './commands/PaymentReceivedValidators.service';
import { PaymentReceiveDTOTransformer } from './commands/PaymentReceivedDTOTransformer';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
@@ -20,6 +19,11 @@ import { BranchesModule } from '../Branches/Branches.module';
import { WarehousesModule } from '../Warehouses/Warehouses.module';
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
import { PaymentReceivedAutoIncrementSubscriber } from './subscribers/PaymentReceivedAutoIncrementSubscriber';
import { PaymentReceivedGLEntriesSubscriber } from './subscribers/PaymentReceivedGLEntriesSubscriber';
import { PaymentReceivedGLEntries } from './commands/PaymentReceivedGLEntries';
import { PaymentReceivedSyncInvoicesSubscriber } from './subscribers/PaymentReceivedSyncInvoices';
import { PaymentReceivedInvoiceSync } from './commands/PaymentReceivedInvoiceSync.service';
@Module({
controllers: [PaymentReceivesController],
@@ -31,12 +35,17 @@ import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementO
GetPaymentReceivedStateService,
GetPaymentReceivedService,
GetPaymentReceivedInvoices,
GetPaymentReceivedPdf,
GetPaymentReceivedPdfService,
PaymentReceivedValidators,
PaymentReceiveDTOTransformer,
PaymentReceivedBrandingTemplate,
PaymentReceivedIncrement,
PaymentReceivedGLEntries,
TenancyContext,
PaymentReceivedInvoiceSync,
PaymentReceivedAutoIncrementSubscriber,
PaymentReceivedGLEntriesSubscriber,
PaymentReceivedSyncInvoicesSubscriber,
],
exports: [PaymentReceivesApplication],
imports: [
@@ -45,7 +54,7 @@ import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementO
BranchesModule,
WarehousesModule,
PdfTemplatesModule,
AutoIncrementOrdersModule
AutoIncrementOrdersModule,
],
})
export class PaymentsReceivedModule {}

View File

@@ -33,7 +33,7 @@ export class CreatePaymentReceivedService {
/**
* Creates a new payment receive and store it to the storage
* with associated invoices payment and journal transactions.
* @param {IPaymentReceivedCreateDTO} paymentReceiveDTO
* @param {IPaymentReceivedCreateDTO} paymentReceiveDTO - Payment receive create DTO.
* @param {Knex.Transaction} trx - Database transaction.
*/
public async createPaymentReceived(

View File

@@ -18,16 +18,31 @@ export class PaymentReceivedGL {
this.paymentReceived = paymentReceived;
}
/**
* Sets the A/R account ID.
* @param {number} ARAccountId - A/R account ID.
* @returns {PaymentReceivedGL}
*/
setARAccountId(ARAccountId: number) {
this.ARAccountId = ARAccountId;
return this;
}
/**
* Sets the exchange gain/loss account ID.
* @param {number} exchangeGainOrLossAccountId - Exchange gain/loss account ID.
* @returns {PaymentReceivedGL}
*/
setExchangeGainOrLossAccountId(exchangeGainOrLossAccountId: number) {
this.exchangeGainOrLossAccountId = exchangeGainOrLossAccountId;
return this;
}
/**
* Sets the base currency code.
* @param {string} baseCurrencyCode - Base currency code.
* @returns {PaymentReceivedGL}
*/
setBaseCurrencyCode(baseCurrencyCode: string) {
this.baseCurrencyCode = baseCurrencyCode;
return this;
@@ -177,5 +192,5 @@ export class PaymentReceivedGL {
*/
public getLedger = (): Ledger => {
return new Ledger(this.GLEntries());
};
};
}

View File

@@ -12,7 +12,7 @@ import { PaymentReceivedPdfTemplateAttributes } from '../types/PaymentReceived.t
import { events } from '@/common/events/events';
@Injectable()
export default class GetPaymentReceivedPdf {
export class GetPaymentReceivedPdfService {
constructor(
private chromiumlyTenancy: ChromiumlyTenancy,
private templateInjectable: TemplateInjectable,

View File

@@ -0,0 +1,19 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
import { PaymentReceivedIncrement } from '../commands/PaymentReceivedIncrement.service';
import { IPaymentReceivedCreatedPayload } from '../types/PaymentReceived.types';
@Injectable()
export class PaymentReceivedAutoIncrementSubscriber {
constructor(private readonly paymentIncrement: PaymentReceivedIncrement) {}
/**
* Handles increment next number of payment receive once be created.
* @param {IPaymentReceivedCreatedPayload} payload -
*/
@OnEvent(events.paymentReceive.onCreated)
private async handlePaymentNextNumberIncrement({}: IPaymentReceivedCreatedPayload) {
await this.paymentIncrement.incrementNextPaymentReceiveNumber();
}
}

View File

@@ -0,0 +1,61 @@
import { Injectable } from '@nestjs/common';
import {
IPaymentReceivedCreatedPayload,
IPaymentReceivedDeletedPayload,
IPaymentReceivedEditedPayload,
} from '../types/PaymentReceived.types';
import { PaymentReceivedGLEntries } from '../commands/PaymentReceivedGLEntries';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
@Injectable()
export class PaymentReceivedGLEntriesSubscriber {
/**
* @param {PaymentReceivedGLEntries} paymentReceivedGLEntries -
*/
constructor(
private readonly paymentReceivedGLEntries: PaymentReceivedGLEntries,
) {}
/**
* Handle journal entries writing once the payment receive created.
*/
@OnEvent(events.paymentReceive.onCreated)
private async handleWriteJournalEntriesOnceCreated({
paymentReceiveId,
trx,
}: IPaymentReceivedCreatedPayload) {
await this.paymentReceivedGLEntries.writePaymentGLEntries(
paymentReceiveId,
trx,
);
}
/**
* Handle journal entries writing once the payment receive edited.
*/
@OnEvent(events.paymentReceive.onEdited)
private async handleOverwriteJournalEntriesOnceEdited({
paymentReceive,
trx,
}: IPaymentReceivedEditedPayload) {
await this.paymentReceivedGLEntries.rewritePaymentGLEntries(
paymentReceive.id,
trx,
);
}
/**
* Handles revert journal entries once deleted.
*/
@OnEvent(events.paymentReceive.onDeleted)
private async handleRevertJournalEntriesOnceDeleted({
paymentReceiveId,
trx,
}: IPaymentReceivedDeletedPayload) {
await this.paymentReceivedGLEntries.revertPaymentGLEntries(
paymentReceiveId,
trx,
);
}
}

View File

@@ -0,0 +1,40 @@
// import { Service, Inject } from 'typedi';
// import events from '@/subscribers/events';
// import { PaymentReceiveNotifyBySms } from '@/services/Sales/PaymentReceived/PaymentReceivedSmsNotify';
// import { IPaymentReceivedCreatedPayload } from '@/interfaces';
// import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
// @Service()
// export default class SendSmsNotificationPaymentReceive {
// @Inject()
// private paymentReceiveSmsNotify: PaymentReceiveNotifyBySms;
// /**
// * Attach events.
// */
// public attach(bus) {
// bus.subscribe(
// events.paymentReceive.onCreated,
// this.handleNotifyViaSmsOncePaymentPublish
// );
// }
// /**
// * Handles send SMS notification after payment transaction creation.
// */
// private handleNotifyViaSmsOncePaymentPublish = ({
// tenantId,
// paymentReceiveId,
// trx,
// }: IPaymentReceivedCreatedPayload) => {
// // Notify via Sms after transactions complete running.
// runAfterTransaction(trx, async () => {
// try {
// await this.paymentReceiveSmsNotify.notifyViaSmsNotificationAfterCreation(
// tenantId,
// paymentReceiveId
// );
// } catch (error) { }
// });
// };
// }

View File

@@ -0,0 +1,71 @@
import {
IPaymentReceivedCreatedPayload,
IPaymentReceivedDeletedPayload,
IPaymentReceivedEditedPayload,
} from '../types/PaymentReceived.types';
import { PaymentReceivedInvoiceSync } from '../commands/PaymentReceivedInvoiceSync.service';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
@Injectable()
export class PaymentReceivedSyncInvoicesSubscriber {
/**
* @param {PaymentReceivedInvoiceSync} paymentSyncInvoice -
*/
constructor(
private readonly paymentSyncInvoice: PaymentReceivedInvoiceSync,
) {}
/**
* Handle sale invoice increment/decrement payment amount
* once created, edited or deleted.
*/
@OnEvent(events.paymentReceive.onCreated)
private async handleInvoiceIncrementPaymentOnceCreated({
paymentReceive,
trx,
}: IPaymentReceivedCreatedPayload) {
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
paymentReceive.entries,
null,
trx
);
}
/**
* Handle sale invoice increment/decrement payment amount once edited.
*/
@OnEvent(events.paymentReceive.onEdited)
private async handleInvoiceIncrementPaymentOnceEdited({
paymentReceive,
oldPaymentReceive,
trx,
}: IPaymentReceivedEditedPayload) {
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
paymentReceive.entries,
oldPaymentReceive?.entries || null,
trx
);
}
/**
* Handle revert invoices payment amount once payment receive deleted.
*/
@OnEvent(events.paymentReceive.onDeleted)
private async handleInvoiceDecrementPaymentAmount({
paymentReceiveId,
oldPaymentReceive,
trx,
}: IPaymentReceivedDeletedPayload) {
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
oldPaymentReceive.entries.map((entry) => ({
...entry,
paymentAmount: 0,
})),
oldPaymentReceive.entries,
trx
);
};
}