From ed04378aacb88f6b18d01634d8a2bd7dd7a1d002 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 13 Mar 2020 00:34:09 -0400 Subject: [PATCH] Add the ability to filter forms by Person. --- src/core/Form.ts | 5 ++++ src/core/Person.ts | 2 ++ src/core/TaxReturn.test.ts | 50 ++++++++++++++++++++++++++++++++++++++ src/core/TaxReturn.ts | 13 +++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/core/Form.ts b/src/core/Form.ts index 3d9d54b..95aeb54 100644 --- a/src/core/Form.ts +++ b/src/core/Form.ts @@ -1,3 +1,4 @@ +import Person from './Person'; import TaxReturn from './TaxReturn'; import { Line } from './Line'; import { InconsistencyError, NotFoundError } from './Errors'; @@ -28,6 +29,10 @@ export default abstract class Form }, } } + person(): Person | undefined { + return undefined; + } + getLine(id: K): L[K] { if (!(id in this._lines)) throw new NotFoundError(`Form ${this.name} does not have line ${id}`); diff --git a/src/core/Person.ts b/src/core/Person.ts index 5f0806c..092bece 100644 --- a/src/core/Person.ts +++ b/src/core/Person.ts @@ -8,6 +8,8 @@ export default class Person { private _name: string; private _relation: Relation; + static joint = Symbol('Joint') as unknown as Person; + constructor(name: string, relation: Relation) { this._name = name; this._relation = relation; diff --git a/src/core/TaxReturn.test.ts b/src/core/TaxReturn.test.ts index 21ca625..2b4522a 100644 --- a/src/core/TaxReturn.test.ts +++ b/src/core/TaxReturn.test.ts @@ -101,3 +101,53 @@ test('get non-existent form', () => { expect(tr.findForm(TestForm)).toBeNull(); expect(tr.findForms(TestForm)).toEqual([]); }); + +class PerPersonForm extends Form { + private _person?: Person; + + readonly name = 'Per Person'; + + readonly supportsMultipleCopies = true; + + protected readonly _lines = {}; + + constructor(person?: Person) { + super(undefined); + this._person = person; + } + + person() { return this._person; } +}; + +test('find forms for person', () => { + const p1 = Person.self('1'); + const p2 = Person.spouse('2'); + + const addFormsToTaxReturn = (tr) => { + tr.addForm(new PerPersonForm(undefined)); + tr.addForm(new PerPersonForm(undefined)); + tr.addForm(new PerPersonForm(p1)); + tr.addForm(new PerPersonForm(p2)); + tr.addForm(new PerPersonForm(p2)); + tr.addForm(new PerPersonForm(Person.joint)); + }; + + const mfsp1 = new TestTaxReturn(); + mfsp1.includeJointPersonForms = false; + mfsp1.addPerson(p1); + addFormsToTaxReturn(mfsp1); + expect(mfsp1.findForms(PerPersonForm).length).toBe(3); + + const mfsp2 = new TestTaxReturn(); + mfsp2.includeJointPersonForms = false; + mfsp2.addPerson(p2); + addFormsToTaxReturn(mfsp2); + expect(mfsp2.findForms(PerPersonForm).length).toBe(4); + + const mfj = new TestTaxReturn(); + mfj.includeJointPersonForms = true; + mfj.addPerson(p1); + mfj.addPerson(p2); + addFormsToTaxReturn(mfj); + expect(mfj.findForms(PerPersonForm).length).toBe(6); +}); diff --git a/src/core/TaxReturn.ts b/src/core/TaxReturn.ts index 359acab..6ddc0b7 100644 --- a/src/core/TaxReturn.ts +++ b/src/core/TaxReturn.ts @@ -56,7 +56,18 @@ export default abstract class TaxReturn { } findForms>(cls: FormClass): T[] { - const forms: T[] = this._forms.filter((form: Form): form is T => isFormT(form, cls)); + const forms: T[] = this._forms + .filter((form: Form): form is T => isFormT(form, cls)) + .filter((form: T) => { + const person = form.person(); + if (person === undefined) + return true; + + if (person == Person.joint && this.includeJointPersonForms) + return true; + + return this._people.includes(form.person()); + }); return forms; } -- 2.43.5