Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pending bank transactions #589

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Inject, Service } from 'typedi';
import { NextFunction, Request, Response, Router } from 'express';
import { param, query } from 'express-validator';
import BaseController from '@/api/controllers/BaseController';
import { GetBankAccountSummary } from '@/services/Banking/BankAccounts/GetBankAccountSummary';
import { BankAccountsApplication } from '@/services/Banking/BankAccounts/BankAccountsApplication';
import { param } from 'express-validator';
import { GetPendingBankAccountTransactions } from '@/services/Cashflow/GetPendingBankAccountTransaction';

@Service()
export class BankAccountsController extends BaseController {
Expand All @@ -13,31 +14,40 @@ export class BankAccountsController extends BaseController {
@Inject()
private bankAccountsApp: BankAccountsApplication;

@Inject()
private getPendingTransactionsService: GetPendingBankAccountTransactions;

/**
* Router constructor.
*/
router() {
const router = Router();

router.get('/:bankAccountId/meta', this.getBankAccountSummary.bind(this));
router.get(
'/pending_transactions',
[
query('account_id').optional().isNumeric().toInt(),
query('page').optional().isNumeric().toInt(),
query('page_size').optional().isNumeric().toInt(),
],
this.validationResult,
this.getBankAccountsPendingTransactions.bind(this)
);
router.post(
'/:bankAccountId/disconnect',
this.disconnectBankAccount.bind(this)
);
router.post('/:bankAccountId/update', this.refreshBankAccount.bind(this));
router.post(
'/:bankAccountId/pause_feeds',
[
param('bankAccountId').exists().isNumeric().toInt(),
],
[param('bankAccountId').exists().isNumeric().toInt()],
this.validationResult,
this.pauseBankAccountFeeds.bind(this)
);
router.post(
'/:bankAccountId/resume_feeds',
[
param('bankAccountId').exists().isNumeric().toInt(),
],
[param('bankAccountId').exists().isNumeric().toInt()],
this.validationResult,
this.resumeBankAccountFeeds.bind(this)
);
Expand Down Expand Up @@ -72,6 +82,32 @@ export class BankAccountsController extends BaseController {
}
}

/**
* Retrieves the bank account pending transactions.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getBankAccountsPendingTransactions(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const query = this.matchedQueryData(req);

try {
const data =
await this.getPendingTransactionsService.getPendingTransactions(
tenantId,
query
);
return res.status(200).send(data);
} catch (error) {
next(error);
}
}

/**
* Disonnect the given bank account.
* @param {Request} req
Expand Down Expand Up @@ -128,9 +164,9 @@ export class BankAccountsController extends BaseController {

/**
* Resumes the bank account feeds sync.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @returns {Promise<Response | void>}
*/
async resumeBankAccountFeeds(
Expand All @@ -155,9 +191,9 @@ export class BankAccountsController extends BaseController {

/**
* Pauses the bank account feeds sync.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @returns {Promise<Response | void>}
*/
async pauseBankAccountFeeds(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function (knex) {
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
table.boolean('pending').defaultTo(false);
table.string('pending_plaid_transaction_id').nullable();
});
};

exports.down = function (knex) {
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
table.dropColumn('pending');
table.dropColumn('pending_plaid_transaction_id');
});
};
16 changes: 16 additions & 0 deletions packages/server/src/interfaces/CashFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ export interface CreateUncategorizedTransactionDTO {
description?: string;
referenceNo?: string | null;
plaidTransactionId?: string | null;
pending?: boolean;
pendingPlaidTransactionId?: string | null;
batch?: string;
}

Expand All @@ -283,3 +285,17 @@ export interface IUncategorizedTransactionCreatedEventPayload {
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO;
trx: Knex.Transaction;
}

export interface IPendingTransactionRemovingEventPayload {
tenantId: number;
uncategorizedTransactionId: number;
pendingTransaction: IUncategorizedCashflowTransaction;
trx?: Knex.Transaction;
}

export interface IPendingTransactionRemovedEventPayload {
tenantId: number;
uncategorizedTransactionId: number;
pendingTransaction: IUncategorizedCashflowTransaction;
trx?: Knex.Transaction;
}
26 changes: 25 additions & 1 deletion packages/server/src/models/UncategorizedCashflowTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class UncategorizedCashflowTransaction extends mixin(
plaidTransactionId!: string;
recognizedTransactionId!: number;
excludedAt: Date;
pending: boolean;

/**
* Table name.
Expand All @@ -46,7 +47,8 @@ export default class UncategorizedCashflowTransaction extends mixin(
'isDepositTransaction',
'isWithdrawalTransaction',
'isRecognized',
'isExcluded'
'isExcluded',
'isPending',
];
}

Expand Down Expand Up @@ -99,6 +101,14 @@ export default class UncategorizedCashflowTransaction extends mixin(
return !!this.excludedAt;
}

/**
* Detarmines whether the transaction is pending.
* @returns {boolean}
*/
public get isPending(): boolean {
return !!this.pending;
}

/**
* Model modifiers.
*/
Expand Down Expand Up @@ -143,6 +153,20 @@ export default class UncategorizedCashflowTransaction extends mixin(
query.whereNull('categorizeRefType');
query.whereNull('categorizeRefId');
},

/**
* Filters the not pending transactions.
*/
notPending(query) {
query.where('pending', false);
},

/**
* Filters the pending transactions.
*/
pending(query) {
query.where('pending', true);
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class GetBankAccountSummary {
q.withGraphJoined('matchedBankTransactions');
q.whereNull('matchedBankTransactions.id');

// Exclude the pending transactions.
q.modify('notPending');

// Count the results.
q.count('uncategorized_cashflow_transactions.id as total');
q.first();
Expand All @@ -65,16 +68,32 @@ export class GetBankAccountSummary {
q.withGraphJoined('recognizedTransaction');
q.whereNotNull('recognizedTransaction.id');

// Exclude the pending transactions.
q.modify('notPending');

// Count the results.
q.count('uncategorized_cashflow_transactions.id as total');
q.first();
});

// Retrieves excluded transactions count.
const excludedTransactionsCount =
await UncategorizedCashflowTransaction.query().onBuild((q) => {
q.where('accountId', bankAccountId);
q.modify('excluded');

// Exclude the pending transactions.
q.modify('notPending');

// Count the results.
q.count('uncategorized_cashflow_transactions.id as total');
q.first();
});
// Retrieves the pending transactions count.
const pendingTransactionsCount =
await UncategorizedCashflowTransaction.query().onBuild((q) => {
q.where('accountId', bankAccountId);
q.modify('pending');

// Count the results.
q.count('uncategorized_cashflow_transactions.id as total');
q.first();
Expand All @@ -83,14 +102,15 @@ export class GetBankAccountSummary {
const totalUncategorizedTransactions =
uncategorizedTranasctionsCount?.total || 0;
const totalRecognizedTransactions = recognizedTransactionsCount?.total || 0;

const totalExcludedTransactions = excludedTransactionsCount?.total || 0;
const totalPendingTransactions = pendingTransactionsCount?.total || 0;

return {
name: bankAccount.name,
totalUncategorizedTransactions,
totalRecognizedTransactions,
totalExcludedTransactions,
totalPendingTransactions,
};
}
}
24 changes: 13 additions & 11 deletions packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Knex } from 'knex';
import uniqid from 'uniqid';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { RemovePendingUncategorizedTransaction } from '@/services/Cashflow/RemovePendingUncategorizedTransaction';

const CONCURRENCY_ASYNC = 10;

Expand All @@ -40,7 +41,7 @@ export class PlaidSyncDb {
private cashflowApp: CashflowApplication;

@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransaction;
private removePendingTransaction: RemovePendingUncategorizedTransaction;

@Inject()
private eventPublisher: EventPublisher;
Expand Down Expand Up @@ -185,21 +186,22 @@ export class PlaidSyncDb {
plaidTransactionsIds: string[],
trx?: Knex.Transaction
) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);

const cashflowTransactions = await CashflowTransaction.query(trx).whereIn(
'plaidTransactionId',
plaidTransactionsIds
);
const cashflowTransactionsIds = cashflowTransactions.map(
const uncategorizedTransactions =
await UncategorizedCashflowTransaction.query(trx).whereIn(
'plaidTransactionId',
plaidTransactionsIds
);
const uncategorizedTransactionsIds = uncategorizedTransactions.map(
(trans) => trans.id
);
await bluebird.map(
cashflowTransactionsIds,
(transactionId: number) =>
this.deleteCashflowTransactionService.deleteCashflowTransaction(
uncategorizedTransactionsIds,
(uncategorizedTransactionId: number) =>
this.removePendingTransaction.removePendingTransaction(
tenantId,
transactionId,
uncategorizedTransactionId,
trx
),
{ concurrency: CONCURRENCY_ASYNC }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export class PlaidUpdateTransactions {
item,
trx
);
// Sync removed transactions.
await this.plaidSync.syncRemoveTransactions(
tenantId,
removed?.map((r) => r.transaction_id),
trx
);
// Sync bank account transactions.
await this.plaidSync.syncAccountsTransactions(
tenantId,
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/services/Banking/Plaid/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
Item as PlaidItem,
Institution as PlaidInstitution,
AccountBase as PlaidAccount,
TransactionBase as PlaidTransactionBase,
} from 'plaid';
import {
CreateUncategorizedTransactionDTO,
IAccountCreateDTO,
PlaidTransaction,
} from '@/interfaces';

/**
Expand Down Expand Up @@ -48,7 +48,7 @@ export const transformPlaidAccountToCreateAccount = R.curry(
export const transformPlaidTrxsToCashflowCreate = R.curry(
(
cashflowAccountId: number,
plaidTranasction: PlaidTransaction
plaidTranasction: PlaidTransactionBase
): CreateUncategorizedTransactionDTO => {
return {
date: plaidTranasction.date,
Expand All @@ -64,6 +64,8 @@ export const transformPlaidTrxsToCashflowCreate = R.curry(
accountId: cashflowAccountId,
referenceNo: plaidTranasction.payment_meta?.reference_number,
plaidTransactionId: plaidTranasction.transaction_id,
pending: plaidTranasction.pending,
pendingPlaidTransactionId: plaidTranasction.pending_transaction_id,
};
}
);
Loading
Loading