Compare commits

...

1 Commits

Author SHA1 Message Date
Ahmed Bouhuolia
59168bc691 fix: Plaid transactions syncing 2024-07-12 23:43:20 +02:00
6 changed files with 74 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
import { Knex } from 'knex';
import { import {
IFinancialSheetCommonMeta, IFinancialSheetCommonMeta,
INumberFormatQuery, INumberFormatQuery,
@@ -257,7 +258,6 @@ export interface IUncategorizedCashflowTransaction {
categorized: boolean; categorized: boolean;
} }
export interface CreateUncategorizedTransactionDTO { export interface CreateUncategorizedTransactionDTO {
date: Date | string; date: Date | string;
accountId: number; accountId: number;
@@ -269,3 +269,16 @@ export interface CreateUncategorizedTransactionDTO {
plaidTransactionId?: string | null; plaidTransactionId?: string | null;
batch?: string; batch?: string;
} }
export interface IUncategorizedTransactionCreatingEventPayload {
tenantId: number;
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO;
trx: Knex.Transaction;
}
export interface IUncategorizedTransactionCreatedEventPayload {
tenantId: number;
uncategorizedTransaction: any;
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO;
trx: Knex.Transaction;
}

View File

@@ -148,7 +148,6 @@ export class PlaidSyncDb {
*/ */
public async syncAccountsTransactions( public async syncAccountsTransactions(
tenantId: number, tenantId: number,
batchNo: string,
plaidAccountsTransactions: PlaidTransaction[], plaidAccountsTransactions: PlaidTransaction[],
trx?: Knex.Transaction trx?: Knex.Transaction
): Promise<void> { ): Promise<void> {
@@ -161,7 +160,6 @@ export class PlaidSyncDb {
return this.syncAccountTranactions( return this.syncAccountTranactions(
tenantId, tenantId,
plaidAccountId, plaidAccountId,
batchNo,
plaidTransactions, plaidTransactions,
trx trx
); );

View File

@@ -37,7 +37,6 @@ export const transformPlaidAccountToCreateAccount = R.curry(
export const transformPlaidTrxsToCashflowCreate = R.curry( export const transformPlaidTrxsToCashflowCreate = R.curry(
( (
cashflowAccountId: number, cashflowAccountId: number,
creditAccountId: number,
plaidTranasction: PlaidTransaction plaidTranasction: PlaidTransaction
): CreateUncategorizedTransactionDTO => { ): CreateUncategorizedTransactionDTO => {
return { return {

View File

@@ -2,7 +2,13 @@ import { Knex } from 'knex';
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService'; import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork'; 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() @Service()
export class CreateUncategorizedTransaction { export class CreateUncategorizedTransaction {
@@ -12,6 +18,9 @@ export class CreateUncategorizedTransaction {
@Inject() @Inject()
private uow: UnitOfWork; private uow: UnitOfWork;
@Inject()
private eventPublisher: EventPublisher;
/** /**
* Creates an uncategorized cashflow transaction. * Creates an uncategorized cashflow transaction.
* @param {number} tenantId * @param {number} tenantId
@@ -19,7 +28,7 @@ export class CreateUncategorizedTransaction {
*/ */
public create( public create(
tenantId: number, tenantId: number,
createDTO: CreateUncategorizedTransactionDTO, createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO,
trx?: Knex.Transaction trx?: Knex.Transaction
) { ) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId); const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
@@ -27,12 +36,30 @@ export class CreateUncategorizedTransaction {
return this.uow.withTransaction( return this.uow.withTransaction(
tenantId, tenantId,
async (trx: Knex.Transaction) => { async (trx: Knex.Transaction) => {
const transaction = await UncategorizedCashflowTransaction.query( await this.eventPublisher.emitAsync(
trx events.cashflow.onTransactionUncategorizedCreated,
).insertAndFetch({ {
...createDTO, tenantId,
}); createUncategorizedTransactionDTO,
return transaction; 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 trx
); );

View File

@@ -22,6 +22,10 @@ export class DecrementUncategorizedTransactionOnCategorize {
events.cashflow.onTransactionUncategorized, events.cashflow.onTransactionUncategorized,
this.incrementUnCategorizedTransactionsOnUncategorized.bind(this) this.incrementUnCategorizedTransactionsOnUncategorized.bind(this)
); );
bus.subscribe(
events.cashflow.onTransactionUncategorizedCreated,
this.incrementUncategoirzedTransactionsOnCreated.bind(this)
);
} }
/** /**
@@ -53,4 +57,22 @@ export class DecrementUncategorizedTransactionOnCategorize {
.findById(uncategorizedTransaction.accountId) .findById(uncategorizedTransaction.accountId)
.increment('uncategorizedTransactions', 1); .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);
}
} }

View File

@@ -399,6 +399,9 @@ export default {
onTransactionCategorizing: 'onTransactionCategorizing', onTransactionCategorizing: 'onTransactionCategorizing',
onTransactionCategorized: 'onCashflowTransactionCategorized', onTransactionCategorized: 'onCashflowTransactionCategorized',
onTransactionUncategorizedCreating: 'onTransactionUncategorizedCreating',
onTransactionUncategorizedCreated: 'onTransactionUncategorizedCreated',
onTransactionUncategorizing: 'onTransactionUncategorizing', onTransactionUncategorizing: 'onTransactionUncategorizing',
onTransactionUncategorized: 'onTransactionUncategorized', onTransactionUncategorized: 'onTransactionUncategorized',