Add the ability to filter forms by Person.
authorRobert Sesek <rsesek@bluestatic.org>
Fri, 13 Mar 2020 04:34:09 +0000 (00:34 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Fri, 13 Mar 2020 04:34:09 +0000 (00:34 -0400)
src/core/Form.ts
src/core/Person.ts
src/core/TaxReturn.test.ts
src/core/TaxReturn.ts

index 3d9d54b928d6796642a5a7efc2c0bfe2ba8088ab..95aeb542b970ba28ac0121d62025fbab8be59f6e 100644 (file)
@@ -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<L extends { [key: string]: Line<any> },
     }
   }
 
+  person(): Person | undefined {
+    return undefined;
+  }
+
   getLine<K extends keyof L>(id: K): L[K] {
     if (!(id in this._lines))
       throw new NotFoundError(`Form ${this.name} does not have line ${id}`);
index 5f0806c233ab62233125c513cb5040961d63b45d..092bece797ad06fbbeafdcc3f41b21b801dbf776 100644 (file)
@@ -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;
index 21ca625165d89941963b40d3b711eb8f0fe80ae7..2b4522aa8e098d61ecd2ea7ad8866211665d4027 100644 (file)
@@ -101,3 +101,53 @@ test('get non-existent form', () => {
   expect(tr.findForm(TestForm)).toBeNull();
   expect(tr.findForms(TestForm)).toEqual([]);
 });
+
+class PerPersonForm extends Form<PerPersonForm['_lines']> {
+  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);
+});
index 359acab3f5921da647c62a0826f0b196ae0d3ce3..6ddc0b74b712febcb17e546f50f68cb98afd2550 100644 (file)
@@ -56,7 +56,18 @@ export default abstract class TaxReturn {
   }
 
   findForms<T extends Form<any>>(cls: FormClass<T>): T[] {
-    const forms: T[] = this._forms.filter((form: Form<any>): form is T => isFormT(form, cls));
+    const forms: T[] = this._forms
+        .filter((form: Form<any>): 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;
   }