From aa23eeec727ea6e9ce9a22b99029ea116299f288 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sun, 5 May 2019 00:10:09 +0100 Subject: [PATCH] Filter by type (#25) * add ability to filter by type * fix bug with calculated month --- src/lib/report.ts | 54 ++++++++++++++++++++++++++++----------- src/lib/transaction.ts | 5 +--- test/report.spec.ts | 57 ++++++++++++++++++++++++++++++++---------- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/src/lib/report.ts b/src/lib/report.ts index 9692eb8..9246ca1 100644 --- a/src/lib/report.ts +++ b/src/lib/report.ts @@ -4,6 +4,7 @@ import { CSVParserManager } from './manager/csvparsermanager'; export type ReportOptionsType = { unique?: boolean }; export type ReportFilter = { month?: string, + type?: string, }; export interface Report { @@ -12,12 +13,13 @@ export interface Report { unfilteredTransactions: Transaction[]; filter(filter: ReportFilter): void; - filterMonth(month: string): void; - applyFilter(): void; - resetFilter(): void; + filterMonth(month: string): Report; + filterType(type: string): Report; + applyFilter(): Report; + resetFilter(): Report; - addTransactions(transactions: Transaction[]): void; - removeTransactions(transactionIds: string[]): void; + addTransactions(transactions: Transaction[]): Report; + removeTransactions(transactionIds: string[]): Report; }; class ReportImpl implements Report { @@ -34,38 +36,62 @@ class ReportImpl implements Report { this.reportFilter = {}; } - addTransactions(transactions: Transaction[]) { + addTransactions(transactions: Transaction[]): Report { this.unfilteredTransactions = this.unfilteredTransactions.concat(transactions); this.applyFilter(); + + return this; } - resetFilter(): void { + resetFilter(): Report { this.transactions = [...this.unfilteredTransactions]; + return this; } - filter(filter: ReportFilter): void { - this.reportFilter = filter; - this.applyFilter(); + filter(filter: ReportFilter): Report { + this.reportFilter = { + ...this.reportFilter, + ...filter + }; + + return this.applyFilter(); } - filterMonth(month: string): void { + filterMonth(month: string): Report { this.filter({ month: month }); + + return this; + } + + filterType(type: string): Report { + this.filter({ type: type }); + + return this; } - applyFilter(): void { + applyFilter(): Report { this.resetFilter(); if (this.reportFilter.month) { this.transactionsInCalendarMonth = this.unfilteredTransactions.filter(txn => txn.calendarMonth === this.reportFilter.month); - this.transactions = this.unfilteredTransactions.filter(txn => txn.calculatedMonth === this.reportFilter.month); + this.transactions = this.transactions.filter(txn => txn.calculatedMonth === this.reportFilter.month); + } + if (this.reportFilter.type) { + this.transactions = this.transactions.filter(txn => ( + txn.type.toLowerCase() === this.reportFilter.type.toLowerCase() + )); } + + return this; } - removeTransactions(transactionsToRemove: string[]) { + removeTransactions(transactionsToRemove: string[]): Report { this.unfilteredTransactions = this.unfilteredTransactions.filter(txn => ( transactionsToRemove.indexOf(txn.identifier) === -1 )); this.applyFilter(); + + return this; } } diff --git a/src/lib/transaction.ts b/src/lib/transaction.ts index ba9312b..29b0686 100644 --- a/src/lib/transaction.ts +++ b/src/lib/transaction.ts @@ -68,10 +68,7 @@ export class Transaction { }); this.calendarMonth = moment(this.date).utc().format('YYYYMM'); - - if (!this.calculatedMonth) { - this.calculatedMonth = this.calendarMonth; - } + this.calculatedMonth = this.calendarMonth; if (!this.identifier) { this.identifier = this.build_identifier(); diff --git a/test/report.spec.ts b/test/report.spec.ts index cfcf068..d497f45 100644 --- a/test/report.spec.ts +++ b/test/report.spec.ts @@ -13,7 +13,7 @@ describe('ReportFactory', () => { }); it('creates report objects', () => { - let rf = new ReportFactory(); + const rf = new ReportFactory(); return rf.fromRecords([{ 'date': '01/01/2017', @@ -22,12 +22,12 @@ describe('ReportFactory', () => { 'accountBalance': '1000', 'description': 'Some Transaction', }]).then(() => { - let report = rf.report; + const report = rf.report; assert.ok(report); expect(report).to.have.property('transactions').with.lengthOf(1); - let transaction = report.transactions[0]; + const transaction = report.transactions[0]; assert.ok(transaction); expect(transaction).to.have.property('creditAmount'); expect(transaction.creditAmount).to.equal(1000); @@ -36,7 +36,7 @@ describe('ReportFactory', () => { }); it('Correctly filters', () => { - let rf = new ReportFactory(); + const rf = new ReportFactory(); return rf.fromRecords([ { 'date': '01/01/2017', @@ -60,7 +60,7 @@ describe('ReportFactory', () => { 'description': 'Second September Transaction', } ]).then(() => { - let report = rf.report; + const report = rf.report; report.filterMonth('201709'); expect(report).to.have.property('transactions').with.lengthOf(2); @@ -85,7 +85,7 @@ describe('ReportFactory', () => { 'description': 'January 2018 Transaction', }]); }).then(() => { - let report = rf.report; + const report = rf.report; expect(report).to.have.property('transactions').with.lengthOf(1); expect(report.transactions[0].description).to.equal('January 2018 Transaction'); @@ -123,7 +123,7 @@ describe('ReportFactory', () => { description: 'January Transaction', } ]).then(() => { - let report = rf.report; + const report = rf.report; expect(report).to.have.property('transactions').with.lengthOf(3); expect(moment(report.transactions[0].date).isSame(moment('2017-01-01T00:00:00Z'))).to.be.true; expect(moment(report.transactions[1].date).isSame(moment('2017-01-01T00:00:00Z'))).to.be.true; @@ -150,7 +150,7 @@ describe('ReportFactory', () => { description: 'January Transaction', } ]).then(() => { - let report = rf.report; + const report = rf.report; expect(report).to.have.property('transactions').with.lengthOf(2); expect(moment(report.transactions[0].date).isSame(moment('2017-01-01T00:00:00Z'))).to.be.true; @@ -191,7 +191,7 @@ describe('ReportFactory', () => { }, ]) }).then(() => { - let report = rf.report; + const report = rf.report; expect(report).to.have.property('transactions').with.lengthOf(2); expect(report.transactions[0].accountBalance).to.equal(1000); expect(report.transactions[1].accountBalance).to.equal(2000); @@ -201,7 +201,7 @@ describe('ReportFactory', () => { }); it ('can combine many sources', () => { - let rf = new ReportFactory(); + const rf = new ReportFactory(); return rf.addRecords([{ 'date': '01/01/2017', 'creditAmount': 0, 'debitAmount': 1000, 'description': 'Desc 1' }]) .then(() => rf.addRecords([{ 'date': '01/01/2017', 'creditAmount': 0, 'debitAmount': 1000, 'description': 'Desc 2' }])) @@ -217,7 +217,7 @@ describe('ReportFactory', () => { ` , 'lloyds')) .then(() => { - let report = rf.report; + const report = rf.report; expect(report).to.have.property('transactions').with.lengthOf(6); expect(report.transactions[0].description).to.equal('Desc 1'); @@ -235,7 +235,7 @@ describe('ReportFactory', () => { }); }); it ('can remove transactions', () => { - let rf = new ReportFactory(); + const rf = new ReportFactory(); return rf.addRecords([{ date: '2018-01-01', identifier: 'a1', @@ -256,5 +256,36 @@ describe('ReportFactory', () => { expect(rf.report.transactions.map(t => t.identifier)) .to.deep.equal(['a1','a4']) }) - }) + }); + it ('can filter by type', () => { + const rf = new ReportFactory(); + return rf.addRecords([{ + date: '2018-01-01T05:00:00Z', + type: 'DD', + description: 'TXN 1', + }, + { + date: '2018-01-01T05:00:00Z', + type: 'DEB', + description: 'TXN2', + }, + { + date: '2018-02-01T05:00:00Z', + type: 'DD', + description: 'TXN3', + }]).then(() => { + const report = rf.report; + report.filterType('dd'); + + expect(report.transactions.length).to.equal(2); + expect(report.transactions[0].description).to.equal('TXN 1'); + expect(report.transactions[1].description).to.equal('TXN3'); + + report.resetFilter(); + report.filterType('DD').filterMonth('201801'); + + expect(report.transactions.length).to.equal(1); + expect(report.transactions[0].description).to.equal('TXN 1'); + }); + }); });