All files / journal-processor/domain journal-entry-mapper.ts

100% Statements 53/53
93.75% Branches 15/16
100% Functions 10/10
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                        1x   9x     87x 87x 87x       30x 9x       45x 45x 45x 171x 82x   89x       45x         79x 154x 154x 80x 74x 72x   2x           51x 51x 51x   51x 147x 147x 147x 147x   147x 57x           57x           57x 57x 90x 36x 36x           36x           36x 36x   54x           54x           54x 54x       51x       34x 34x 34x 34x       4x 17x            
import { autoInjectable } from "tsyringe";
 
import {
  absolute,
  equal,
  lessThan,
  negate,
  plus,
} from "../../tr8-script/domain/utils/math.js";
import { JournalEntry, JournalTxLog } from "./journal-events.js";
 
@autoInjectable()
class JournalEntryMapper {
  group(journals: JournalTxLog[]): JournalTxLog[][] {
    const grouped: Record<string, JournalTxLog[]> = journals.reduce(
      (acc, j) => {
        // Group by both id and subId to separate events from different subIds
        const groupKey = j.subId ? `${j.id}-${j.subId}` : j.id;
        acc[groupKey] = [...(acc[groupKey] ?? []), j];
        return acc;
      },
      {} as Record<string, JournalTxLog[]>,
    );
    const result = Object.entries(grouped).map(([_, journals]) => journals);
    return result;
  }
 
  split(journals: JournalTxLog[]): [JournalTxLog[], JournalTxLog[]] {
    const deducted: JournalTxLog[] = [];
    const added: JournalTxLog[] = [];
    for (const j of journals) {
      if (lessThan(j.change, { amount: 0n, scale: 0n })) {
        deducted.push(j);
      } else {
        added.push(j);
      }
    }
 
    return [deducted, added];
  }
 
  sort(journals: JournalTxLog[]): JournalTxLog[] {
    // Sort journals by absolute value of change
    return journals.sort((a, b) => {
      const [a1, b1] = [absolute(a.change), absolute(b.change)];
      if (lessThan(a1, b1)) {
        return -1;
      } else if (lessThan(b1, a1)) {
        return 1;
      } else {
        return 0;
      }
    });
  }
 
  convert(deficit: JournalTxLog[], surplus: JournalTxLog[]): JournalEntry[] {
    const ret: JournalEntry[] = [];
    let dp = 0;
    let sp = 0;
 
    while (dp < deficit.length && sp < surplus.length) {
      const d = deficit[dp]!;
      const s = surplus[sp]!;
      const dc = negate(d.change);
      const sc = s.change;
 
      if (equal(dc, sc)) {
        ret.push({
          type: "CREDIT",
          amount: s.change,
          book: d.book,
          account: d.account,
        });
        ret.push({
          type: "DEBIT",
          amount: s.change,
          book: s.book,
          account: s.account,
        });
        dp++;
        sp++;
      } else if (lessThan(dc, sc)) {
        const change = dc;
        ret.push({
          type: "CREDIT",
          amount: change,
          book: d.book,
          account: d.account,
        });
        ret.push({
          type: "DEBIT",
          amount: change,
          book: s.book,
          account: s.account,
        });
        surplus[sp]!.change = plus(s.change, d.change);
        dp++;
      } else {
        ret.push({
          type: "CREDIT",
          amount: s.change,
          book: d.book,
          account: d.account,
        });
        ret.push({
          type: "DEBIT",
          amount: s.change,
          book: s.book,
          account: s.account,
        });
        deficit[dp]!.change = plus(d.change, s.change);
        sp++;
      }
    }
 
    return ret;
  }
 
  mapEntrySingle(journals: JournalTxLog[]): JournalEntry[] {
    const [deducted, added] = this.split(journals);
    const deficit = this.sort(deducted);
    const surplus = this.sort(added);
    return this.convert(deficit, surplus);
  }
 
  map(journals: JournalTxLog[]): JournalEntry[] {
    return this.group(journals)
      .map((x) => this.mapEntrySingle(x))
      .flat();
  }
}
 
export { JournalEntryMapper };