Skip to content

Commit

Permalink
Filter by type (#25)
Browse files Browse the repository at this point in the history
* add ability to filter by type

* fix bug with calculated month
  • Loading branch information
errietta committed May 4, 2019
1 parent 1bdbb80 commit aa23eee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
54 changes: 40 additions & 14 deletions src/lib/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CSVParserManager } from './manager/csvparsermanager';
export type ReportOptionsType = { unique?: boolean };
export type ReportFilter = {
month?: string,
type?: string,
};

export interface Report {
Expand All @@ -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 {
Expand All @@ -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;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
57 changes: 44 additions & 13 deletions test/report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -36,7 +36,7 @@ describe('ReportFactory', () => {
});

it('Correctly filters', () => {
let rf = new ReportFactory();
const rf = new ReportFactory();
return rf.fromRecords([
{
'date': '01/01/2017',
Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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' }]))
Expand All @@ -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');
Expand All @@ -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',
Expand All @@ -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');
});
});
});

0 comments on commit aa23eee

Please sign in to comment.