@htilabs/platform-common-models 0.86.0 → 0.87.1

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 (40) hide show
  1. package/README.md +20 -0
  2. package/dist/{index-XWSYon40.d.cts → index-Bj-s0nZy.d.cts} +1 -1
  3. package/dist/{index-Bh1zO_Vu.d.ts → index-DvAypMJV.d.ts} +1 -1
  4. package/dist/index.d.cts +2 -2
  5. package/dist/index.d.ts +2 -2
  6. package/dist/models/index.d.cts +2 -2
  7. package/dist/models/index.d.ts +2 -2
  8. package/dist/org-merge/index.cjs.map +1 -1
  9. package/dist/org-merge/index.js.map +1 -1
  10. package/dist/points-of-contact/index.cjs +222 -23
  11. package/dist/points-of-contact/index.cjs.map +1 -1
  12. package/dist/points-of-contact/index.d.cts +50 -5
  13. package/dist/points-of-contact/index.d.ts +50 -5
  14. package/dist/points-of-contact/index.js +219 -22
  15. package/dist/points-of-contact/index.js.map +1 -1
  16. package/dist/reader-BmhzOis-.d.ts +273 -0
  17. package/dist/reader-DfQtdw6n.d.cts +273 -0
  18. package/dist/schemas/index.d.cts +25 -25
  19. package/dist/schemas/index.d.ts +25 -25
  20. package/dist/seat-logs/index.cjs +815 -0
  21. package/dist/seat-logs/index.cjs.map +1 -0
  22. package/dist/seat-logs/index.d.cts +274 -0
  23. package/dist/seat-logs/index.d.ts +274 -0
  24. package/dist/seat-logs/index.js +742 -0
  25. package/dist/seat-logs/index.js.map +1 -0
  26. package/package.json +6 -1
  27. package/src/pointsOfContact/__tests__/seatContactLog.test.ts +170 -0
  28. package/src/pointsOfContact/readRules.ts +22 -4
  29. package/src/pointsOfContact/reader.ts +95 -1
  30. package/src/pointsOfContact/schemas.ts +4 -3
  31. package/src/seatLogs/__tests__/seatLogs.review.test.ts +222 -0
  32. package/src/seatLogs/__tests__/seatLogs.test.ts +335 -0
  33. package/src/seatLogs/index.ts +15 -0
  34. package/src/seatLogs/log.ts +700 -0
  35. package/src/seatLogs/personId.ts +100 -0
  36. package/src/seatLogs/personSlugWordlists.ts +40 -0
  37. package/src/seatLogs/reader.ts +143 -0
  38. package/src/seatLogs/schemas.ts +118 -0
  39. package/dist/{cvcEntry-D787utvP.d.cts → cvcEntry-BLeRivoF.d.cts} +8 -8
  40. package/dist/{cvcEntry-D787utvP.d.ts → cvcEntry-BLeRivoF.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 = {
@@ -889,21 +1086,21 @@ var ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([
889
1086
  "teamId",
890
1087
  "teamIds"
891
1088
  ]);
892
- var optionalText = import_zod2.z.string().nullish();
893
- var detailText = import_zod2.z.string().nullish().catch(void 0);
894
- var OrganizationProgramRowSchema = import_zod2.z.object({
895
- programId: import_zod2.z.string().min(1),
896
- 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),
897
1094
  description: optionalText,
898
1095
  status: optionalText,
899
1096
  defaultSharingPolicy: optionalText,
900
- reviewStatus: import_zod2.z.enum(PROGRAM_REVIEW_STATUS_VALUES).nullish(),
1097
+ reviewStatus: import_zod3.z.enum(PROGRAM_REVIEW_STATUS_VALUES).nullish(),
901
1098
  addedBy: optionalText,
902
1099
  addedInApp: optionalText,
903
1100
  addedInSession: optionalText,
904
1101
  programType: detailText,
905
1102
  serviceArea: detailText,
906
- providesRelevantServices: import_zod2.z.boolean().nullish().catch(void 0),
1103
+ providesRelevantServices: import_zod3.z.boolean().nullish().catch(void 0),
907
1104
  operatingStartDate: detailText,
908
1105
  operatingEndDate: detailText,
909
1106
  locationId: detailText,
@@ -911,11 +1108,11 @@ var OrganizationProgramRowSchema = import_zod2.z.object({
911
1108
  /** The first team that works for the program (canon `team_id`). */
912
1109
  teamId: detailText,
913
1110
  /** Every team that works for the program (canon `team_ids`). */
914
- teamIds: import_zod2.z.array(import_zod2.z.string()).nullish().catch(void 0)
1111
+ teamIds: import_zod3.z.array(import_zod3.z.string()).nullish().catch(void 0)
915
1112
  });
916
1113
 
917
1114
  // src/orgDirectory/orgRecord.ts
918
- var import_zod3 = require("zod");
1115
+ var import_zod4 = require("zod");
919
1116
  var ORG_REVIEW_STATUS = PROGRAM_REVIEW_STATUS;
920
1117
  var ORGANIZATION_STATUS = { ACTIVE: 1, INACTIVE: 2 };
921
1118
  function isListedOrganization(row) {
@@ -977,9 +1174,9 @@ var ORG_KEY_PATTERN = /^[a-z0-9][a-z0-9_-]{0,119}$/;
977
1174
  function isOrgKey(value) {
978
1175
  return typeof value === "string" && ORG_KEY_PATTERN.test(value);
979
1176
  }
980
- 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();
981
1178
  var ORG_VSP_TWO_STEP_MESSAGE = "vspPrivateNonprofit is step 2 of the HMIS VSP two-step and requires vsp: true";
982
- var OrgIdentityWriteSchema = import_zod3.z.object({
1179
+ var OrgIdentityWriteSchema = import_zod4.z.object({
983
1180
  legalName: line(300),
984
1181
  commonName: line(300),
985
1182
  shortName: line(60),
@@ -987,9 +1184,9 @@ var OrgIdentityWriteSchema = import_zod3.z.object({
987
1184
  /** Another organization's id (24-hex or a seed slug). Self-reference is the writer's check. */
988
1185
  parentOrgId: line(120)
989
1186
  }).partial().strict();
990
- var OrgVspWriteSchema = import_zod3.z.object({
991
- vsp: import_zod3.z.boolean().nullable(),
992
- 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()
993
1190
  }).partial().strict().refine((v) => v.vspPrivateNonprofit == null || v.vsp === true, {
994
1191
  message: ORG_VSP_TWO_STEP_MESSAGE,
995
1192
  path: ["vspPrivateNonprofit"]
@@ -1159,8 +1356,8 @@ async function readAgencyOwnOrganization(db, agencyId, opts = {}) {
1159
1356
  var POC_PARTNER_ADD_APPS = POC_ADDED_IN_APPS.filter(
1160
1357
  (a) => a !== "platform"
1161
1358
  );
1162
- var directoryId = import_zod4.z.string().trim().min(1).max(200);
1163
- 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), {
1164
1361
  message: "Name the partner's agency_id or org_id."
1165
1362
  });
1166
1363
  var POC_IN_APP_PENDING_CAP_PER_ACTOR = 25;
@@ -1678,8 +1875,10 @@ async function mintPartnerContact(db, args, driver) {
1678
1875
  preferredEmail,
1679
1876
  preferredPhone,
1680
1877
  resolveMagicLinkRecipients,
1878
+ seatContactListsFromLog,
1681
1879
  toNetworkView,
1682
1880
  toOwnerView,
1683
- toStewardView
1881
+ toStewardView,
1882
+ withSeatContactLogs
1684
1883
  });
1685
1884
  //# sourceMappingURL=index.cjs.map