From 8a4034cc5d65cd2ecee5f46aae389acf24dd3dac Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 16 Oct 2024 19:46:39 +0200 Subject: [PATCH] fix: Sync Plaid credit card account type --- packages/server/src/data/AccountTypes.ts | 1 + .../models/UncategorizedCashflowTransaction.ts | 6 ++++++ .../server/src/services/Banking/Plaid/utils.ts | 18 +++++++++++++++++- .../Cashflow/GetCashflowAccountsService.ts | 11 +++++++++-- .../Cashflow/GetCashflowTransactionsService.ts | 4 ++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/server/src/data/AccountTypes.ts b/packages/server/src/data/AccountTypes.ts index b33148625..b6055e852 100644 --- a/packages/server/src/data/AccountTypes.ts +++ b/packages/server/src/data/AccountTypes.ts @@ -129,6 +129,7 @@ export const ACCOUNT_TYPES = [ normal: ACCOUNT_NORMAL.CREDIT, rootType: ACCOUNT_ROOT_TYPE.LIABILITY, parentType: ACCOUNT_PARENT_TYPE.CURRENT_LIABILITY, + multiCurrency: true, balanceSheet: true, incomeSheet: false, }, diff --git a/packages/server/src/models/UncategorizedCashflowTransaction.ts b/packages/server/src/models/UncategorizedCashflowTransaction.ts index e1a98b0c0..708cc123c 100644 --- a/packages/server/src/models/UncategorizedCashflowTransaction.ts +++ b/packages/server/src/models/UncategorizedCashflowTransaction.ts @@ -11,6 +11,12 @@ export default class UncategorizedCashflowTransaction extends mixin( ) { id!: number; date!: Date | string; + + /** + * Transaction amount. + * Negative represents to spending and positive to deposit/card charge. + * @param {number} + */ amount!: number; categorized!: boolean; accountId!: number; diff --git a/packages/server/src/services/Banking/Plaid/utils.ts b/packages/server/src/services/Banking/Plaid/utils.ts index 2804279c5..3700e37ab 100644 --- a/packages/server/src/services/Banking/Plaid/utils.ts +++ b/packages/server/src/services/Banking/Plaid/utils.ts @@ -4,11 +4,27 @@ import { Institution as PlaidInstitution, AccountBase as PlaidAccount, TransactionBase as PlaidTransactionBase, + AccountType as PlaidAccountType, } from 'plaid'; import { CreateUncategorizedTransactionDTO, IAccountCreateDTO, } from '@/interfaces'; +import { ACCOUNT_TYPE } from '@/data/AccountTypes'; + +/** + * Retrieves the system account type from the given Plaid account type. + * @param {PlaidAccountType} plaidAccountType + * @returns {string} + */ +const getAccountTypeFromPlaidAccountType = ( + plaidAccountType: PlaidAccountType +) => { + if (plaidAccountType === PlaidAccountType.Credit) { + return ACCOUNT_TYPE.CREDIT_CARD; + } + return ACCOUNT_TYPE.BANK; +}; /** * Transformes the Plaid account to create cashflow account DTO. @@ -28,7 +44,7 @@ export const transformPlaidAccountToCreateAccount = R.curry( code: '', description: plaidAccount.official_name, currencyCode: plaidAccount.balances.iso_currency_code, - accountType: 'cash', + accountType: getAccountTypeFromPlaidAccountType(plaidAccount.type), active: true, bankBalance: plaidAccount.balances.current, accountMask: plaidAccount.mask, diff --git a/packages/server/src/services/Cashflow/GetCashflowAccountsService.ts b/packages/server/src/services/Cashflow/GetCashflowAccountsService.ts index 65b3c50a1..0c608816b 100644 --- a/packages/server/src/services/Cashflow/GetCashflowAccountsService.ts +++ b/packages/server/src/services/Cashflow/GetCashflowAccountsService.ts @@ -4,6 +4,7 @@ import { CashflowAccountTransformer } from './CashflowAccountTransformer'; import TenancyService from '@/services/Tenancy/TenancyService'; import DynamicListingService from '@/services/DynamicListing/DynamicListService'; import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable'; +import { ACCOUNT_TYPE } from '@/data/AccountTypes'; @Service() export default class GetCashflowAccountsService { @@ -41,14 +42,20 @@ export default class GetCashflowAccountsService { const accounts = await CashflowAccount.query().onBuild((builder) => { dynamicList.buildQuery()(builder); - builder.whereIn('account_type', ['bank', 'cash']); + builder.whereIn('account_type', [ + ACCOUNT_TYPE.BANK, + ACCOUNT_TYPE.CASH, + ACCOUNT_TYPE.CREDIT_CARD, + ]); builder.modify('inactiveMode', filter.inactiveMode); }); // Retrieves the transformed accounts. - return this.transformer.transform( + const transformed = await this.transformer.transform( tenantId, accounts, new CashflowAccountTransformer() ); + + return transformed; } } diff --git a/packages/server/src/services/Cashflow/GetCashflowTransactionsService.ts b/packages/server/src/services/Cashflow/GetCashflowTransactionsService.ts index 64afd2194..919eca70d 100644 --- a/packages/server/src/services/Cashflow/GetCashflowTransactionsService.ts +++ b/packages/server/src/services/Cashflow/GetCashflowTransactionsService.ts @@ -12,7 +12,7 @@ export class GetCashflowTransactionService { private tenancy: HasTenancyService; @Inject() - private transfromer: TransformerInjectable; + private transformer: TransformerInjectable; /** * Retrieve the given cashflow transaction. @@ -37,7 +37,7 @@ export class GetCashflowTransactionService { this.throwErrorCashflowTranscationNotFound(cashflowTransaction); // Transformes the cashflow transaction model to POJO. - return this.transfromer.transform( + return this.transformer.transform( tenantId, cashflowTransaction, new CashflowTransactionTransformer()