feat: ability to remove the removed Plaid transactions in updating

This commit is contained in:
Ahmed Bouhuolia
2024-02-04 22:17:37 +02:00
parent c688190acc
commit 00d9bc537c
3 changed files with 40 additions and 5 deletions

View File

@@ -9,8 +9,11 @@ import {
transformPlaidTrxsToCashflowCreate,
} from './utils';
import NewCashflowTransactionService from '@/services/Cashflow/NewCashflowTransactionService';
import DeleteCashflowTransactionService from '@/services/Cashflow/DeleteCashflowTransactionService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
const CONCURRENCY_ASYNC = 10;
@Service()
export class PlaidSyncDb {
@Inject()
@@ -22,6 +25,9 @@ export class PlaidSyncDb {
@Inject()
private createCashflowTransactionService: NewCashflowTransactionService;
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransactionService;
/**
* Syncs the plaid accounts to the system accounts.
* @param {number} tenantId Tenant ID.
@@ -39,7 +45,7 @@ export class PlaidSyncDb {
accountCreateDTOs,
(createAccountDTO: any) =>
this.createAccountService.createAccount(tenantId, createAccountDTO),
{ concurrency: 10 }
{ concurrency: CONCURRENCY_ASYNC }
);
}
@@ -79,7 +85,7 @@ export class PlaidSyncDb {
tenantId,
cashflowDTO
),
{ concurrency: 10 }
{ concurrency: CONCURRENCY_ASYNC }
);
}
@@ -104,7 +110,35 @@ export class PlaidSyncDb {
plaidTransactions
);
},
{ concurrency: 10 }
{ concurrency: CONCURRENCY_ASYNC }
);
}
/**
* Syncs the removed Plaid transactions ids from the cashflow system transactions.
* @param {string[]} plaidTransactionsIds - Plaid Transactions IDs.
*/
public async syncRemoveTransactions(
tenantId: number,
plaidTransactionsIds: string[]
) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
const cashflowTransactions = await CashflowTransaction.query().whereIn(
'plaidTransactionId',
plaidTransactionsIds
);
const cashflowTransactionsIds = cashflowTransactions.map(
(trans) => trans.id
);
await bluebird.map(
cashflowTransactionsIds,
(transactionId: number) =>
this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId,
transactionId
),
{ concurrency: CONCURRENCY_ASYNC }
);
}
@@ -121,6 +155,6 @@ export class PlaidSyncDb {
) {
const { PlaidItem } = this.tenancy.models(tenantId);
await PlaidItem.query().findById(plaidItemId).patch({ lastCursor });
await PlaidItem.query().findOne({ plaidItemId }).patch({ lastCursor });
}
}