mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat(server): change the Plaid synced bank name.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('accounts', (table) => {
|
||||
table.integer('uncategorized_transactions').defaultTo(0);
|
||||
table.boolean('is_system_account').defaultTo(true);
|
||||
table.boolean('is_feeds_active').defaultTo(false);
|
||||
table.datetime('last_feeds_updated_at').nullable();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -40,11 +40,14 @@ export class PlaidSyncDb {
|
||||
*/
|
||||
public async syncBankAccounts(
|
||||
tenantId: number,
|
||||
plaidAccounts: PlaidAccount[]
|
||||
plaidAccounts: PlaidAccount[],
|
||||
institution: any
|
||||
): Promise<void> {
|
||||
const accountCreateDTOs = R.map(transformPlaidAccountToCreateAccount)(
|
||||
plaidAccounts
|
||||
);
|
||||
const transformToPlaidAccounts =
|
||||
transformPlaidAccountToCreateAccount(institution);
|
||||
|
||||
const accountCreateDTOs = R.map(transformToPlaidAccounts)(plaidAccounts);
|
||||
|
||||
await bluebird.map(
|
||||
accountCreateDTOs,
|
||||
(createAccountDTO: any) =>
|
||||
@@ -162,4 +165,38 @@ export class PlaidSyncDb {
|
||||
|
||||
await PlaidItem.query().findOne({ plaidItemId }).patch({ lastCursor });
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the last feeds updated at of the given Plaid accounts ids.
|
||||
* @param {number} tenantId
|
||||
* @param {string[]} plaidAccountIds
|
||||
*/
|
||||
public async updateLastFeedsUpdatedAt(
|
||||
tenantId: number,
|
||||
plaidAccountIds: string[]
|
||||
) {
|
||||
const { Account } = this.tenancy.models(tenantId);
|
||||
|
||||
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
|
||||
lastFeedsUpdatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the accounts feed active status of the given Plaid accounts ids.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} plaidAccountIds
|
||||
* @param {boolean} isFeedsActive
|
||||
*/
|
||||
public async updateAccountsFeedsActive(
|
||||
tenantId: number,
|
||||
plaidAccountIds: string[],
|
||||
isFeedsActive: boolean = true
|
||||
) {
|
||||
const { Account } = this.tenancy.models(tenantId);
|
||||
|
||||
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
|
||||
isFeedsActive,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,19 @@ export class PlaidUpdateTransactions {
|
||||
const request = { access_token: accessToken };
|
||||
const plaidInstance = new PlaidClientWrapper();
|
||||
const {
|
||||
data: { accounts },
|
||||
data: { accounts, item },
|
||||
} = await plaidInstance.accountsGet(request);
|
||||
|
||||
const plaidAccountsIds = accounts.map((a) => a.account_id);
|
||||
|
||||
const {
|
||||
data: { institution },
|
||||
} = await plaidInstance.institutionsGetById({
|
||||
institution_id: item.institution_id,
|
||||
country_codes: ['US', 'UK'],
|
||||
});
|
||||
// Update the DB.
|
||||
await this.plaidSync.syncBankAccounts(tenantId, accounts);
|
||||
await this.plaidSync.syncBankAccounts(tenantId, accounts, institution);
|
||||
await this.plaidSync.syncAccountsTransactions(
|
||||
tenantId,
|
||||
added.concat(modified)
|
||||
@@ -37,6 +45,12 @@ export class PlaidUpdateTransactions {
|
||||
await this.plaidSync.syncRemoveTransactions(tenantId, removed);
|
||||
await this.plaidSync.syncTransactionsCursor(tenantId, plaidItemId, cursor);
|
||||
|
||||
// Update the last feeds updated at of the updated accounts.
|
||||
await this.plaidSync.updateLastFeedsUpdatedAt(tenantId, plaidAccountsIds);
|
||||
|
||||
// Turn on the accounts feeds flag.
|
||||
await this.plaidSync.updateAccountsFeedsActive(tenantId, plaidAccountsIds);
|
||||
|
||||
return {
|
||||
addedCount: added.length,
|
||||
modifiedCount: modified.length,
|
||||
|
||||
@@ -2,8 +2,6 @@ import * as R from 'ramda';
|
||||
import {
|
||||
CreateUncategorizedTransactionDTO,
|
||||
IAccountCreateDTO,
|
||||
ICashflowNewCommandDTO,
|
||||
IUncategorizedCashflowTransaction,
|
||||
PlaidAccount,
|
||||
PlaidTransaction,
|
||||
} from '@/interfaces';
|
||||
@@ -13,21 +11,21 @@ import {
|
||||
* @param {PlaidAccount} plaidAccount
|
||||
* @returns {IAccountCreateDTO}
|
||||
*/
|
||||
export const transformPlaidAccountToCreateAccount = (
|
||||
plaidAccount: PlaidAccount
|
||||
): IAccountCreateDTO => {
|
||||
return {
|
||||
name: plaidAccount.name,
|
||||
code: '',
|
||||
description: plaidAccount.official_name,
|
||||
currencyCode: plaidAccount.balances.iso_currency_code,
|
||||
accountType: 'cash',
|
||||
active: true,
|
||||
plaidAccountId: plaidAccount.account_id,
|
||||
bankBalance: plaidAccount.balances.current,
|
||||
accountMask: plaidAccount.mask,
|
||||
};
|
||||
};
|
||||
export const transformPlaidAccountToCreateAccount = R.curry(
|
||||
(institution: any, plaidAccount: PlaidAccount): IAccountCreateDTO => {
|
||||
return {
|
||||
name: `${institution.name} - ${plaidAccount.name}`,
|
||||
code: '',
|
||||
description: plaidAccount.official_name,
|
||||
currencyCode: plaidAccount.balances.iso_currency_code,
|
||||
accountType: 'cash',
|
||||
active: true,
|
||||
plaidAccountId: plaidAccount.account_id,
|
||||
bankBalance: plaidAccount.balances.current,
|
||||
accountMask: plaidAccount.mask,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Transformes the plaid transaction to cashflow create DTO.
|
||||
|
||||
Reference in New Issue
Block a user