From b57276c898ad3c39d13b18185e766655999c4430 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sun, 7 Oct 2018 20:00:53 +0100 Subject: [PATCH] Remove transactions (#23) --- src/lib/report.ts | 12 ++++++++++++ src/lib/transaction.ts | 4 +++- test/report.spec.ts | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/lib/report.ts b/src/lib/report.ts index 244d874..9692eb8 100644 --- a/src/lib/report.ts +++ b/src/lib/report.ts @@ -17,6 +17,7 @@ export interface Report { resetFilter(): void; addTransactions(transactions: Transaction[]): void; + removeTransactions(transactionIds: string[]): void; }; class ReportImpl implements Report { @@ -59,6 +60,13 @@ class ReportImpl implements Report { this.transactions = this.unfilteredTransactions.filter(txn => txn.calculatedMonth === this.reportFilter.month); } } + + removeTransactions(transactionsToRemove: string[]) { + this.unfilteredTransactions = this.unfilteredTransactions.filter(txn => ( + transactionsToRemove.indexOf(txn.identifier) === -1 + )); + this.applyFilter(); + } } export class ReportFactory { @@ -103,6 +111,10 @@ export class ReportFactory { return new Promise((resolve, reject) => resolve()); } + + removeRecords(transactionIdentifiers: string[]) { + this.report.removeTransactions(transactionIdentifiers); + } } export default ReportFactory; diff --git a/src/lib/transaction.ts b/src/lib/transaction.ts index 1a3bf43..63b8680 100644 --- a/src/lib/transaction.ts +++ b/src/lib/transaction.ts @@ -73,7 +73,9 @@ export class Transaction { this.calculatedMonth = this.calendarMonth; } - this.identifier = this.build_identifier(); + if (!this.identifier) { + this.identifier = this.build_identifier(); + } } txn_amount() { diff --git a/test/report.spec.ts b/test/report.spec.ts index ac95ba6..cfcf068 100644 --- a/test/report.spec.ts +++ b/test/report.spec.ts @@ -234,4 +234,27 @@ describe('ReportFactory', () => { expect(report.transactions[5].creditAmount).to.equal(0); }); }); + it ('can remove transactions', () => { + let rf = new ReportFactory(); + return rf.addRecords([{ + date: '2018-01-01', + identifier: 'a1', + }, { + date: '2018-01-01', + identifier: 'a2', + }, { + date: '2018-01-03', + identifier: 'a3', + }, { + date: '2018-01-04', + identifier: 'a4', + }]) + .then(() => { + rf.removeRecords(['a2','a3','b']) + }) + .then(() => { + expect(rf.report.transactions.map(t => t.identifier)) + .to.deep.equal(['a1','a4']) + }) + }) });