diff --git a/packages/server/src/interfaces/CashFlow.ts b/packages/server/src/interfaces/CashFlow.ts index ce9bbf5b2..e6838711c 100644 --- a/packages/server/src/interfaces/CashFlow.ts +++ b/packages/server/src/interfaces/CashFlow.ts @@ -1,3 +1,4 @@ +import { Knex } from 'knex'; import { IFinancialSheetCommonMeta, INumberFormatQuery, @@ -257,7 +258,6 @@ export interface IUncategorizedCashflowTransaction { categorized: boolean; } - export interface CreateUncategorizedTransactionDTO { date: Date | string; accountId: number; @@ -269,3 +269,16 @@ export interface CreateUncategorizedTransactionDTO { plaidTransactionId?: string | null; batch?: string; } + +export interface IUncategorizedTransactionCreatingEventPayload { + tenantId: number; + createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO; + trx: Knex.Transaction; +} + +export interface IUncategorizedTransactionCreatedEventPayload { + tenantId: number; + uncategorizedTransaction: any; + createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO; + trx: Knex.Transaction; +} diff --git a/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts b/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts index af5bc8c8a..40f30af17 100644 --- a/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts +++ b/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts @@ -148,7 +148,6 @@ export class PlaidSyncDb { */ public async syncAccountsTransactions( tenantId: number, - batchNo: string, plaidAccountsTransactions: PlaidTransaction[], trx?: Knex.Transaction ): Promise { @@ -161,7 +160,6 @@ export class PlaidSyncDb { return this.syncAccountTranactions( tenantId, plaidAccountId, - batchNo, plaidTransactions, trx ); diff --git a/packages/server/src/services/Banking/Plaid/utils.ts b/packages/server/src/services/Banking/Plaid/utils.ts index a416d0a36..243f9449b 100644 --- a/packages/server/src/services/Banking/Plaid/utils.ts +++ b/packages/server/src/services/Banking/Plaid/utils.ts @@ -37,7 +37,6 @@ export const transformPlaidAccountToCreateAccount = R.curry( export const transformPlaidTrxsToCashflowCreate = R.curry( ( cashflowAccountId: number, - creditAccountId: number, plaidTranasction: PlaidTransaction ): CreateUncategorizedTransactionDTO => { return { diff --git a/packages/server/src/services/Cashflow/CreateUncategorizedTransaction.ts b/packages/server/src/services/Cashflow/CreateUncategorizedTransaction.ts index 434722049..0433f292d 100644 --- a/packages/server/src/services/Cashflow/CreateUncategorizedTransaction.ts +++ b/packages/server/src/services/Cashflow/CreateUncategorizedTransaction.ts @@ -2,7 +2,13 @@ import { Knex } from 'knex'; import { Inject, Service } from 'typedi'; import HasTenancyService from '../Tenancy/TenancyService'; import UnitOfWork from '../UnitOfWork'; -import { CreateUncategorizedTransactionDTO } from '@/interfaces'; +import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; +import events from '@/subscribers/events'; +import { + CreateUncategorizedTransactionDTO, + IUncategorizedTransactionCreatedEventPayload, + IUncategorizedTransactionCreatingEventPayload, +} from '@/interfaces'; @Service() export class CreateUncategorizedTransaction { @@ -12,6 +18,9 @@ export class CreateUncategorizedTransaction { @Inject() private uow: UnitOfWork; + @Inject() + private eventPublisher: EventPublisher; + /** * Creates an uncategorized cashflow transaction. * @param {number} tenantId @@ -19,7 +28,7 @@ export class CreateUncategorizedTransaction { */ public create( tenantId: number, - createDTO: CreateUncategorizedTransactionDTO, + createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO, trx?: Knex.Transaction ) { const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId); @@ -27,12 +36,30 @@ export class CreateUncategorizedTransaction { return this.uow.withTransaction( tenantId, async (trx: Knex.Transaction) => { - const transaction = await UncategorizedCashflowTransaction.query( - trx - ).insertAndFetch({ - ...createDTO, - }); - return transaction; + await this.eventPublisher.emitAsync( + events.cashflow.onTransactionUncategorizedCreated, + { + tenantId, + createUncategorizedTransactionDTO, + trx, + } as IUncategorizedTransactionCreatingEventPayload + ); + + const uncategorizedTransaction = + await UncategorizedCashflowTransaction.query(trx).insertAndFetch({ + ...createUncategorizedTransactionDTO, + }); + + await this.eventPublisher.emitAsync( + events.cashflow.onTransactionUncategorizedCreated, + { + tenantId, + uncategorizedTransaction, + createUncategorizedTransactionDTO, + trx, + } as IUncategorizedTransactionCreatedEventPayload + ); + return uncategorizedTransaction; }, trx ); diff --git a/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts b/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts index eb56d67dd..df68c36de 100644 --- a/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts +++ b/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts @@ -22,6 +22,10 @@ export class DecrementUncategorizedTransactionOnCategorize { events.cashflow.onTransactionUncategorized, this.incrementUnCategorizedTransactionsOnUncategorized.bind(this) ); + bus.subscribe( + events.cashflow.onTransactionUncategorizedCreated, + this.incrementUncategoirzedTransactionsOnCreated.bind(this) + ); } /** @@ -53,4 +57,22 @@ export class DecrementUncategorizedTransactionOnCategorize { .findById(uncategorizedTransaction.accountId) .increment('uncategorizedTransactions', 1); } + + /** + * Increments uncategorized transactions count once creating a new transaction. + * @param {ICommandCashflowCreatedPayload} payload - + */ + public async incrementUncategoirzedTransactionsOnCreated({ + tenantId, + uncategorizedTransaction, + trx, + }: any) { + const { Account } = this.tenancy.models(tenantId); + + if (!uncategorizedTransaction.accountId) return; + + await Account.query(trx) + .findById(uncategorizedTransaction.accountId) + .increment('uncategorizedTransactions', 1); + } } diff --git a/packages/server/src/subscribers/events.ts b/packages/server/src/subscribers/events.ts index ba6f28c35..711dbce35 100644 --- a/packages/server/src/subscribers/events.ts +++ b/packages/server/src/subscribers/events.ts @@ -399,6 +399,9 @@ export default { onTransactionCategorizing: 'onTransactionCategorizing', onTransactionCategorized: 'onCashflowTransactionCategorized', + onTransactionUncategorizedCreating: 'onTransactionUncategorizedCreating', + onTransactionUncategorizedCreated: 'onTransactionUncategorizedCreated', + onTransactionUncategorizing: 'onTransactionUncategorizing', onTransactionUncategorized: 'onTransactionUncategorized',