@finsys/core 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -9200,6 +9200,10 @@ var FileFieldTableType = /* @__PURE__ */ ((FileFieldTableType2) => {
9200
9200
  FileFieldTableType2["KEY_VALUE"] = "keyValue";
9201
9201
  return FileFieldTableType2;
9202
9202
  })(FileFieldTableType || {});
9203
+ var IHS_FIELD_ORIGINS = ["extracted", "derived", "manual"];
9204
+ function isValidIhsFieldOrigin(origin) {
9205
+ return typeof origin === "string" && IHS_FIELD_ORIGINS.includes(origin);
9206
+ }
9203
9207
 
9204
9208
  // src/document-types.ts
9205
9209
  var cached = null;
@@ -9597,6 +9601,124 @@ function buildFileFieldTables(ihsData, fieldProvenance) {
9597
9601
  }
9598
9602
  return tables;
9599
9603
  }
9604
+ function instanceColumnLabel(row) {
9605
+ const period = row.timePeriod || row.instanceKey;
9606
+ return row.sourceLabel ? `${period} \xB7 ${row.sourceLabel}` : period;
9607
+ }
9608
+ function instanceColumnLabels(rows) {
9609
+ const seenCounts = /* @__PURE__ */ new Map();
9610
+ return rows.map((row) => {
9611
+ const raw = instanceColumnLabel(row);
9612
+ const occurrence = (seenCounts.get(raw) ?? 0) + 1;
9613
+ seenCounts.set(raw, occurrence);
9614
+ return occurrence === 1 ? raw : `${raw} (${occurrence})`;
9615
+ });
9616
+ }
9617
+ function groupColumnsByInstance(baseColumnNames, instanceRows) {
9618
+ const groups = {};
9619
+ for (const baseName of baseColumnNames) {
9620
+ groups[baseName] = {};
9621
+ }
9622
+ if (!instanceRows?.length) return groups;
9623
+ const labels = instanceColumnLabels(instanceRows);
9624
+ instanceRows.forEach((row, i) => {
9625
+ const label = labels[i];
9626
+ for (const baseName of baseColumnNames) {
9627
+ if (Object.prototype.hasOwnProperty.call(row, baseName)) {
9628
+ groups[baseName][label] = row[baseName];
9629
+ }
9630
+ }
9631
+ });
9632
+ return groups;
9633
+ }
9634
+ function buildInstanceTable(groupName, baseNames, groupDisplayName, instanceRows, fieldProvenance) {
9635
+ const columnGroups = groupColumnsByInstance(baseNames, instanceRows);
9636
+ const instanceLabels = instanceColumnLabels(instanceRows);
9637
+ const items = [];
9638
+ for (const [baseName, labelMap] of Object.entries(columnGroups)) {
9639
+ const hasAny = Object.values(labelMap).some((v) => v !== null && v !== void 0 && v !== "");
9640
+ if (!hasAny) continue;
9641
+ const numeric = isNumericField(baseName);
9642
+ const data = {};
9643
+ const formattedData = {};
9644
+ const confidence = {};
9645
+ const provenance = {};
9646
+ for (const [i, row] of instanceRows.entries()) {
9647
+ const label = instanceLabels[i];
9648
+ const value = labelMap[label] ?? null;
9649
+ data[label] = value;
9650
+ formattedData[label] = formatValue(value, numeric);
9651
+ const legacyKey = row.timePeriod ? `${baseName}${row.timePeriod}` : void 0;
9652
+ const prov = legacyKey ? fieldProvenance?.[legacyKey] : void 0;
9653
+ if (prov) {
9654
+ provenance[label] = prov;
9655
+ if (prov.origin === "extracted" && typeof prov.confidence === "number" && !Number.isNaN(prov.confidence)) {
9656
+ confidence[label] = prov.confidence;
9657
+ }
9658
+ }
9659
+ }
9660
+ items.push({
9661
+ displayName: getDisplayName(baseName),
9662
+ timePeriods: instanceLabels,
9663
+ data,
9664
+ formattedData,
9665
+ type: "timeSeries" /* TIME_SERIES */,
9666
+ isNumeric: numeric,
9667
+ ...Object.keys(confidence).length ? { confidence } : {},
9668
+ ...Object.keys(provenance).length ? { provenance } : {}
9669
+ });
9670
+ }
9671
+ const hasData = items.some(
9672
+ (item) => Object.values(item.data).some((v) => v !== null && v !== void 0 && v !== "")
9673
+ );
9674
+ if (!items.length || !hasData) return null;
9675
+ return {
9676
+ name: groupName,
9677
+ displayName: groupDisplayName,
9678
+ type: "timeSeries" /* TIME_SERIES */,
9679
+ items,
9680
+ hasData
9681
+ };
9682
+ }
9683
+ function buildFileFieldTablesFromInstances(instancesByCategory = {}, fieldProvenance, categoryOverrides) {
9684
+ const specs = getBaseFieldSpecs();
9685
+ const fileFields = specs.filter((f) => f.type === "file" && f.ihs_column_names?.length);
9686
+ const grouped = groupFieldsByPattern(fileFields);
9687
+ const tables = {};
9688
+ for (const [groupName, fields] of Object.entries(grouped)) {
9689
+ const instanceRows = instancesByCategory[groupName];
9690
+ if (!instanceRows?.length) continue;
9691
+ const baseNames = /* @__PURE__ */ new Set();
9692
+ for (const field of fields) {
9693
+ for (const col of field.ihs_column_names ?? []) {
9694
+ baseNames.add(col.replace(TIME_PERIOD_REGEX, ""));
9695
+ }
9696
+ }
9697
+ const groupDisplayName = getGroupDisplayNames()[groupName] || fields[0]?.displayName || getDisplayName(groupName);
9698
+ const table = buildInstanceTable(
9699
+ groupName,
9700
+ [...baseNames],
9701
+ groupDisplayName,
9702
+ instanceRows,
9703
+ fieldProvenance
9704
+ );
9705
+ if (table) tables[groupName] = table;
9706
+ }
9707
+ for (const [groupName, spec] of Object.entries(categoryOverrides ?? {})) {
9708
+ if (Object.prototype.hasOwnProperty.call(tables, groupName)) continue;
9709
+ const instanceRows = instancesByCategory[groupName];
9710
+ if (!instanceRows?.length) continue;
9711
+ const table = buildInstanceTable(
9712
+ groupName,
9713
+ spec.baseColumnNames,
9714
+ spec.displayName,
9715
+ instanceRows,
9716
+ fieldProvenance
9717
+ );
9718
+ if (table) tables[groupName] = table;
9719
+ }
9720
+ return tables;
9721
+ }
9600
9722
  var DOC_DISPLAY_NAMES = {
9601
9723
  bankStatements: "Bank Statements",
9602
9724
  financialStatements: "Financial Statements",
@@ -9849,6 +9971,7 @@ var IhsStatus = /* @__PURE__ */ ((IhsStatus2) => {
9849
9971
  IhsStatus2["CreatingApplication"] = "CREATING_APPLICATION";
9850
9972
  IhsStatus2["ApplicationFinalized"] = "APPLICATION_FINALIZED";
9851
9973
  IhsStatus2["LenderEvaluation"] = "LENDER_EVALUATION";
9974
+ IhsStatus2["EditingApplication"] = "EDITING_APPLICATION";
9852
9975
  IhsStatus2["Approved"] = "APPROVED";
9853
9976
  IhsStatus2["LouDelivered"] = "LOU_DELIVERED";
9854
9977
  IhsStatus2["AwaitingDisbursement"] = "AWAITING_DISBURSEMENT";
@@ -9862,6 +9985,7 @@ var IHS_VALID_STATUSES = [
9862
9985
  "CREATING_APPLICATION" /* CreatingApplication */,
9863
9986
  "APPLICATION_FINALIZED" /* ApplicationFinalized */,
9864
9987
  "LENDER_EVALUATION" /* LenderEvaluation */,
9988
+ "EDITING_APPLICATION" /* EditingApplication */,
9865
9989
  "APPROVED" /* Approved */,
9866
9990
  "LOU_DELIVERED" /* LouDelivered */,
9867
9991
  "AWAITING_DISBURSEMENT" /* AwaitingDisbursement */,
@@ -10627,6 +10751,7 @@ export {
10627
10751
  FormFieldValidator,
10628
10752
  FormSpec,
10629
10753
  IHS_FAILURE_STATUSES,
10754
+ IHS_FIELD_ORIGINS,
10630
10755
  IHS_TERMINAL_STATUSES,
10631
10756
  IHS_VALID_STATUSES,
10632
10757
  IhsStatus,
@@ -10639,6 +10764,7 @@ export {
10639
10764
  assertDocumentType,
10640
10765
  buildDocumentRows,
10641
10766
  buildFileFieldTables,
10767
+ buildFileFieldTablesFromInstances,
10642
10768
  categoryFieldsOf,
10643
10769
  categoryForField,
10644
10770
  categorySchemaOf,
@@ -10665,6 +10791,7 @@ export {
10665
10791
  getReuploadableDocTypes,
10666
10792
  getStepDefaultValues,
10667
10793
  getStepSchema,
10794
+ groupColumnsByInstance,
10668
10795
  groupColumnsByTimePeriod,
10669
10796
  groupDetailsByCategory,
10670
10797
  groupFieldsByCategory,
@@ -10673,6 +10800,7 @@ export {
10673
10800
  isDocumentType,
10674
10801
  isFailureIhsStatus,
10675
10802
  isTerminalIhsStatus,
10803
+ isValidIhsFieldOrigin,
10676
10804
  isValidIhsStatus,
10677
10805
  parseFileField,
10678
10806
  processIhsDetails,