From 95bb4fc8e3bf256c06f7992ac9d418540695ad55 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Tue, 18 Feb 2025 19:26:58 +0200 Subject: [PATCH] refactor: nestjs --- .../server-nest/src/modules/App/App.module.ts | 6 +- .../CreateUncategorizedTransaction.service.ts | 2 +- .../common/FinancialFilter.ts | 1 + .../common/FinancialTablePreviousPeriod.ts | 1 + .../common/FinancialTablePreviousYear.ts | 1 + .../APAgingSummaryRepository.ts | 13 ++-- .../ARAgingSummaryRepository.ts | 13 ++-- .../BalanceSheet/BalanceSheetRepository.ts | 53 +++++++++------- .../modules/CashFlowStatement/CashFlow.ts | 1 + .../CashFlowStatement/CashFlowDatePeriods.ts | 1 + .../CashFlowStatement/CashFlowRepository.ts | 5 +- .../CashFlowStatement/CashFlowTable.ts | 1 + .../CashflowStatementBase.ts | 1 + .../GeneralLedger/GeneralLedgerRepository.ts | 13 ++-- .../InventoryItemDetailsRepository.ts | 16 ++--- .../InventoryValuationSheetRepository.ts | 17 +++-- .../JournalSheet/JournalSheetRepository.ts | 9 +-- .../ProfitLossSheet/ProfitLossSheetBase.ts | 1 + .../ProfitLossSheet/ProfitLossSheetFilter.ts | 1 + .../ProfitLossSheet/ProfitLossSheetQuery.ts | 1 + .../ProfitLossSheetRepository.ts | 62 +++++++++++-------- .../VendorBalanceSummaryRepository.ts | 15 +++-- .../commands/DeleteItemCategory.service.ts | 1 - .../PaymentsReceived.module.ts | 11 +++- .../PaymentReceivedMailNotification.ts | 43 ++++++++++--- .../src/modules/PaymentReceived/constants.ts | 4 ++ ...ymentReceivedMailNotification.processor.ts | 43 +++++++++++++ .../types/PaymentReceived.types.ts | 13 +++- .../commands/SaleReceiptMailNotification.ts | 4 +- .../processes/SendSaleReceiptMail.process.ts | 10 ++- 30 files changed, 249 insertions(+), 114 deletions(-) create mode 100644 packages/server-nest/src/modules/PaymentReceived/processors/PaymentReceivedMailNotification.processor.ts diff --git a/packages/server-nest/src/modules/App/App.module.ts b/packages/server-nest/src/modules/App/App.module.ts index 48eb37bd1..000680117 100644 --- a/packages/server-nest/src/modules/App/App.module.ts +++ b/packages/server-nest/src/modules/App/App.module.ts @@ -1,6 +1,7 @@ import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { EventEmitterModule } from '@nestjs/event-emitter'; +import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core'; import { join } from 'path'; import { AcceptLanguageResolver, @@ -25,7 +26,6 @@ import { TenancyDatabaseModule } from '../Tenancy/TenancyDB/TenancyDB.module'; import { TenancyModelsModule } from '../Tenancy/TenancyModels/Tenancy.module'; import { LoggerMiddleware } from '@/middleware/logger.middleware'; import { ExcludeNullInterceptor } from '@/interceptors/ExcludeNull.interceptor'; -import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core'; import { JwtAuthGuard } from '../Auth/Jwt.guard'; import { UserIpInterceptor } from '@/interceptors/user-ip.interceptor'; import { TenancyGlobalMiddleware } from '../Tenancy/TenancyGlobal.middleware'; @@ -154,13 +154,13 @@ import { StripePaymentModule } from '../StripePayment/StripePayment.module'; BankingTransactionsModule, BankingTransactionsExcludeModule, BankingTransactionsRegonizeModule, - // BankingMatchingModule, + BankingMatchingModule, // TransactionsLockingModule, // SettingsModule, InventoryAdjustmentsModule, PostHogModule, EventTrackerModule, - // FinancialStatementsModule, + FinancialStatementsModule, StripePaymentModule, ], controllers: [AppController], diff --git a/packages/server-nest/src/modules/BankingCategorize/commands/CreateUncategorizedTransaction.service.ts b/packages/server-nest/src/modules/BankingCategorize/commands/CreateUncategorizedTransaction.service.ts index 2b69b60b2..e4fc61156 100644 --- a/packages/server-nest/src/modules/BankingCategorize/commands/CreateUncategorizedTransaction.service.ts +++ b/packages/server-nest/src/modules/BankingCategorize/commands/CreateUncategorizedTransaction.service.ts @@ -42,7 +42,7 @@ export class CreateUncategorizedTransactionService { } as IUncategorizedTransactionCreatingEventPayload, ); - const uncategorizedTransaction = await this.uncategorizedBankTransaction + const uncategorizedTransaction = await this.uncategorizedBankTransaction() .query(trx) .insertAndFetch({ ...createUncategorizedTransactionDTO, diff --git a/packages/server-nest/src/modules/FinancialStatements/common/FinancialFilter.ts b/packages/server-nest/src/modules/FinancialStatements/common/FinancialFilter.ts index f857acc04..fa21339e1 100644 --- a/packages/server-nest/src/modules/FinancialStatements/common/FinancialFilter.ts +++ b/packages/server-nest/src/modules/FinancialStatements/common/FinancialFilter.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import { GConstructor } from '@/common/types/Constructor'; import { isEmpty } from 'lodash'; import { FinancialSheet } from './FinancialSheet'; diff --git a/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousPeriod.ts b/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousPeriod.ts index 9fb653c17..65ed2e796 100644 --- a/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousPeriod.ts +++ b/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousPeriod.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as moment from 'moment'; import { ITableColumn, ITableColumnAccessor } from '../types/Table.types'; import { IDateRange } from '../types/Report.types'; diff --git a/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousYear.ts b/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousYear.ts index 989ab1e91..34e174101 100644 --- a/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousYear.ts +++ b/packages/server-nest/src/modules/FinancialStatements/common/FinancialTablePreviousYear.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as moment from 'moment'; import { ITableColumn, ITableColumnAccessor } from '../types/Table.types'; import { IDateRange } from '../types/Report.types'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/APAgingSummary/APAgingSummaryRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/APAgingSummary/APAgingSummaryRepository.ts index 2f64795b3..77c0d3b8a 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/APAgingSummary/APAgingSummaryRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/APAgingSummary/APAgingSummaryRepository.ts @@ -5,13 +5,14 @@ import { Vendor } from '@/modules/Vendors/models/Vendor'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; import { IAPAgingSummaryQuery } from './APAgingSummary.types'; import { ModelObject } from 'objection'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; export class APAgingSummaryRepository { @Inject(Vendor.name) - private readonly vendorModel: typeof Vendor; + private readonly vendorModel: TenantModelProxy; @Inject(Bill.name) - private readonly billModel: typeof Bill; + private readonly billModel: TenantModelProxy; @Inject(TenancyContext) private readonly tenancyContext: TenancyContext; @@ -93,8 +94,8 @@ export class APAgingSummaryRepository { // Retrieve all vendors from the storage. const vendors = this.filter.vendorsIds.length > 0 - ? await this.vendorModel.query().whereIn('id', this.filter.vendorsIds) - : await this.vendorModel.query(); + ? await this.vendorModel().query().whereIn('id', this.filter.vendorsIds) + : await this.vendorModel().query(); this.vendors = vendors; } @@ -108,7 +109,7 @@ export class APAgingSummaryRepository { query.modify('filterByBranches', this.filter.branchesIds); } }; - const overdueBills = await this.billModel + const overdueBills = await this.billModel() .query() .modify('overdueBillsFromDate', this.filter.asDate) .onBuild(commonQuery); @@ -127,7 +128,7 @@ export class APAgingSummaryRepository { } }; // Retrieve all due vendors bills. - const dueBills = await this.billModel + const dueBills = await this.billModel() .query() .modify('dueBillsFromDate', this.filter.asDate) .onBuild(commonQuery); diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/ARAgingSummary/ARAgingSummaryRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/ARAgingSummary/ARAgingSummaryRepository.ts index f56ae6fbb..488e0bb9f 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/ARAgingSummary/ARAgingSummaryRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/ARAgingSummary/ARAgingSummaryRepository.ts @@ -5,16 +5,17 @@ import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; import { ModelObject } from 'objection'; import { IARAgingSummaryQuery } from './ARAgingSummary.types'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; export class ARAgingSummaryRepository { @Inject(TenancyContext) private tenancyContext: TenancyContext; @Inject(Customer.name) - private customerModel: typeof Customer; + private customerModel: TenantModelProxy; @Inject(SaleInvoice.name) - private saleInvoiceModel: typeof SaleInvoice; + private saleInvoiceModel: TenantModelProxy; /** * Filter. @@ -92,10 +93,10 @@ export class ARAgingSummaryRepository { // Retrieve all customers from the storage. const customers = this.filter.customersIds.length > 0 - ? await this.customerModel + ? await this.customerModel() .query() .whereIn('id', this.filter.customersIds) - : await this.customerModel.query(); + : await this.customerModel().query(); this.customers = customers; } @@ -110,7 +111,7 @@ export class ARAgingSummaryRepository { } }; // Retrieve all overdue sale invoices. - const overdueSaleInvoices = await this.saleInvoiceModel + const overdueSaleInvoices = await this.saleInvoiceModel() .query() .modify('overdueInvoicesFromDate', this.filter.asDate) .onBuild(commonQuery); @@ -132,7 +133,7 @@ export class ARAgingSummaryRepository { } }; // Retrieve all due sale invoices. - const currentInvoices = await this.saleInvoiceModel + const currentInvoices = await this.saleInvoiceModel() .query() .modify('dueInvoicesFromDate', this.filter.asDate) .onBuild(commonQuery); diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/BalanceSheet/BalanceSheetRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/BalanceSheet/BalanceSheetRepository.ts index 346e0da9e..9a0f1854e 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/BalanceSheet/BalanceSheetRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/BalanceSheet/BalanceSheetRepository.ts @@ -14,6 +14,7 @@ import { Ledger } from '@/modules/Ledger/Ledger'; import { transformToMapBy } from '@/utils/transform-to-map-by'; import { Account } from '@/modules/Accounts/models/Account.model'; import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction.model'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class BalanceSheetRepository extends R.compose( @@ -24,13 +25,15 @@ export class BalanceSheetRepository extends R.compose( * Account model. */ @Inject(Account.name) - public readonly accountModel: typeof Account; + public readonly accountModel: TenantModelProxy; /** * Account transaction model. */ @Inject(AccountTransaction.name) - public readonly accountTransactionModel: typeof AccountTransaction; + public readonly accountTransactionModel: TenantModelProxy< + typeof AccountTransaction + >; /** * @description Balance sheet query. @@ -216,7 +219,7 @@ export class BalanceSheetRepository extends R.compose( * Initialize accounts graph. */ public initAccountsGraph = async () => { - this.accountsGraph = this.accountModel.toDependencyGraph(this.accounts); + this.accountsGraph = this.accountModel().toDependencyGraph(this.accounts); }; // ---------------------------- @@ -338,7 +341,7 @@ export class BalanceSheetRepository extends R.compose( * @return {Promise} */ public getAccounts = () => { - return this.accountModel.query(); + return this.accountModel().query(); }; /** @@ -353,18 +356,20 @@ export class BalanceSheetRepository extends R.compose( toDate: Date, datePeriodsType: string, ) => { - return this.accountTransactionModel.query().onBuild((query) => { - query.sum('credit as credit'); - query.sum('debit as debit'); - query.groupBy('accountId'); - query.select(['accountId']); + return this.accountTransactionModel() + .query() + .onBuild((query) => { + query.sum('credit as credit'); + query.sum('debit as debit'); + query.groupBy('accountId'); + query.select(['accountId']); - query.modify('groupByDateFormat', datePeriodsType); - query.modify('filterDateRange', fromDate, toDate); - query.withGraphFetched('account'); + query.modify('groupByDateFormat', datePeriodsType); + query.modify('filterDateRange', fromDate, toDate); + query.withGraphFetched('account'); - this.commonFilterBranchesQuery(query); - }); + this.commonFilterBranchesQuery(query); + }); }; /** @@ -372,17 +377,19 @@ export class BalanceSheetRepository extends R.compose( * @param {Date|string} openingDate - */ public closingAccountsTotal = async (openingDate: Date | string) => { - return this.accountTransactionModel.query().onBuild((query) => { - query.sum('credit as credit'); - query.sum('debit as debit'); - query.groupBy('accountId'); - query.select(['accountId']); + return this.accountTransactionModel() + .query() + .onBuild((query) => { + query.sum('credit as credit'); + query.sum('debit as debit'); + query.groupBy('accountId'); + query.select(['accountId']); - query.modify('filterDateRange', null, openingDate); - query.withGraphFetched('account'); + query.modify('filterDateRange', null, openingDate); + query.withGraphFetched('account'); - this.commonFilterBranchesQuery(query); - }); + this.commonFilterBranchesQuery(query); + }); }; /** diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlow.ts b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlow.ts index fe7b3bec6..336e6650e 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlow.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlow.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { defaultTo, set, sumBy, isEmpty, mapValues, get } from 'lodash'; import * as mathjs from 'mathjs'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowDatePeriods.ts b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowDatePeriods.ts index 2b5524d80..e7c6f8a8a 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowDatePeriods.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowDatePeriods.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { sumBy, mapValues, get } from 'lodash'; import { ACCOUNT_ROOT_TYPE } from '@/constants/accounts'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowRepository.ts index aa012ad89..bb99b7651 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowRepository.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import { Inject, Injectable } from '@nestjs/common'; import * as moment from 'moment'; import { Knex } from 'knex'; @@ -11,8 +12,8 @@ import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable() export class CashFlowRepository { /** - * @param {typeof Account} accountModel - Account model. - * @param {typeof AccountTransaction} accountTransactionModel - Account transaction model. + * @param {TenantModelProxy} accountModel - Account model. + * @param {TenantModelProxy} accountTransactionModel - Account transaction model. */ constructor( @Inject(Account.name) diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowTable.ts b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowTable.ts index 20a5465b9..180ca7a26 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowTable.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashFlowTable.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { isEmpty } from 'lodash'; import moment from 'moment'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashflowStatementBase.ts b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashflowStatementBase.ts index 9f2fbc8e3..d10873b6d 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashflowStatementBase.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/CashFlowStatement/CashflowStatementBase.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { map } from 'lodash'; import { Account } from "@/modules/Accounts/models/Account.model"; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/GeneralLedger/GeneralLedgerRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/GeneralLedger/GeneralLedgerRepository.ts index cd6c03456..fc51284c2 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/GeneralLedger/GeneralLedgerRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/GeneralLedger/GeneralLedgerRepository.ts @@ -12,6 +12,7 @@ import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; import { transformToMap } from '@/utils/transform-to-key'; import { Ledger } from '@/modules/Ledger/Ledger'; import { TenantModel } from '@/modules/System/models/TenantModel'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class GeneralLedgerRepository { @@ -41,10 +42,12 @@ export class GeneralLedgerRepository { private readonly accountRepository: AccountRepository; @Inject(AccountTransaction.name) - private readonly accountTransactionModel: typeof AccountTransaction; + private readonly accountTransactionModel: TenantModelProxy< + typeof AccountTransaction + >; @Inject(Contact.name) - private readonly contactModel: typeof Contact; + private readonly contactModel: TenantModelProxy; @Inject(TenancyContext) private readonly tenancyContext: TenancyContext; @@ -97,7 +100,7 @@ export class GeneralLedgerRepository { * Initialize the contacts. */ public async initContacts() { - this.contacts = await this.contactModel.query(); + this.contacts = await this.contactModel().query(); this.contactsById = transformToMap(this.contacts, 'id'); } @@ -105,7 +108,7 @@ export class GeneralLedgerRepository { * Initialize the G/L transactions from/to the given date. */ public async initTransactions() { - this.transactions = await this.accountTransactionModel + this.transactions = await this.accountTransactionModel() .query() .onBuild((query) => { query.modify( @@ -132,7 +135,7 @@ export class GeneralLedgerRepository { */ public async initAccountsOpeningBalance() { // Retrieves opening balance credit/debit sumation. - this.openingBalanceTransactions = await this.accountTransactionModel + this.openingBalanceTransactions = await this.accountTransactionModel() .query() .onBuild((query) => { const toDate = moment(this.filter.fromDate).subtract(1, 'day'); diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/InventoryItemDetails/InventoryItemDetailsRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/InventoryItemDetails/InventoryItemDetailsRepository.ts index f6fb7a274..fbad1077c 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/InventoryItemDetails/InventoryItemDetailsRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/InventoryItemDetails/InventoryItemDetailsRepository.ts @@ -8,14 +8,17 @@ import { Inject, Injectable, Scope } from '@nestjs/common'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; import { transformToMapKeyValue } from '@/utils/transform-to-map-key-value'; import { transformToMapBy } from '@/utils/transform-to-map-by'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class InventoryItemDetailsRepository { @Inject(Item.name) - readonly itemModel: typeof Item; + readonly itemModel: TenantModelProxy; @Inject(InventoryTransaction.name) - readonly inventoryTransactionModel: typeof InventoryTransaction; + readonly inventoryTransactionModel: TenantModelProxy< + typeof InventoryTransaction + >; @Inject(TenancyContext) readonly tenancyContext: TenancyContext; @@ -141,7 +144,7 @@ export class InventoryItemDetailsRepository { public async getInventoryItems( itemsIds?: number[], ): Promise[]> { - return this.itemModel.query().onBuild((q) => { + return this.itemModel().query().onBuild((q) => { q.where('type', 'inventory'); if (!isEmpty(itemsIds)) { @@ -163,7 +166,7 @@ export class InventoryItemDetailsRepository { .toDate(); // Opening inventory transactions. - const openingTransactions = this.inventoryTransactionModel + const openingTransactions = this.inventoryTransactionModel() .query() .select('*') .select(raw("IF(`DIRECTION` = 'IN', `QUANTITY`, 0) as 'QUANTITY_IN'")) @@ -188,8 +191,7 @@ export class InventoryItemDetailsRepository { if (!isEmpty(filter.branchesIds)) { openingTransactions.modify('filterByBranches', filter.branchesIds); } - - const openingBalanceTransactions = await this.inventoryTransactionModel + const openingBalanceTransactions = await this.inventoryTransactionModel() .query() .from(openingTransactions) .select('itemId') @@ -214,7 +216,7 @@ export class InventoryItemDetailsRepository { public async getItemInventoryTransactions( filter: IInventoryDetailsQuery, ): Promise[]> { - const inventoryTransactions = this.inventoryTransactionModel + const inventoryTransactions = this.inventoryTransactionModel() .query() .modify('filterDateRange', filter.fromDate, filter.toDate) .orderBy('date', 'ASC') diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/InventoryValuationSheet/InventoryValuationSheetRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/InventoryValuationSheet/InventoryValuationSheetRepository.ts index 0b95dec27..88e8793b0 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/InventoryValuationSheet/InventoryValuationSheetRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/InventoryValuationSheet/InventoryValuationSheetRepository.ts @@ -6,6 +6,7 @@ import { ModelObject } from 'objection'; import { Item } from '@/modules/Items/models/Item'; import { transformToMap } from '@/utils/transform-to-key'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class InventoryValuationSheetRepository { @@ -13,10 +14,12 @@ export class InventoryValuationSheetRepository { private readonly tenancyContext: TenancyContext; @Inject(InventoryCostLotTracker.name) - private readonly inventoryCostLotTracker: typeof InventoryCostLotTracker; + private readonly inventoryCostLotTracker: TenantModelProxy< + typeof InventoryCostLotTracker + >; @Inject(Item.name) - private readonly itemModel: typeof Item; + private readonly itemModel: TenantModelProxy; /** * The filter. @@ -89,8 +92,10 @@ export class InventoryValuationSheetRepository { * Retrieve the inventory items. */ async asyncItems() { - const inventoryItems = await this.itemModel.query().onBuild((q) => { - q.where('type', 'inventory'); + const inventoryItems = await this.itemModel() + .query() + .onBuild((q) => { + q.where('type', 'inventory'); if (this.filter.itemsIds.length > 0) { q.whereIn('id', this.filter.itemsIds); @@ -121,13 +126,13 @@ export class InventoryValuationSheetRepository { } }; // Retrieve the inventory cost `IN` transactions. - const INTransactions = await this.inventoryCostLotTracker + const INTransactions = await this.inventoryCostLotTracker() .query() .onBuild(commonQuery) .where('direction', 'IN'); // Retrieve the inventory cost `OUT` transactions. - const OUTTransactions = await this.inventoryCostLotTracker + const OUTTransactions = await this.inventoryCostLotTracker() .query() .onBuild(commonQuery) .where('direction', 'OUT'); diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/JournalSheet/JournalSheetRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/JournalSheet/JournalSheetRepository.ts index 369ac366d..3e3e05bcd 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/JournalSheet/JournalSheetRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/JournalSheet/JournalSheetRepository.ts @@ -2,6 +2,7 @@ import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository'; import { Contact } from '@/modules/Contacts/models/Contact'; import { Ledger } from '@/modules/Ledger/Ledger'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; import { transformToMap } from '@/utils/transform-to-key'; import { Inject } from '@nestjs/common'; @@ -15,10 +16,10 @@ export class JournalSheetRepository { private accountRepository: AccountRepository; @Inject(Contact.name) - private contactModel: typeof Contact; + private contactModel: TenantModelProxy; @Inject(AccountTransaction.name) - private accountTransaction: typeof AccountTransaction; + private accountTransaction: TenantModelProxy; @Inject(AccountTransaction.name) private accountTransactions: Array>; @@ -88,7 +89,7 @@ export class JournalSheetRepository { */ async initAccountTransactions() { // Retrieve all journal transactions based on the given query. - const transactions = await this.accountTransaction + const transactions = await this.accountTransaction() .query() .onBuild((query) => { if (this.filter.fromRange || this.filter.toRange) { @@ -119,7 +120,7 @@ export class JournalSheetRepository { * Initialize contacts. */ async initContacts() { - const contacts = await this.contactModel.query(); + const contacts = await this.contactModel().query(); this.contacts = contacts; this.contactsById = transformToMap(contacts, 'id'); diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetBase.ts b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetBase.ts index 534f71717..63763b5e1 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetBase.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetBase.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { TOTAL_NODE_TYPES } from './constants'; import { FinancialSheet } from '../../common/FinancialSheet'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetFilter.ts b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetFilter.ts index eeead8058..c11d8b853 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetFilter.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetFilter.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import * as R from 'ramda'; import { get } from 'lodash'; import { ProfitLossSheetBase } from './ProfitLossSheetBase'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetQuery.ts b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetQuery.ts index 0a94e2798..742fd52a7 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetQuery.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetQuery.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import { merge } from 'lodash'; import * as R from 'ramda'; import { IProfitLossSheetQuery } from './ProfitLossSheet.types'; diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetRepository.ts index bb4efd3b0..33c4902f8 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/ProfitLossSheet/ProfitLossSheetRepository.ts @@ -1,9 +1,11 @@ +// @ts-nocheck import { Inject, Injectable, Scope } from '@nestjs/common'; import { ModelObject } from 'objection'; import { castArray } from 'lodash'; import * as R from 'ramda'; import { Knex } from 'knex'; import { isEmpty } from 'lodash'; +import * as moment from 'moment'; import { transformToMapBy } from '@/utils/transform-to-map-by'; import { ProfitLossSheetQuery } from './ProfitLossSheetQuery'; import { Ledger } from '@/modules/Ledger/Ledger'; @@ -13,16 +15,17 @@ import { Account } from '@/modules/Accounts/models/Account.model'; import { FinancialDatePeriods } from '../../common/FinancialDatePeriods'; import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction.model'; import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( class {}, ) { @Inject(Account.name) - public accountModel: typeof Account; + public accountModel: TenantModelProxy; @Inject(AccountTransaction.name) - public accountTransactionModel: typeof AccountTransaction; + public accountTransactionModel: TenantModelProxy; @Inject(TenancyContext) public tenancyContext: TenancyContext; @@ -206,7 +209,7 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( * Initialize accounts graph. */ private initAccountsGraph = async () => { - this.accountsGraph = this.accountModel.toDependencyGraph(this.accounts); + this.accountsGraph = this.accountModel().toDependencyGraph(this.accounts); }; // ---------------------------- @@ -217,8 +220,8 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( */ private initAccountsTotalLedger = async (): Promise => { const totalByAccount = await this.accountsTotal( - this.query.fromDate, - this.query.toDate, + this.query.query.fromDate, + this.query.query.toDate, ); // Inject to the repository. this.totalAccountsLedger = Ledger.fromTransactions(totalByAccount); @@ -306,18 +309,23 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( /** * Retrieve the opening balance transactions of the report. */ - public accountsTotal = async (fromDate: Date, toDate: Date) => { - return this.accountTransactionModel.query().onBuild((query) => { - query.sum('credit as credit'); - query.sum('debit as debit'); - query.groupBy('accountId'); - query.select(['accountId']); + public accountsTotal = async ( + fromDate: moment.MomentInput, + toDate: moment.MomentInput, + ) => { + return this.accountTransactionModel() + .query() + .onBuild((query) => { + query.sum('credit as credit'); + query.sum('debit as debit'); + query.groupBy('accountId'); + query.select(['accountId']); - query.modify('filterDateRange', fromDate, toDate); - query.withGraphFetched('account'); + query.modify('filterDateRange', fromDate, toDate); + query.withGraphFetched('account'); - this.commonFilterBranchesQuery(query); - }); + this.commonFilterBranchesQuery(query); + }); }; /** @@ -331,18 +339,20 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( toDate: moment.MomentInput, datePeriodsType, ) => { - return this.accountTransactionModel.query().onBuild((query) => { - query.sum('credit as credit'); - query.sum('debit as debit'); - query.groupBy('accountId'); - query.select(['accountId']); + return this.accountTransactionModel() + .query() + .onBuild((query) => { + query.sum('credit as credit'); + query.sum('debit as debit'); + query.groupBy('accountId'); + query.select(['accountId']); - query.modify('groupByDateFormat', datePeriodsType); - query.modify('filterDateRange', fromDate, toDate); - query.withGraphFetched('account'); + query.modify('groupByDateFormat', datePeriodsType); + query.modify('filterDateRange', fromDate, toDate); + query.withGraphFetched('account'); - this.commonFilterBranchesQuery(query); - }); + this.commonFilterBranchesQuery(query); + }); }; /** @@ -360,7 +370,7 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)( * @return {Promise} */ private getAccounts = () => { - return this.accountModel.query(); + return this.accountModel().query(); }; /** diff --git a/packages/server-nest/src/modules/FinancialStatements/modules/VendorBalanceSummary/VendorBalanceSummaryRepository.ts b/packages/server-nest/src/modules/FinancialStatements/modules/VendorBalanceSummary/VendorBalanceSummaryRepository.ts index 5b9140191..1b544cac2 100644 --- a/packages/server-nest/src/modules/FinancialStatements/modules/VendorBalanceSummary/VendorBalanceSummaryRepository.ts +++ b/packages/server-nest/src/modules/FinancialStatements/modules/VendorBalanceSummary/VendorBalanceSummaryRepository.ts @@ -9,17 +9,20 @@ import { Account } from '@/modules/Accounts/models/Account.model'; import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types'; import { ModelObject } from 'objection'; import { ACCOUNT_TYPE } from '@/constants/accounts'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable({ scope: Scope.TRANSIENT }) export class VendorBalanceSummaryRepository { @Inject(AccountTransaction.name) - private readonly accountTransactionModel: typeof AccountTransaction; + private readonly accountTransactionModel: TenantModelProxy< + typeof AccountTransaction + >; @Inject(Vendor.name) - private readonly vendorModel: typeof Vendor; + private readonly vendorModel: TenantModelProxy; @Inject(Account.name) - private readonly accountModel: typeof Account; + private readonly accountModel: TenantModelProxy; /** * Filter. @@ -99,7 +102,7 @@ export class VendorBalanceSummaryRepository { public async getVendors( vendorsIds?: number[], ): Promise[]> { - const vendorQuery = this.vendorModel.query().orderBy('displayName'); + const vendorQuery = this.vendorModel().query().orderBy('displayName'); if (!isEmpty(vendorsIds)) { vendorQuery.whereIn('id', vendorsIds); @@ -113,7 +116,7 @@ export class VendorBalanceSummaryRepository { * @returns {Promise} */ public async getPayableAccounts(): Promise[]> { - return this.accountModel + return this.accountModel() .query() .where('accountType', ACCOUNT_TYPE.ACCOUNTS_PAYABLE); } @@ -130,7 +133,7 @@ export class VendorBalanceSummaryRepository { const payableAccountsIds = map(payableAccounts, 'id'); // Retrieve the customers transactions of A/R accounts. - const customersTranasctions = await this.accountTransactionModel + const customersTranasctions = await this.accountTransactionModel() .query() .onBuild((query) => { query.whereIn('accountId', payableAccountsIds); diff --git a/packages/server-nest/src/modules/ItemCategories/commands/DeleteItemCategory.service.ts b/packages/server-nest/src/modules/ItemCategories/commands/DeleteItemCategory.service.ts index 5db7b4fd5..b7001e79a 100644 --- a/packages/server-nest/src/modules/ItemCategories/commands/DeleteItemCategory.service.ts +++ b/packages/server-nest/src/modules/ItemCategories/commands/DeleteItemCategory.service.ts @@ -31,7 +31,6 @@ export class DeleteItemCategoryService { /** * Deletes the given item category. - * @param {number} tenantId - Tenant id. * @param {number} itemCategoryId - Item category id. * @return {Promise} */ diff --git a/packages/server-nest/src/modules/PaymentReceived/PaymentsReceived.module.ts b/packages/server-nest/src/modules/PaymentReceived/PaymentsReceived.module.ts index 80214e128..3e769e4c6 100644 --- a/packages/server-nest/src/modules/PaymentReceived/PaymentsReceived.module.ts +++ b/packages/server-nest/src/modules/PaymentReceived/PaymentsReceived.module.ts @@ -31,9 +31,12 @@ import { GetPaymentsReceivedService } from './queries/GetPaymentsReceived.servic import { MailNotificationModule } from '../MailNotification/MailNotification.module'; import { DynamicListModule } from '../DynamicListing/DynamicList.module'; import { MailModule } from '../Mail/Mail.module'; +import { SendPaymentReceivedMailProcessor } from './processors/PaymentReceivedMailNotification.processor'; +import { BullModule } from '@nestjs/bull'; +import { SEND_PAYMENT_RECEIVED_MAIL_QUEUE } from './constants'; @Module({ - controllers: [PaymentReceivesController,], + controllers: [PaymentReceivesController], providers: [ PaymentReceivesApplication, CreatePaymentReceivedService, @@ -53,8 +56,9 @@ import { MailModule } from '../Mail/Mail.module'; PaymentReceivedAutoIncrementSubscriber, PaymentReceivedGLEntriesSubscriber, PaymentReceivedSyncInvoicesSubscriber, + GetPaymentsReceivedService, SendPaymentReceiveMailNotification, - GetPaymentsReceivedService + SendPaymentReceivedMailProcessor, ], exports: [PaymentReceivesApplication, CreatePaymentReceivedService], imports: [ @@ -68,7 +72,8 @@ import { MailModule } from '../Mail/Mail.module'; AccountsModule, MailNotificationModule, DynamicListModule, - MailModule + MailModule, + BullModule.registerQueue({ name: SEND_PAYMENT_RECEIVED_MAIL_QUEUE }), ], }) export class PaymentsReceivedModule {} diff --git a/packages/server-nest/src/modules/PaymentReceived/commands/PaymentReceivedMailNotification.ts b/packages/server-nest/src/modules/PaymentReceived/commands/PaymentReceivedMailNotification.ts index 548955518..7dca72f12 100644 --- a/packages/server-nest/src/modules/PaymentReceived/commands/PaymentReceivedMailNotification.ts +++ b/packages/server-nest/src/modules/PaymentReceived/commands/PaymentReceivedMailNotification.ts @@ -1,7 +1,11 @@ +import { Queue } from 'bullmq'; +import { InjectQueue } from '@nestjs/bullmq'; import { Inject, Injectable } from '@nestjs/common'; import { DEFAULT_PAYMENT_MAIL_CONTENT, DEFAULT_PAYMENT_MAIL_SUBJECT, + SEND_PAYMENT_RECEIVED_MAIL_JOB, + SEND_PAYMENT_RECEIVED_MAIL_QUEUE, } from '../constants'; import { transformPaymentReceivedToMailDataArgs } from '../utils'; import { EventEmitter2 } from '@nestjs/event-emitter'; @@ -10,13 +14,17 @@ import { ContactMailNotification } from '@/modules/MailNotification/ContactMailN import { PaymentReceived } from '../models/PaymentReceived'; import { GetPaymentReceivedService } from '../queries/GetPaymentReceived.service'; import { mergeAndValidateMailOptions } from '@/modules/MailNotification/utils'; -import { PaymentReceiveMailOptsDTO } from '../types/PaymentReceived.types'; +import { + PaymentReceiveMailOptsDTO, + SendPaymentReceivedMailPayload, +} from '../types/PaymentReceived.types'; import { PaymentReceiveMailOpts } from '../types/PaymentReceived.types'; import { PaymentReceiveMailPresendEvent } from '../types/PaymentReceived.types'; import { SendInvoiceMailDTO } from '@/modules/SaleInvoices/SaleInvoice.types'; import { Mail } from '@/modules/Mail/Mail'; import { MailTransporter } from '@/modules/Mail/MailTransporter.service'; import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; +import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service'; @Injectable() export class SendPaymentReceiveMailNotification { @@ -25,6 +33,10 @@ export class SendPaymentReceiveMailNotification { private readonly contactMailNotification: ContactMailNotification, private readonly eventEmitter: EventEmitter2, private readonly mailTransport: MailTransporter, + private readonly tenancyContext: TenancyContext, + + @InjectQueue(SEND_PAYMENT_RECEIVED_MAIL_QUEUE) + private readonly sendPaymentMailQueue: Queue, @Inject(PaymentReceived.name) private readonly paymentReceiveModel: TenantModelProxy< @@ -40,19 +52,30 @@ export class SendPaymentReceiveMailNotification { * @returns {Promise} */ public async triggerMail( - paymentReceiveId: number, - messageDTO: PaymentReceiveMailOptsDTO, + paymentReceivedId: number, + messageOptions: PaymentReceiveMailOptsDTO, ): Promise { - const payload = { - paymentReceiveId, - messageDTO, - }; - // await this.agenda.now('payment-receive-mail-send', payload); + const tenant = await this.tenancyContext.getTenant(); + const user = await this.tenancyContext.getSystemUser(); + const organizationId = tenant.organizationId; + const userId = user.id; + + const payload = { + paymentReceivedId, + messageOptions, + userId, + organizationId, + } as SendPaymentReceivedMailPayload; + + await this.sendPaymentMailQueue.add( + SEND_PAYMENT_RECEIVED_MAIL_JOB, + payload, + ); // Triggers `onPaymentReceivePreMailSend` event. await this.eventEmitter.emitAsync(events.paymentReceive.onPreMailSend, { - paymentReceiveId, - messageOptions: messageDTO, + paymentReceivedId, + messageOptions, } as PaymentReceiveMailPresendEvent); } diff --git a/packages/server-nest/src/modules/PaymentReceived/constants.ts b/packages/server-nest/src/modules/PaymentReceived/constants.ts index ffbce4a9f..c4b94c2dd 100644 --- a/packages/server-nest/src/modules/PaymentReceived/constants.ts +++ b/packages/server-nest/src/modules/PaymentReceived/constants.ts @@ -1,3 +1,7 @@ +export const SEND_PAYMENT_RECEIVED_MAIL_QUEUE = + 'SEND_PAYMENT_RECEIVED_MAIL_QUEUE'; +export const SEND_PAYMENT_RECEIVED_MAIL_JOB = 'SEND_PAYMENT_RECEIVED_MAIL_JOB'; + export const DEFAULT_PAYMENT_MAIL_SUBJECT = 'Payment Received for {Customer Name} from {Company Name}'; export const DEFAULT_PAYMENT_MAIL_CONTENT = ` diff --git a/packages/server-nest/src/modules/PaymentReceived/processors/PaymentReceivedMailNotification.processor.ts b/packages/server-nest/src/modules/PaymentReceived/processors/PaymentReceivedMailNotification.processor.ts new file mode 100644 index 000000000..5ca792e6a --- /dev/null +++ b/packages/server-nest/src/modules/PaymentReceived/processors/PaymentReceivedMailNotification.processor.ts @@ -0,0 +1,43 @@ +import { JOB_REF, Process, Processor } from '@nestjs/bull'; +import { Job } from 'bull'; +import { + SEND_PAYMENT_RECEIVED_MAIL_JOB, + SEND_PAYMENT_RECEIVED_MAIL_QUEUE, +} from '../constants'; +import { Inject, Scope } from '@nestjs/common'; +import { REQUEST } from '@nestjs/core'; +import { ClsService } from 'nestjs-cls'; +import { SendPaymentReceiveMailNotification } from '../commands/PaymentReceivedMailNotification'; +import { SendPaymentReceivedMailPayload } from '../types/PaymentReceived.types'; + +@Processor({ + name: SEND_PAYMENT_RECEIVED_MAIL_QUEUE, + scope: Scope.REQUEST, +}) +export class SendPaymentReceivedMailProcessor { + constructor( + private readonly sendPaymentReceivedMail: SendPaymentReceiveMailNotification, + private readonly clsService: ClsService, + + @Inject(JOB_REF) + private readonly jobRef: Job, + ) {} + + @Process(SEND_PAYMENT_RECEIVED_MAIL_JOB) + async handleSendMail() { + const { messageOptions, paymentReceivedId, organizationId, userId } = + this.jobRef.data; + + this.clsService.set('organizationId', organizationId); + this.clsService.set('userId', userId); + + try { + await this.sendPaymentReceivedMail.sendMail( + paymentReceivedId, + messageOptions, + ); + } catch (error) { + console.log(error); + } + } +} diff --git a/packages/server-nest/src/modules/PaymentReceived/types/PaymentReceived.types.ts b/packages/server-nest/src/modules/PaymentReceived/types/PaymentReceived.types.ts index cefe06529..12889242e 100644 --- a/packages/server-nest/src/modules/PaymentReceived/types/PaymentReceived.types.ts +++ b/packages/server-nest/src/modules/PaymentReceived/types/PaymentReceived.types.ts @@ -2,7 +2,11 @@ import { AttachmentLinkDTO } from '@/modules/Attachments/Attachments.types'; import { Knex } from 'knex'; import { PaymentReceived } from '../models/PaymentReceived'; import { IDynamicListFilter } from '@/modules/DynamicListing/DynamicFilter/DynamicFilter.types'; -import { CommonMailOptions, CommonMailOptionsDTO } from '@/modules/MailNotification/MailNotification.types'; +import { + CommonMailOptions, + CommonMailOptionsDTO, +} from '@/modules/MailNotification/MailNotification.types'; +import { TenantJobPayload } from '@/interfaces/Tenant'; export interface IPaymentReceivedCreateDTO { customerId: number; @@ -148,7 +152,7 @@ export enum PaymentReceiveAction { export interface PaymentReceiveMailOpts extends CommonMailOptions {} export interface PaymentReceiveMailOptsDTO extends CommonMailOptionsDTO {} export interface PaymentReceiveMailPresendEvent { - paymentReceiveId: number; + paymentReceivedId: number; messageOptions: PaymentReceiveMailOptsDTO; } @@ -207,3 +211,8 @@ export interface PaymentReceivedPdfTemplateAttributes { export interface IPaymentReceivedState { defaultTemplateId: number; } + +export interface SendPaymentReceivedMailPayload extends TenantJobPayload { + paymentReceivedId: number; + messageOptions: PaymentReceiveMailOptsDTO; +} diff --git a/packages/server-nest/src/modules/SaleReceipts/commands/SaleReceiptMailNotification.ts b/packages/server-nest/src/modules/SaleReceipts/commands/SaleReceiptMailNotification.ts index e69574073..75edee509 100644 --- a/packages/server-nest/src/modules/SaleReceipts/commands/SaleReceiptMailNotification.ts +++ b/packages/server-nest/src/modules/SaleReceipts/commands/SaleReceiptMailNotification.ts @@ -71,7 +71,9 @@ export class SaleReceiptMailNotification { organizationId, } as SaleReceiptSendMailPayload; - this.sendSaleReceiptMailProcess.add(SendSaleReceiptMailJob, { ...payload }); + await this.sendSaleReceiptMailProcess.add(SendSaleReceiptMailJob, { + ...payload, + }); // Triggers the event `onSaleReceiptPreMailSend`. await this.eventEmitter.emitAsync(events.saleReceipt.onPreMailSend, { diff --git a/packages/server-nest/src/modules/SaleReceipts/processes/SendSaleReceiptMail.process.ts b/packages/server-nest/src/modules/SaleReceipts/processes/SendSaleReceiptMail.process.ts index d77c504b8..4bd8accdc 100644 --- a/packages/server-nest/src/modules/SaleReceipts/processes/SendSaleReceiptMail.process.ts +++ b/packages/server-nest/src/modules/SaleReceipts/processes/SendSaleReceiptMail.process.ts @@ -2,16 +2,22 @@ import { Process, Processor } from '@nestjs/bull'; import { Job } from 'bull'; import { SendSaleReceiptMailQueue } from '../constants'; import { SaleReceiptMailNotification } from '../commands/SaleReceiptMailNotification'; +import { SaleReceiptSendMailPayload } from '../types/SaleReceipts.types'; +import { ClsService } from 'nestjs-cls'; @Processor(SendSaleReceiptMailQueue) export class SendSaleReceiptMailProcess { constructor( private readonly saleReceiptMailNotification: SaleReceiptMailNotification, + private readonly clsService: ClsService, ) {} @Process(SendSaleReceiptMailQueue) - async handleSendMailJob(job: Job) { - const { messageOpts, saleReceiptId } = job.data; + async handleSendMailJob(job: Job) { + const { messageOpts, saleReceiptId, organizationId, userId } = job.data; + + this.clsService.set('organizationId', organizationId); + this.clsService.set('userId', userId); await this.saleReceiptMailNotification.sendMail(saleReceiptId, messageOpts); }