@htilabs/platform-common-models 0.85.0 → 0.87.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.
Files changed (64) hide show
  1. package/README.md +30 -0
  2. package/dist/{index-XWSYon40.d.cts → index-1pWiqEBC.d.cts} +1 -1
  3. package/dist/{index-Bh1zO_Vu.d.ts → index-uz57Fic9.d.ts} +1 -1
  4. package/dist/index.cjs +12 -1
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.cts +3 -3
  7. package/dist/index.d.ts +3 -3
  8. package/dist/index.js +12 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/models/index.d.cts +2 -2
  11. package/dist/models/index.d.ts +2 -2
  12. package/dist/org-directory/index.cjs +8 -2
  13. package/dist/org-directory/index.cjs.map +1 -1
  14. package/dist/org-directory/index.d.cts +11 -2
  15. package/dist/org-directory/index.d.ts +11 -2
  16. package/dist/org-directory/index.js +8 -2
  17. package/dist/org-directory/index.js.map +1 -1
  18. package/dist/org-merge/index.cjs +8 -2
  19. package/dist/org-merge/index.cjs.map +1 -1
  20. package/dist/org-merge/index.js +8 -2
  21. package/dist/org-merge/index.js.map +1 -1
  22. package/dist/points-of-contact/index.cjs +229 -24
  23. package/dist/points-of-contact/index.cjs.map +1 -1
  24. package/dist/points-of-contact/index.d.cts +50 -5
  25. package/dist/points-of-contact/index.d.ts +50 -5
  26. package/dist/points-of-contact/index.js +226 -23
  27. package/dist/points-of-contact/index.js.map +1 -1
  28. package/dist/reader-BmhzOis-.d.ts +273 -0
  29. package/dist/reader-DfQtdw6n.d.cts +273 -0
  30. package/dist/schemas/index.cjs +12 -1
  31. package/dist/schemas/index.cjs.map +1 -1
  32. package/dist/schemas/index.d.cts +57 -27
  33. package/dist/schemas/index.d.ts +57 -27
  34. package/dist/schemas/index.js +12 -1
  35. package/dist/schemas/index.js.map +1 -1
  36. package/dist/seat-logs/index.cjs +815 -0
  37. package/dist/seat-logs/index.cjs.map +1 -0
  38. package/dist/seat-logs/index.d.cts +274 -0
  39. package/dist/seat-logs/index.d.ts +274 -0
  40. package/dist/seat-logs/index.js +742 -0
  41. package/dist/seat-logs/index.js.map +1 -0
  42. package/dist/validation/index.d.cts +1 -1
  43. package/dist/validation/index.d.ts +1 -1
  44. package/package.json +6 -1
  45. package/src/orgDirectory/__tests__/programDetails.test.ts +2 -1
  46. package/src/orgDirectory/__tests__/programTeamLink.test.ts +107 -0
  47. package/src/orgDirectory/schemas.ts +8 -1
  48. package/src/pointsOfContact/__tests__/seatContactLog.test.ts +170 -0
  49. package/src/pointsOfContact/readRules.ts +22 -4
  50. package/src/pointsOfContact/reader.ts +95 -1
  51. package/src/pointsOfContact/schemas.ts +4 -3
  52. package/src/schemas/organization-program.ts +25 -2
  53. package/src/seatLogs/__tests__/seatLogs.review.test.ts +222 -0
  54. package/src/seatLogs/__tests__/seatLogs.test.ts +335 -0
  55. package/src/seatLogs/index.ts +15 -0
  56. package/src/seatLogs/log.ts +700 -0
  57. package/src/seatLogs/personId.ts +100 -0
  58. package/src/seatLogs/personSlugWordlists.ts +40 -0
  59. package/src/seatLogs/reader.ts +143 -0
  60. package/src/seatLogs/schemas.ts +118 -0
  61. package/dist/{agency-CnVTwLeV.d.ts → agency-BHv54o79.d.ts} +4 -4
  62. package/dist/{agency-8ufZJJDH.d.cts → agency-Djp57S71.d.cts} +4 -4
  63. package/dist/{cvcEntry-D787utvP.d.cts → cvcEntry-DAuhe0cd.d.cts} +8 -8
  64. package/dist/{cvcEntry-D787utvP.d.ts → cvcEntry-DAuhe0cd.d.ts} +8 -8
@@ -87,9 +87,11 @@ __export(pointsOfContact_exports, {
87
87
  preferredEmail: () => preferredEmail,
88
88
  preferredPhone: () => preferredPhone,
89
89
  resolveMagicLinkRecipients: () => resolveMagicLinkRecipients,
90
+ seatContactListsFromLog: () => seatContactListsFromLog,
90
91
  toNetworkView: () => toNetworkView,
91
92
  toOwnerView: () => toOwnerView,
92
- toStewardView: () => toStewardView
93
+ toStewardView: () => toStewardView,
94
+ withSeatContactLogs: () => withSeatContactLogs
93
95
  });
94
96
  module.exports = __toCommonJS(pointsOfContact_exports);
95
97
 
@@ -538,10 +540,17 @@ function reachOf(d, seat) {
538
540
  if (!d.user_id) return { emails: copyEntries(d.emails), phones: copyEntries(d.phones) };
539
541
  const one = (v) => v ? [{ value: v, is_preferred: true }] : [];
540
542
  return {
541
- emails: one(seat?.email?.trim().toLowerCase()),
542
- phones: one(seat?.phone?.trim())
543
+ emails: seat?.emails ? copyEntries(seat.emails) : one(seat?.email?.trim().toLowerCase()),
544
+ phones: seat?.phones ? copyEntries(seat.phones) : one(seat?.phone?.trim())
543
545
  };
544
546
  }
547
+ function copySeat(seat) {
548
+ if (!seat) return null;
549
+ const out = { ...seat };
550
+ if (seat.emails) out.emails = copyEntries(seat.emails);
551
+ if (seat.phones) out.phones = copyEntries(seat.phones);
552
+ return out;
553
+ }
545
554
  var preferredValue = (list) => preferredContactEntry(list)?.value ?? null;
546
555
  function toStewardView(d, seat) {
547
556
  const { identity_key: _internal, ...rest } = d;
@@ -553,7 +562,7 @@ function toStewardView(d, seat) {
553
562
  purposes: [...d.purposes],
554
563
  urgent_methods: [...d.urgent_methods],
555
564
  purpose_labels: d.purposes.map(contactPurposeLabel),
556
- seat: d.user_id ? seat ?? null : null,
565
+ seat: d.user_id ? copySeat(seat) : null,
557
566
  email: preferredValue(reach.emails),
558
567
  phone: preferredValue(reach.phones)
559
568
  };
@@ -603,7 +612,195 @@ function byContactReadingOrder(a, b) {
603
612
  return SUBJECT_ORDER[a.subject_type] - SUBJECT_ORDER[b.subject_type] || a.subject_id.localeCompare(b.subject_id) || Number(b.is_primary) - Number(a.is_primary) || (a.display_name ?? "").localeCompare(b.display_name ?? "") || a.id.localeCompare(b.id);
604
613
  }
605
614
 
615
+ // src/seatLogs/schemas.ts
616
+ var import_zod2 = require("zod");
617
+
618
+ // src/generated/cvc-enums.ts
619
+ var NameTypeKeys = {
620
+ missingness_root: "001-000-000-000-000-000-000",
621
+ unknown: "001-000-000-000-000-000-001",
622
+ anonymous: "001-000-000-000-000-000-002",
623
+ data_not_collected: "001-000-000-000-000-000-003",
624
+ alias: "001-001-000-000-000-000-000",
625
+ code_name: "001-002-000-000-000-000-000",
626
+ handle_screenname: "001-003-000-000-000-000-000",
627
+ legal: "001-004-000-000-000-000-000",
628
+ maiden_former: "001-005-000-000-000-000-000",
629
+ nickname: "001-006-000-000-000-000-000",
630
+ street_name: "001-007-000-000-000-000-000",
631
+ trade_name_stage_name: "001-008-000-000-000-000-000"
632
+ };
633
+
634
+ // src/seatLogs/schemas.ts
635
+ var SEAT_CONTACTS_COLLECTION = "seat_contacts";
636
+ var SEAT_LOG_SUBJECT_TYPES = ["user", "person"];
637
+ var SEAT_CONTACT_TYPES = ["email", "phone"];
638
+ var SEAT_NAME_TYPES = Object.keys(NameTypeKeys);
639
+ var SeatLogEventSchema = import_zod2.z.object({
640
+ /** `add`, `set_display`, `set_preferred`, `archive`, or `cascade` (a sibling's flag moved). */
641
+ act: import_zod2.z.string().min(1),
642
+ /** ISO instant. */
643
+ at: import_zod2.z.string().min(1),
644
+ /** The acting seat's id. */
645
+ actor: import_zod2.z.string()
646
+ });
647
+ var DayOrEmpty = import_zod2.z.union([import_zod2.z.literal(""), import_zod2.z.string().regex(/^\d{4}-\d{2}-\d{2}$/)]);
648
+ var subjectShape = {
649
+ entry_id: import_zod2.z.string().min(1),
650
+ subject_id: import_zod2.z.string().min(1),
651
+ subject_type: import_zod2.z.enum(SEAT_LOG_SUBJECT_TYPES),
652
+ period_start: DayOrEmpty,
653
+ period_end: DayOrEmpty,
654
+ /** Where the entry came from (`admin_hub` on the profile). */
655
+ source: import_zod2.z.string(),
656
+ /** How it was captured (`direct_entry` on the profile). */
657
+ method: import_zod2.z.string(),
658
+ notes: import_zod2.z.string(),
659
+ events: import_zod2.z.array(SeatLogEventSchema).optional()
660
+ };
661
+ var SeatNameEntrySchema = import_zod2.z.object({
662
+ ...subjectShape,
663
+ name_type: import_zod2.z.enum(SEAT_NAME_TYPES),
664
+ /** The name as written. */
665
+ raw: import_zod2.z.string().min(1),
666
+ /** The display name: one live entry per subject carries it. */
667
+ is_display: import_zod2.z.boolean(),
668
+ /** Set with the display mark on the profile (prototype acts both together). */
669
+ is_preferred: import_zod2.z.boolean()
670
+ });
671
+ var SeatContactEntrySchema = import_zod2.z.object({
672
+ ...subjectShape,
673
+ contact_type: import_zod2.z.enum(SEAT_CONTACT_TYPES),
674
+ /** Email trimmed; phone in the store's canonical display form ({@link canonSeatContactValue}). */
675
+ value: import_zod2.z.string().min(1),
676
+ use: import_zod2.z.string(),
677
+ /** The preferred entry of its type: one live entry per subject and type carries it. */
678
+ is_preferred: import_zod2.z.boolean(),
679
+ safety_status: import_zod2.z.string(),
680
+ /** Phone only: the identity key ({@link seatContactKey}), stamped by the store. */
681
+ digits: import_zod2.z.string().optional()
682
+ });
683
+
684
+ // src/seatLogs/log.ts
685
+ var isLive = (e) => !e.period_end;
686
+ function liveSeatContacts(entries) {
687
+ return entries.filter(isLive);
688
+ }
689
+ function preferredSeatContact(entries, type) {
690
+ const live = liveSeatContacts(entries).filter((e) => e.contact_type === type);
691
+ return live.find((e) => e.is_preferred) ?? live[0] ?? null;
692
+ }
693
+
694
+ // src/seatLogs/reader.ts
695
+ var SeatLogShapeError = class extends Error {
696
+ constructor(entryId, issues) {
697
+ super(`seat-logs: stored entry ${entryId ?? "(no id)"} does not match the contract: ${issues.join("; ")}`);
698
+ this.entryId = entryId;
699
+ this.issues = issues;
700
+ this.name = "SeatLogShapeError";
701
+ }
702
+ };
703
+ function strip(r) {
704
+ if (r === null || typeof r !== "object") return {};
705
+ const { _id: _ignored, ...rest } = r;
706
+ return rest;
707
+ }
708
+ function parseWith(schema, raw) {
709
+ const row = strip(raw);
710
+ const p = schema.safeParse(row);
711
+ if (p.success) return p.data;
712
+ throw new SeatLogShapeError(
713
+ typeof row.entry_id === "string" ? row.entry_id : null,
714
+ p.error.issues.map((i) => `${i.path.join(".") || "(row)"}: ${i.message}`)
715
+ );
716
+ }
717
+ var idForms = (id) => [.../* @__PURE__ */ new Set([id, id.toLowerCase()])];
718
+ var byEntryId = (a, b) => a.entry_id < b.entry_id ? -1 : a.entry_id > b.entry_id ? 1 : 0;
719
+ async function readLogs(db, collection, subjectType, subjectIds, parse, opts) {
720
+ const ids = [...new Set(subjectIds.filter((s) => typeof s === "string" && s))];
721
+ const out = /* @__PURE__ */ new Map();
722
+ if (!ids.length) return out;
723
+ const askedByKey = /* @__PURE__ */ new Map();
724
+ for (const id of ids) {
725
+ const k = id.toLowerCase();
726
+ askedByKey.set(k, [...askedByKey.get(k) ?? [], id]);
727
+ }
728
+ const raw = await db.collection(collection).find({ subject_type: subjectType, subject_id: { $in: ids.flatMap(idForms) } }).toArray();
729
+ for (const r of raw) {
730
+ let e;
731
+ try {
732
+ e = parse(r);
733
+ } catch (err) {
734
+ if (opts.invalid !== "skip" || !(err instanceof SeatLogShapeError)) throw err;
735
+ opts.onInvalidRow?.(err);
736
+ continue;
737
+ }
738
+ for (const asked of askedByKey.get(e.subject_id.toLowerCase()) ?? []) {
739
+ const list = out.get(asked) ?? [];
740
+ list.push(e);
741
+ out.set(asked, list);
742
+ }
743
+ }
744
+ for (const list of out.values()) list.sort(byEntryId);
745
+ return out;
746
+ }
747
+ function readSeatContactLogs(db, subjectIds, subjectType = "user", opts = {}) {
748
+ return readLogs(
749
+ db,
750
+ SEAT_CONTACTS_COLLECTION,
751
+ subjectType,
752
+ subjectIds,
753
+ (r) => parseWith(SeatContactEntrySchema, r),
754
+ opts
755
+ );
756
+ }
757
+
606
758
  // src/pointsOfContact/reader.ts
759
+ function seatContactListsFromLog(entries) {
760
+ const live = liveSeatContacts(entries);
761
+ if (!live.length) return null;
762
+ const listOf = (type) => {
763
+ const pref = preferredSeatContact(live, type);
764
+ const seen = /* @__PURE__ */ new Set();
765
+ const out = [];
766
+ for (const e of live) {
767
+ if (e.contact_type !== type) continue;
768
+ const value = type === "email" ? e.value.trim().toLowerCase() : e.value.trim();
769
+ if (!value || seen.has(value)) continue;
770
+ seen.add(value);
771
+ out.push({ value, is_preferred: e === pref });
772
+ }
773
+ return out;
774
+ };
775
+ return { emails: listOf("email"), phones: listOf("phone") };
776
+ }
777
+ function withSeatContactLogs(db, resolve, opts = {}) {
778
+ return async (userIds) => {
779
+ const base = await resolve(userIds);
780
+ const ids = userIds.filter((id) => base.has(id));
781
+ const out = new Map(base);
782
+ if (!ids.length) return out;
783
+ const logs = await readSeatContactLogs(db, ids, "user", {
784
+ invalid: "skip",
785
+ onInvalidRow: opts.onInvalidRow
786
+ });
787
+ for (const id of ids) {
788
+ const lists = seatContactListsFromLog(logs.get(id) ?? []);
789
+ if (!lists) continue;
790
+ const next = { ...base.get(id) };
791
+ if (lists.emails.length) {
792
+ next.emails = lists.emails;
793
+ next.email = lists.emails.find((e) => e.is_preferred)?.value ?? null;
794
+ }
795
+ if (lists.phones.length) {
796
+ next.phones = lists.phones;
797
+ next.phone = lists.phones.find((e) => e.is_preferred)?.value ?? null;
798
+ }
799
+ out.set(id, next);
800
+ }
801
+ return out;
802
+ };
803
+ }
607
804
  function subjectFilter(subject) {
608
805
  const type = requireId("subject_type", subject.subject_type);
609
806
  const base = { subject_type: type, subject_id: requireId("subject_id", subject.subject_id) };
@@ -837,10 +1034,10 @@ async function listPendingContacts(db, opts = {}) {
837
1034
  }
838
1035
 
839
1036
  // src/pointsOfContact/writer.ts
840
- var import_zod4 = require("zod");
1037
+ var import_zod5 = require("zod");
841
1038
 
842
1039
  // src/orgDirectory/schemas.ts
843
- var import_zod2 = require("zod");
1040
+ var import_zod3 = require("zod");
844
1041
 
845
1042
  // src/orgDirectory/vocabulary.ts
846
1043
  var PROGRAM_REVIEW_STATUS = {
@@ -885,31 +1082,37 @@ var ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([
885
1082
  "operatingStartDate",
886
1083
  "operatingEndDate",
887
1084
  "locationId",
888
- "notes"
1085
+ "notes",
1086
+ "teamId",
1087
+ "teamIds"
889
1088
  ]);
890
- var optionalText = import_zod2.z.string().nullish();
891
- var detailText = import_zod2.z.string().nullish().catch(void 0);
892
- var OrganizationProgramRowSchema = import_zod2.z.object({
893
- programId: import_zod2.z.string().min(1),
894
- name: import_zod2.z.string().min(1),
1089
+ var optionalText = import_zod3.z.string().nullish();
1090
+ var detailText = import_zod3.z.string().nullish().catch(void 0);
1091
+ var OrganizationProgramRowSchema = import_zod3.z.object({
1092
+ programId: import_zod3.z.string().min(1),
1093
+ name: import_zod3.z.string().min(1),
895
1094
  description: optionalText,
896
1095
  status: optionalText,
897
1096
  defaultSharingPolicy: optionalText,
898
- reviewStatus: import_zod2.z.enum(PROGRAM_REVIEW_STATUS_VALUES).nullish(),
1097
+ reviewStatus: import_zod3.z.enum(PROGRAM_REVIEW_STATUS_VALUES).nullish(),
899
1098
  addedBy: optionalText,
900
1099
  addedInApp: optionalText,
901
1100
  addedInSession: optionalText,
902
1101
  programType: detailText,
903
1102
  serviceArea: detailText,
904
- providesRelevantServices: import_zod2.z.boolean().nullish().catch(void 0),
1103
+ providesRelevantServices: import_zod3.z.boolean().nullish().catch(void 0),
905
1104
  operatingStartDate: detailText,
906
1105
  operatingEndDate: detailText,
907
1106
  locationId: detailText,
908
- notes: detailText
1107
+ notes: detailText,
1108
+ /** The first team that works for the program (canon `team_id`). */
1109
+ teamId: detailText,
1110
+ /** Every team that works for the program (canon `team_ids`). */
1111
+ teamIds: import_zod3.z.array(import_zod3.z.string()).nullish().catch(void 0)
909
1112
  });
910
1113
 
911
1114
  // src/orgDirectory/orgRecord.ts
912
- var import_zod3 = require("zod");
1115
+ var import_zod4 = require("zod");
913
1116
  var ORG_REVIEW_STATUS = PROGRAM_REVIEW_STATUS;
914
1117
  var ORGANIZATION_STATUS = { ACTIVE: 1, INACTIVE: 2 };
915
1118
  function isListedOrganization(row) {
@@ -971,9 +1174,9 @@ var ORG_KEY_PATTERN = /^[a-z0-9][a-z0-9_-]{0,119}$/;
971
1174
  function isOrgKey(value) {
972
1175
  return typeof value === "string" && ORG_KEY_PATTERN.test(value);
973
1176
  }
974
- var line = (max) => import_zod3.z.string().trim().min(1).max(max).nullable();
1177
+ var line = (max) => import_zod4.z.string().trim().min(1).max(max).nullable();
975
1178
  var ORG_VSP_TWO_STEP_MESSAGE = "vspPrivateNonprofit is step 2 of the HMIS VSP two-step and requires vsp: true";
976
- var OrgIdentityWriteSchema = import_zod3.z.object({
1179
+ var OrgIdentityWriteSchema = import_zod4.z.object({
977
1180
  legalName: line(300),
978
1181
  commonName: line(300),
979
1182
  shortName: line(60),
@@ -981,9 +1184,9 @@ var OrgIdentityWriteSchema = import_zod3.z.object({
981
1184
  /** Another organization's id (24-hex or a seed slug). Self-reference is the writer's check. */
982
1185
  parentOrgId: line(120)
983
1186
  }).partial().strict();
984
- var OrgVspWriteSchema = import_zod3.z.object({
985
- vsp: import_zod3.z.boolean().nullable(),
986
- vspPrivateNonprofit: import_zod3.z.boolean().nullable()
1187
+ var OrgVspWriteSchema = import_zod4.z.object({
1188
+ vsp: import_zod4.z.boolean().nullable(),
1189
+ vspPrivateNonprofit: import_zod4.z.boolean().nullable()
987
1190
  }).partial().strict().refine((v) => v.vspPrivateNonprofit == null || v.vsp === true, {
988
1191
  message: ORG_VSP_TWO_STEP_MESSAGE,
989
1192
  path: ["vspPrivateNonprofit"]
@@ -1153,8 +1356,8 @@ async function readAgencyOwnOrganization(db, agencyId, opts = {}) {
1153
1356
  var POC_PARTNER_ADD_APPS = POC_ADDED_IN_APPS.filter(
1154
1357
  (a) => a !== "platform"
1155
1358
  );
1156
- var directoryId = import_zod4.z.string().trim().min(1).max(200);
1157
- var PartnerContactSelectorSchema = import_zod4.z.object({ agency_id: directoryId.optional(), org_id: directoryId.optional() }).strict().refine((p) => Boolean(p.agency_id || p.org_id), {
1359
+ var directoryId = import_zod5.z.string().trim().min(1).max(200);
1360
+ var PartnerContactSelectorSchema = import_zod5.z.object({ agency_id: directoryId.optional(), org_id: directoryId.optional() }).strict().refine((p) => Boolean(p.agency_id || p.org_id), {
1158
1361
  message: "Name the partner's agency_id or org_id."
1159
1362
  });
1160
1363
  var POC_IN_APP_PENDING_CAP_PER_ACTOR = 25;
@@ -1672,8 +1875,10 @@ async function mintPartnerContact(db, args, driver) {
1672
1875
  preferredEmail,
1673
1876
  preferredPhone,
1674
1877
  resolveMagicLinkRecipients,
1878
+ seatContactListsFromLog,
1675
1879
  toNetworkView,
1676
1880
  toOwnerView,
1677
- toStewardView
1881
+ toStewardView,
1882
+ withSeatContactLogs
1678
1883
  });
1679
1884
  //# sourceMappingURL=index.cjs.map