Skip to content

Commit

Permalink
Merge pull request #525 from bigcapitalhq/fix-plaid-transactions-syncing
Browse files Browse the repository at this point in the history
fix: Plaid transactions syncing
  • Loading branch information
abouolia authored Jul 12, 2024
2 parents 81b26c6 + 59168bc commit 249eada
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
15 changes: 14 additions & 1 deletion packages/server/src/interfaces/CashFlow.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Knex } from 'knex';
import {
IFinancialSheetCommonMeta,
INumberFormatQuery,
Expand Down Expand Up @@ -257,7 +258,6 @@ export interface IUncategorizedCashflowTransaction {
categorized: boolean;
}


export interface CreateUncategorizedTransactionDTO {
date: Date | string;
accountId: number;
Expand All @@ -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;
}
2 changes: 0 additions & 2 deletions packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export class PlaidSyncDb {
*/
public async syncAccountsTransactions(
tenantId: number,
batchNo: string,
plaidAccountsTransactions: PlaidTransaction[],
trx?: Knex.Transaction
): Promise<void> {
Expand All @@ -161,7 +160,6 @@ export class PlaidSyncDb {
return this.syncAccountTranactions(
tenantId,
plaidAccountId,
batchNo,
plaidTransactions,
trx
);
Expand Down
1 change: 0 additions & 1 deletion packages/server/src/services/Banking/Plaid/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const transformPlaidAccountToCreateAccount = R.curry(
export const transformPlaidTrxsToCashflowCreate = R.curry(
(
cashflowAccountId: number,
creditAccountId: number,
plaidTranasction: PlaidTransaction
): CreateUncategorizedTransactionDTO => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -12,27 +18,48 @@ export class CreateUncategorizedTransaction {
@Inject()
private uow: UnitOfWork;

@Inject()
private eventPublisher: EventPublisher;

/**
* Creates an uncategorized cashflow transaction.
* @param {number} tenantId
* @param {CreateUncategorizedTransactionDTO} createDTO
*/
public create(
tenantId: number,
createDTO: CreateUncategorizedTransactionDTO,
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO,
trx?: Knex.Transaction
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);

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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class DecrementUncategorizedTransactionOnCategorize {
events.cashflow.onTransactionUncategorized,
this.incrementUnCategorizedTransactionsOnUncategorized.bind(this)
);
bus.subscribe(
events.cashflow.onTransactionUncategorizedCreated,
this.incrementUncategoirzedTransactionsOnCreated.bind(this)
);
}

/**
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions packages/server/src/subscribers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ export default {
onTransactionCategorizing: 'onTransactionCategorizing',
onTransactionCategorized: 'onCashflowTransactionCategorized',

onTransactionUncategorizedCreating: 'onTransactionUncategorizedCreating',
onTransactionUncategorizedCreated: 'onTransactionUncategorizedCreated',

onTransactionUncategorizing: 'onTransactionUncategorizing',
onTransactionUncategorized: 'onTransactionUncategorized',

Expand Down

0 comments on commit 249eada

Please sign in to comment.