-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #569 from bigcapitalhq/fix-edit-bank-rule-recognized
fix: Recognize transactions on editing bank rule
- Loading branch information
Showing
14 changed files
with
227 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/server/src/services/Banking/RegonizeTranasctions/RevertRecognizedTransactions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Inject, Service } from 'typedi'; | ||
import { castArray } from 'lodash'; | ||
import { Knex } from 'knex'; | ||
import HasTenancyService from '@/services/Tenancy/TenancyService'; | ||
import UnitOfWork from '@/services/UnitOfWork'; | ||
import { RevertRecognizedTransactionsCriteria } from './_types'; | ||
|
||
@Service() | ||
export class RevertRecognizedTransactions { | ||
@Inject() | ||
private tenancy: HasTenancyService; | ||
|
||
@Inject() | ||
private uow: UnitOfWork; | ||
|
||
/** | ||
* Revert and unlinks the recognized transactions based on the given bank rule | ||
* and transactions criteria.. | ||
* @param {number} tenantId - Tenant id. | ||
* @param {number|Array<number>} bankRuleId - Bank rule id. | ||
* @param {RevertRecognizedTransactionsCriteria} transactionsCriteria - | ||
* @param {Knex.Transaction} trx - Knex transaction. | ||
* @returns {Promise<void>} | ||
*/ | ||
public async revertRecognizedTransactions( | ||
tenantId: number, | ||
ruleId?: number | Array<number>, | ||
transactionsCriteria?: RevertRecognizedTransactionsCriteria, | ||
trx?: Knex.Transaction | ||
): Promise<void> { | ||
const { UncategorizedCashflowTransaction, RecognizedBankTransaction } = | ||
this.tenancy.models(tenantId); | ||
|
||
const rulesIds = castArray(ruleId); | ||
|
||
return this.uow.withTransaction( | ||
tenantId, | ||
async (trx: Knex.Transaction) => { | ||
// Retrieves all the recognized transactions of the banbk rule. | ||
const uncategorizedTransactions = | ||
await UncategorizedCashflowTransaction.query(trx).onBuild((q) => { | ||
q.withGraphJoined('recognizedTransaction'); | ||
q.whereNotNull('recognizedTransaction.id'); | ||
|
||
if (rulesIds.length > 0) { | ||
q.whereIn('recognizedTransaction.bankRuleId', rulesIds); | ||
} | ||
if (transactionsCriteria?.accountId) { | ||
q.where('accountId', transactionsCriteria.accountId); | ||
} | ||
if (transactionsCriteria?.batch) { | ||
q.where('batch', transactionsCriteria.batch); | ||
} | ||
}); | ||
const uncategorizedTransactionIds = uncategorizedTransactions.map( | ||
(r) => r.id | ||
); | ||
// Unlink the recongized transactions out of uncategorized transactions. | ||
await UncategorizedCashflowTransaction.query(trx) | ||
.whereIn('id', uncategorizedTransactionIds) | ||
.patch({ | ||
recognizedTransactionId: null, | ||
}); | ||
// Delete the recognized bank transactions that assocaited to bank rule. | ||
await RecognizedBankTransaction.query(trx) | ||
.whereIn('uncategorizedTransactionId', uncategorizedTransactionIds) | ||
.delete(); | ||
}, | ||
trx | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/server/src/services/Banking/RegonizeTranasctions/_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface RevertRecognizedTransactionsCriteria { | ||
batch?: string; | ||
accountId?: number; | ||
} | ||
|
||
|
||
export interface RecognizeTransactionsCriteria { | ||
batch?: string; | ||
accountId?: number; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/server/src/services/Banking/RegonizeTranasctions/jobs/RerecognizeTransactionsJob.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Container, { Service } from 'typedi'; | ||
import { RecognizeTranasctionsService } from '../RecognizeTranasctionsService'; | ||
import { RevertRecognizedTransactions } from '../RevertRecognizedTransactions'; | ||
|
||
@Service() | ||
export class ReregonizeTransactionsJob { | ||
/** | ||
* Constructor method. | ||
*/ | ||
constructor(agenda) { | ||
agenda.define( | ||
'rerecognize-uncategorized-transactions-job', | ||
{ priority: 'high', concurrency: 2 }, | ||
this.handler | ||
); | ||
} | ||
|
||
/** | ||
* Triggers sending invoice mail. | ||
*/ | ||
private handler = async (job, done: Function) => { | ||
const { tenantId, ruleId, transactionsCriteria } = job.attrs.data; | ||
const regonizeTransactions = Container.get(RecognizeTranasctionsService); | ||
const revertRegonizedTransactions = Container.get( | ||
RevertRecognizedTransactions | ||
); | ||
|
||
try { | ||
await revertRegonizedTransactions.revertRecognizedTransactions( | ||
tenantId, | ||
ruleId, | ||
transactionsCriteria | ||
); | ||
await regonizeTransactions.recognizeTransactions( | ||
tenantId, | ||
ruleId, | ||
transactionsCriteria | ||
); | ||
done(); | ||
} catch (error) { | ||
console.log(error); | ||
done(error); | ||
} | ||
}; | ||
} |
38 changes: 38 additions & 0 deletions
38
.../server/src/services/Banking/RegonizeTranasctions/jobs/RevertRecognizedTransactionsJob.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Container, { Service } from 'typedi'; | ||
import { RevertRecognizedTransactions } from '../RevertRecognizedTransactions'; | ||
|
||
@Service() | ||
export class RevertRegonizeTransactionsJob { | ||
/** | ||
* Constructor method. | ||
*/ | ||
constructor(agenda) { | ||
agenda.define( | ||
'revert-recognized-uncategorized-transactions-job', | ||
{ priority: 'high', concurrency: 2 }, | ||
this.handler | ||
); | ||
} | ||
|
||
/** | ||
* Triggers sending invoice mail. | ||
*/ | ||
private handler = async (job, done: Function) => { | ||
const { tenantId, ruleId, transactionsCriteria } = job.attrs.data; | ||
const revertRegonizedTransactions = Container.get( | ||
RevertRecognizedTransactions | ||
); | ||
|
||
try { | ||
await revertRegonizedTransactions.revertRecognizedTransactions( | ||
tenantId, | ||
ruleId, | ||
transactionsCriteria | ||
); | ||
done(); | ||
} catch (error) { | ||
console.log(error); | ||
done(error); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.