@htilabs/platform-common-models 0.34.0 → 0.36.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 (52) hide show
  1. package/README.md +135 -0
  2. package/dist/constants/index.cjs +2 -0
  3. package/dist/constants/index.cjs.map +1 -1
  4. package/dist/constants/index.js +2 -0
  5. package/dist/constants/index.js.map +1 -1
  6. package/dist/{cvcEntry-DAuhe0cd.d.cts → cvcEntry-BqAGctqK.d.cts} +36 -36
  7. package/dist/{cvcEntry-DAuhe0cd.d.ts → cvcEntry-BqAGctqK.d.ts} +36 -36
  8. package/dist/{index-1pWiqEBC.d.cts → index-BTqHP4DM.d.cts} +1 -1
  9. package/dist/{index-uz57Fic9.d.ts → index-Dr6GBCoY.d.ts} +1 -1
  10. package/dist/index.cjs +2 -0
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +2 -2
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.js +2 -0
  15. package/dist/index.js.map +1 -1
  16. package/dist/models/index.d.cts +2 -2
  17. package/dist/models/index.d.ts +2 -2
  18. package/dist/org-directory/index.cjs +4 -3
  19. package/dist/org-directory/index.cjs.map +1 -1
  20. package/dist/org-directory/index.d.cts +22 -6
  21. package/dist/org-directory/index.d.ts +22 -6
  22. package/dist/org-directory/index.js +4 -3
  23. package/dist/org-directory/index.js.map +1 -1
  24. package/dist/points-of-contact/index.cjs +6 -3
  25. package/dist/points-of-contact/index.cjs.map +1 -1
  26. package/dist/points-of-contact/index.js +6 -3
  27. package/dist/points-of-contact/index.js.map +1 -1
  28. package/dist/schemas/index.d.cts +11 -11
  29. package/dist/schemas/index.d.ts +11 -11
  30. package/dist/share-review/index.cjs +301 -0
  31. package/dist/share-review/index.cjs.map +1 -0
  32. package/dist/share-review/index.d.cts +431 -0
  33. package/dist/share-review/index.d.ts +431 -0
  34. package/dist/share-review/index.js +263 -0
  35. package/dist/share-review/index.js.map +1 -0
  36. package/package.json +11 -1
  37. package/src/__sot__/README.md +2 -0
  38. package/src/__sot__/captureConflictSemantics.pin.json +156 -0
  39. package/src/generated/captureConflictSemantics.ts +200 -0
  40. package/src/orgDirectory/__tests__/orgDirectory.test.ts +60 -0
  41. package/src/orgDirectory/rules.ts +26 -9
  42. package/src/pointsOfContact/__tests__/writer.test.ts +15 -0
  43. package/src/shareReview/__tests__/captureConflictSemantics.test.ts +148 -0
  44. package/src/shareReview/__tests__/captureConflictSemanticsPin.test.ts +78 -0
  45. package/src/shareReview/__tests__/recommendedDecision.test.ts +35 -0
  46. package/src/shareReview/__tests__/reviewDecision.test.ts +102 -0
  47. package/src/shareReview/__tests__/shareFieldReviewPlan.test.ts +523 -0
  48. package/src/shareReview/captureConflictSemantics.ts +170 -0
  49. package/src/shareReview/index.ts +23 -0
  50. package/src/shareReview/recommendedDecision.ts +21 -0
  51. package/src/shareReview/reviewDecision.ts +71 -0
  52. package/src/shareReview/shareFieldReviewPlan.ts +288 -0
@@ -1,3 +1,3 @@
1
- export { a as CvcEntry, b as CvcRegistry, F as Form, P as PlatformFunction, d as PlatformRole, Q as Question, f as ScoringRule, h as Section } from '../cvcEntry-DAuhe0cd.cjs';
1
+ export { a as CvcEntry, b as CvcRegistry, F as Form, P as PlatformFunction, d as PlatformRole, Q as Question, f as ScoringRule, h as Section } from '../cvcEntry-BqAGctqK.cjs';
2
2
  import 'mongoose';
3
- export { C as CvcEntryDoc, a as CvcRegistryDoc, F as FormDoc, P as PlatformFunctionDoc, b as PlatformModels, c as PlatformRoleDoc, Q as QuestionDoc, S as ScoringRuleDoc, d as SectionDoc, g as createPlatformModels, h as cvcEntryModel, i as cvcRegistryModel, j as formModel, p as platformFunctionModel, k as platformRoleModel, q as questionModel, s as scoringRuleModel, l as sectionModel } from '../index-1pWiqEBC.cjs';
3
+ export { C as CvcEntryDoc, a as CvcRegistryDoc, F as FormDoc, P as PlatformFunctionDoc, b as PlatformModels, c as PlatformRoleDoc, Q as QuestionDoc, S as ScoringRuleDoc, d as SectionDoc, g as createPlatformModels, h as cvcEntryModel, i as cvcRegistryModel, j as formModel, p as platformFunctionModel, k as platformRoleModel, q as questionModel, s as scoringRuleModel, l as sectionModel } from '../index-BTqHP4DM.cjs';
@@ -1,3 +1,3 @@
1
- export { a as CvcEntry, b as CvcRegistry, F as Form, P as PlatformFunction, d as PlatformRole, Q as Question, f as ScoringRule, h as Section } from '../cvcEntry-DAuhe0cd.js';
1
+ export { a as CvcEntry, b as CvcRegistry, F as Form, P as PlatformFunction, d as PlatformRole, Q as Question, f as ScoringRule, h as Section } from '../cvcEntry-BqAGctqK.js';
2
2
  import 'mongoose';
3
- export { C as CvcEntryDoc, a as CvcRegistryDoc, F as FormDoc, P as PlatformFunctionDoc, b as PlatformModels, c as PlatformRoleDoc, Q as QuestionDoc, S as ScoringRuleDoc, d as SectionDoc, g as createPlatformModels, h as cvcEntryModel, i as cvcRegistryModel, j as formModel, p as platformFunctionModel, k as platformRoleModel, q as questionModel, s as scoringRuleModel, l as sectionModel } from '../index-uz57Fic9.js';
3
+ export { C as CvcEntryDoc, a as CvcRegistryDoc, F as FormDoc, P as PlatformFunctionDoc, b as PlatformModels, c as PlatformRoleDoc, Q as QuestionDoc, S as ScoringRuleDoc, d as SectionDoc, g as createPlatformModels, h as cvcEntryModel, i as cvcRegistryModel, j as formModel, p as platformFunctionModel, k as platformRoleModel, q as questionModel, s as scoringRuleModel, l as sectionModel } from '../index-Dr6GBCoY.js';
@@ -115,9 +115,10 @@ function organizationRowShape(raw) {
115
115
  if (!raw || typeof raw !== "object") return "unknown";
116
116
  const r = raw;
117
117
  if (typeof r.canonicalKey === "string" && r.canonicalKey !== "") return "canonical";
118
- if (typeof r.id === "string" || typeof r.type === "string" || "acronym" in r || "location" in r)
119
- return "pave";
120
- if (typeof r.type === "number") return "thread";
118
+ if (typeof r.id === "string" || "acronym" in r || "location" in r) return "pave";
119
+ if (typeof r.type === "number" || typeof r.type === "string" && r.type.trim() !== "") {
120
+ return "thread";
121
+ }
121
122
  return "unknown";
122
123
  }
123
124
  function isAgencyOwnOrganization(org, agency) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/orgDirectory/index.ts","../../src/orgDirectory/vocabulary.ts","../../src/orgDirectory/schemas.ts","../../src/orgDirectory/rules.ts","../../src/orgDirectory/read.ts"],"sourcesContent":["/**\n * @htilabs/platform-common-models/org-directory\n *\n * The READ contract for the platform-db org directory: `organizations` rows\n * (Thread / auth shape and PAVE 2.0 shape), their `programs[]`, and the\n * agency's-own-organization rule. The Platform writes through\n * `lib/org-directory/` (platform `docs/ORG_DIRECTORY_CONTRACT.md`); PAVE,\n * Thread and LEADS read through here. No mongoose, no driver import, no SOT.\n * Subpath only. The program vocabulary is also re-exported by the Mongoose\n * fragment (`schemas/organization-program.ts`) — one source.\n */\n\nexport * from \"./vocabulary.js\";\nexport * from \"./schemas.js\";\nexport * from \"./rules.js\";\nexport * from \"./read.js\";\n","/**\n * The program-row vocabulary of `organizations.programs[]`: the review\n * lifecycle, the lifecycle codes, the live rule and the name key. Pure (no\n * mongoose): `schemas/organization-program.ts` re-exports these, so the\n * Mongoose fragment and the `org-directory` read contract share one source.\n */\n\n// ─── Review status ──────────────────────────────────────────────────\n\n/**\n * Review lifecycle of a program named inside an app (UDD Program\n * `review_status`). The sheet's own words — a lane's `pending_steward` /\n * `declined` never reach the stored row.\n */\nexport const PROGRAM_REVIEW_STATUS = {\n PENDING: \"pending\",\n CONFIRMED: \"confirmed\",\n REJECTED: \"rejected\",\n} as const;\n\nexport type ProgramReviewStatus =\n (typeof PROGRAM_REVIEW_STATUS)[keyof typeof PROGRAM_REVIEW_STATUS];\n\n/** The three values, for `enum:` declarations and Zod `z.enum`. */\nexport const PROGRAM_REVIEW_STATUS_VALUES = Object.freeze([\n PROGRAM_REVIEW_STATUS.PENDING,\n PROGRAM_REVIEW_STATUS.CONFIRMED,\n PROGRAM_REVIEW_STATUS.REJECTED,\n] as const);\n\nexport function isProgramReviewStatus(value: unknown): value is ProgramReviewStatus {\n return (\n typeof value === \"string\" && (PROGRAM_REVIEW_STATUS_VALUES as readonly string[]).includes(value)\n );\n}\n\n// ─── Lifecycle status ───────────────────────────────────────────────\n\n/**\n * The program lifecycle codes the writers store. `active` is what PAVE 2.0's\n * `POST /organizations/:id/programs` writes; `retired` takes a program out of\n * every picker. Absent reads as active.\n */\nexport const PROGRAM_STATUS = {\n ACTIVE: \"active\",\n RETIRED: \"retired\",\n} as const;\n\nexport type ProgramStatus = (typeof PROGRAM_STATUS)[keyof typeof PROGRAM_STATUS];\n\nexport const PROGRAM_STATUS_VALUES = Object.freeze([\n PROGRAM_STATUS.ACTIVE,\n PROGRAM_STATUS.RETIRED,\n] as const);\n\n// ─── Live rule and name key ─────────────────────────────────────────\n\n/**\n * Whether a program row may be OFFERED (and matched by name on an add).\n * A retired or rejected program is not a place anyone can be enrolled today,\n * so pickers do not list it and typing its name mints a new row rather than\n * binding to it (the prototype's independent pre-review catch, 2026-10-06).\n */\nexport function isLiveOrganizationProgram(row: {\n status?: string | null | undefined;\n reviewStatus?: string | null | undefined;\n}): boolean {\n return (\n row.status !== PROGRAM_STATUS.RETIRED && row.reviewStatus !== PROGRAM_REVIEW_STATUS.REJECTED\n );\n}\n\n/**\n * The key two program names match on: trimmed, lower-cased. Canon:\n * `directoryProgramFor` (platform-prototype `src/surveys.jsx` L4233 @\n * `0fa04c1`); PAVE 2.0's `POST /organizations/:id/programs` dedupes the same\n * way.\n */\nexport function programNameKey(name: string): string {\n return name.trim().toLowerCase();\n}\n","/**\n * The stored program row of `organizations.programs[]`, as a Zod schema for\n * readers. The Mongoose definition (`schemas/organization-program.ts`,\n * `organizationProgramSchemaDefinition`) is the write-side twin; a test holds\n * the key lists equal.\n */\n\nimport { z } from \"zod\";\n\nimport { PROGRAM_REVIEW_STATUS_VALUES } from \"./vocabulary.js\";\n\n/** The program row's keys, in the fragment's declaration order. */\nexport const ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([\n \"programId\",\n \"name\",\n \"description\",\n \"status\",\n \"defaultSharingPolicy\",\n \"reviewStatus\",\n \"addedBy\",\n \"addedInApp\",\n \"addedInSession\",\n] as const);\n\nexport type OrganizationProgramRowKey = (typeof ORGANIZATION_PROGRAM_ROW_KEYS)[number];\n\nconst optionalText = z.string().nullish();\n\n/**\n * One stored program row. `programId` + `name` are required; the rest are\n * optional and may be null. Unknown keys are stripped (a reader never relies\n * on a key outside the fragment).\n */\nexport const OrganizationProgramRowSchema = z.object({\n programId: z.string().min(1),\n name: z.string().min(1),\n description: optionalText,\n status: optionalText,\n defaultSharingPolicy: optionalText,\n reviewStatus: z\n .enum(PROGRAM_REVIEW_STATUS_VALUES as unknown as [\"pending\", \"confirmed\", \"rejected\"])\n .nullish(),\n addedBy: optionalText,\n addedInApp: optionalText,\n addedInSession: optionalText,\n});\n\nexport type OrganizationProgramRow = z.infer<typeof OrganizationProgramRowSchema>;\n","/**\n * Read rules for the platform-db org directory: which row shape a document\n * is, when an organization is an agency's OWN record, and the normalised read\n * view every app shares. Pure: no mongoose, no driver.\n *\n * Contract: platform `docs/ORG_DIRECTORY_CONTRACT.md`.\n */\n\nimport { OrganizationProgramRowSchema, type OrganizationProgramRow } from \"./schemas.js\";\nimport { isLiveOrganizationProgram } from \"./vocabulary.js\";\n\n/** A driver ObjectId, or anything that prints itself as one. */\nexport interface ObjectIdLike {\n toHexString(): string;\n}\n\n/** An id's comparable string form: an ObjectId's hex, a string as-is, else null. */\nexport function directoryIdString(value: unknown): string | null {\n if (typeof value === \"string\") return value === \"\" ? null : value;\n if (\n value &&\n typeof value === \"object\" &&\n typeof (value as ObjectIdLike).toHexString === \"function\"\n ) {\n return (value as ObjectIdLike).toHexString();\n }\n return null;\n}\n\n/** Two ids name the same row (string vs ObjectId of one hex are the same). Absent is never a match. */\nexport function sameDirectoryId(a: unknown, b: unknown): boolean {\n const x = directoryIdString(a);\n const y = directoryIdString(b);\n return x !== null && y !== null && x.toLowerCase() === y.toLowerCase();\n}\n\n/**\n * Which writer's shape a stored `organizations` row has.\n *\n * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.\n * - `pave`: PAVE 2.0's shape (`packages/db/src/models/organization.ts`):\n * a string `id`, `type` as a CVC-48 key, `acronym`, `location`.\n * - `thread`: the Thread / auth shape: `type` an IC-58 Int.\n * - `unknown`: none of the above.\n */\nexport type OrganizationRowShape = \"canonical\" | \"pave\" | \"thread\" | \"unknown\";\n\nexport function organizationRowShape(raw: unknown): OrganizationRowShape {\n if (!raw || typeof raw !== \"object\") return \"unknown\";\n const r = raw as Record<string, unknown>;\n if (typeof r.canonicalKey === \"string\" && r.canonicalKey !== \"\") return \"canonical\";\n if (typeof r.id === \"string\" || typeof r.type === \"string\" || \"acronym\" in r || \"location\" in r)\n return \"pave\";\n if (typeof r.type === \"number\") return \"thread\";\n return \"unknown\";\n}\n\n/**\n * Whether `org` is `agency`'s OWN organization record: both halves of the pair\n * auth's `ensureOrganizationForAgency` writes hold — `org.agencyId` names the\n * agency AND `agency.organizationId` names the org. Neither half alone counts;\n * `agencyScope` (tenancy) never counts.\n */\nexport function isAgencyOwnOrganization(\n org: { _id?: unknown; agencyId?: unknown } | null | undefined,\n agency: { _id?: unknown; organizationId?: unknown } | null | undefined,\n): boolean {\n if (!org || !agency) return false;\n return (\n sameDirectoryId(org.agencyId, agency._id) && sameDirectoryId(agency.organizationId, org._id)\n );\n}\n\nexport interface DirectoryProgramView {\n programId: string;\n name: string;\n description: string | null;\n status: string | null;\n reviewStatus: OrganizationProgramRow[\"reviewStatus\"] | null;\n addedBy: string | null;\n addedInApp: string | null;\n /** Offered in pickers: not retired, not rejected. */\n live: boolean;\n}\n\n/** One program row as readers see it, or null when it fails the row schema. */\nexport function toDirectoryProgramView(raw: unknown): DirectoryProgramView | null {\n const parsed = OrganizationProgramRowSchema.safeParse(raw);\n if (!parsed.success) return null;\n const p = parsed.data;\n return {\n programId: p.programId,\n name: p.name,\n description: p.description ?? null,\n status: p.status ?? null,\n reviewStatus: p.reviewStatus ?? null,\n addedBy: p.addedBy ?? null,\n addedInApp: p.addedInApp ?? null,\n live: isLiveOrganizationProgram(p),\n };\n}\n\nexport interface DirectoryOrganizationView {\n /** `String(_id)`: the id listings and sharing settings key on. */\n id: string;\n shape: OrganizationRowShape;\n name: string;\n /** IC-58 Int (Thread / auth shape), else null. */\n type: number | null;\n /** CVC-48 `referral_entity_category` key (PAVE shape), else null. */\n typeKey: string | null;\n /** PAVE's own string `id` on a PAVE-shape row, else null. */\n paveId: string | null;\n community: string | null;\n /** auth's identity half; see {@link isAgencyOwnOrganization}. */\n agencyId: string | null;\n /** Tenancy scope. Never an identity. */\n agencyScope: string | null;\n canonicalKey: string | null;\n programs: DirectoryProgramView[];\n /** Program rows that failed the row schema (not returned). */\n invalidPrograms: number;\n}\n\nconst text = (v: unknown): string | null => (typeof v === \"string\" && v !== \"\" ? v : null);\n\n/** Normalise a stored row of any writer's shape into the shared read view. */\nexport function toDirectoryOrganizationView(raw: unknown): DirectoryOrganizationView | null {\n if (!raw || typeof raw !== \"object\") return null;\n const r = raw as Record<string, unknown>;\n const id = directoryIdString(r._id);\n if (!id) return null;\n const rows = Array.isArray(r.programs) ? r.programs : [];\n const programs = rows\n .map(toDirectoryProgramView)\n .filter((p): p is DirectoryProgramView => p !== null);\n return {\n id,\n shape: organizationRowShape(r),\n name: text(r.name) ?? \"\",\n type: typeof r.type === \"number\" ? r.type : null,\n typeKey: typeof r.type === \"string\" ? r.type : null,\n paveId: text(r.id),\n community: text(r.community),\n agencyId: directoryIdString(r.agencyId),\n agencyScope: directoryIdString(r.agencyScope),\n canonicalKey: text(r.canonicalKey),\n programs,\n invalidPrograms: rows.length - programs.length,\n };\n}\n","/**\n * The raw-driver reads of the platform-db org directory. No driver import: the\n * readers take any object with the MongoDB driver's\n * `collection(name).findOne(filter, options)` shape, plus a `toObjectId`\n * factory so a 24-hex string id also matches a row stored under an ObjectId\n * (production `organizations._id` and `agencies._id` are ObjectIds).\n */\n\nimport {\n isAgencyOwnOrganization,\n toDirectoryOrganizationView,\n type DirectoryOrganizationView,\n type ObjectIdLike,\n} from \"./rules.js\";\n\nexport const ORGANIZATIONS_COLLECTION = \"organizations\";\nexport const AGENCIES_COLLECTION = \"agencies\";\n\nexport interface OrgDirectoryDbLike {\n collection(name: string): {\n findOne(filter: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;\n };\n}\n\nexport interface OrgDirectoryReadOptions {\n /** Build the driver's ObjectId from 24-hex (e.g. `(h) => new ObjectId(h)`). */\n toObjectId?: (hex: string) => unknown;\n}\n\nconst HEX = /^[0-9a-fA-F]{24}$/;\n\n/** Every stored form of one id: the string, and its ObjectId when 24-hex. */\nexport function organizationIdForms(\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): unknown[] {\n if (typeof id !== \"string\") return [id.toHexString(), id];\n return HEX.test(id) && opts.toObjectId ? [id, opts.toObjectId(id.toLowerCase())] : [id];\n}\n\n/** One organization in the shared view, or null. Read errors propagate. */\nexport async function readDirectoryOrganization(\n db: OrgDirectoryDbLike,\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const raw = await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(id, opts) } });\n return raw ? toDirectoryOrganizationView(raw) : null;\n}\n\n/**\n * An agency's OWN organization (both halves of the pair hold), or null — never\n * an organization the agency merely scopes (`agencyScope`) or that points at it\n * from one side only. The agency is looked up by `_id`.\n */\nexport async function readAgencyOwnOrganization(\n db: OrgDirectoryDbLike,\n agencyId: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const agency = (await db\n .collection(AGENCIES_COLLECTION)\n .findOne(\n { _id: { $in: organizationIdForms(agencyId, opts) } },\n { projection: { _id: 1, organizationId: 1 } },\n )) as { _id?: unknown; organizationId?: unknown } | null;\n if (!agency || agency.organizationId == null) return null;\n const pointer = agency.organizationId as string | ObjectIdLike;\n const org = (await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(pointer, opts) } })) as {\n _id?: unknown;\n agencyId?: unknown;\n } | null;\n return org && isAgencyOwnOrganization(org, agency) ? toDirectoryOrganizationView(org) : null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcO,IAAM,wBAAwB;AAAA,EACnC,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AACZ;AAMO,IAAM,+BAA+B,OAAO,OAAO;AAAA,EACxD,sBAAsB;AAAA,EACtB,sBAAsB;AAAA,EACtB,sBAAsB;AACxB,CAAU;AAEH,SAAS,sBAAsB,OAA8C;AAClF,SACE,OAAO,UAAU,YAAa,6BAAmD,SAAS,KAAK;AAEnG;AASO,IAAM,iBAAiB;AAAA,EAC5B,QAAQ;AAAA,EACR,SAAS;AACX;AAIO,IAAM,wBAAwB,OAAO,OAAO;AAAA,EACjD,eAAe;AAAA,EACf,eAAe;AACjB,CAAU;AAUH,SAAS,0BAA0B,KAG9B;AACV,SACE,IAAI,WAAW,eAAe,WAAW,IAAI,iBAAiB,sBAAsB;AAExF;AAQO,SAAS,eAAe,MAAsB;AACnD,SAAO,KAAK,KAAK,EAAE,YAAY;AACjC;;;ACzEA,iBAAkB;AAKX,IAAM,gCAAgC,OAAO,OAAO;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAU;AAIV,IAAM,eAAe,aAAE,OAAO,EAAE,QAAQ;AAOjC,IAAM,+BAA+B,aAAE,OAAO;AAAA,EACnD,WAAW,aAAE,OAAO,EAAE,IAAI,CAAC;AAA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GAAG,EAAE;AACxD,SAAO,IAAI,KAAK,EAAE,KAAK,KAAK,aAAa,CAAC,IAAI,KAAK,WAAW,GAAG,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE;AACxF;AAGA,eAAsB,0BACpB,IACA,IACA,OAAgC,CAAC,GACU;AAC3C,QAAM,MAAM,MAAM,GACf,WAAW,wBAAwB,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,IAAI,IAAI,EAAE,EAAE,CAAC;AAC1D,SAAO,MAAM,4BAA4B,GAAG,IAAI;AAClD;AAOA,eAAsB,0BACpB,IACA,UACA,OAAgC,CAAC,GACU;AAC3C,QAAM,SAAU,MAAM,GACnB,WAAW,mBAAmB,EAC9B;AAAA,IACC,EAAE,KAAK,EAAE,KAAK,oBAAoB,UAAU,IAAI,EAAE,EAAE;AAAA,IACpD,EAAE,YAAY,EAAE,KAAK,GAAG,gBAAgB,EAAE,EAAE;AAAA,EAC9C;AACF,MAAI,CAAC,UAAU,OAAO,kBAAkB,KAAM,QAAO;AACrD,QAAM,UAAU,OAAO;AACvB,QAAM,MAAO,MAAM,GAChB,WAAW,wBAAwB,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,SAAS,IAAI,EAAE,EAAE,CAAC;AAI/D,SAAO,OAAO,wBAAwB,KAAK,MAAM,IAAI,4BAA4B,GAAG,IAAI;AAC1F;","names":[]}
1
+ {"version":3,"sources":["../../src/orgDirectory/index.ts","../../src/orgDirectory/vocabulary.ts","../../src/orgDirectory/schemas.ts","../../src/orgDirectory/rules.ts","../../src/orgDirectory/read.ts"],"sourcesContent":["/**\n * @htilabs/platform-common-models/org-directory\n *\n * The READ contract for the platform-db org directory: `organizations` rows\n * (Thread / auth shape and PAVE 2.0 shape), their `programs[]`, and the\n * agency's-own-organization rule. The Platform writes through\n * `lib/org-directory/` (platform `docs/ORG_DIRECTORY_CONTRACT.md`); PAVE,\n * Thread and LEADS read through here. No mongoose, no driver import, no SOT.\n * Subpath only. The program vocabulary is also re-exported by the Mongoose\n * fragment (`schemas/organization-program.ts`) — one source.\n */\n\nexport * from \"./vocabulary.js\";\nexport * from \"./schemas.js\";\nexport * from \"./rules.js\";\nexport * from \"./read.js\";\n","/**\n * The program-row vocabulary of `organizations.programs[]`: the review\n * lifecycle, the lifecycle codes, the live rule and the name key. Pure (no\n * mongoose): `schemas/organization-program.ts` re-exports these, so the\n * Mongoose fragment and the `org-directory` read contract share one source.\n */\n\n// ─── Review status ──────────────────────────────────────────────────\n\n/**\n * Review lifecycle of a program named inside an app (UDD Program\n * `review_status`). The sheet's own words — a lane's `pending_steward` /\n * `declined` never reach the stored row.\n */\nexport const PROGRAM_REVIEW_STATUS = {\n PENDING: \"pending\",\n CONFIRMED: \"confirmed\",\n REJECTED: \"rejected\",\n} as const;\n\nexport type ProgramReviewStatus =\n (typeof PROGRAM_REVIEW_STATUS)[keyof typeof PROGRAM_REVIEW_STATUS];\n\n/** The three values, for `enum:` declarations and Zod `z.enum`. */\nexport const PROGRAM_REVIEW_STATUS_VALUES = Object.freeze([\n PROGRAM_REVIEW_STATUS.PENDING,\n PROGRAM_REVIEW_STATUS.CONFIRMED,\n PROGRAM_REVIEW_STATUS.REJECTED,\n] as const);\n\nexport function isProgramReviewStatus(value: unknown): value is ProgramReviewStatus {\n return (\n typeof value === \"string\" && (PROGRAM_REVIEW_STATUS_VALUES as readonly string[]).includes(value)\n );\n}\n\n// ─── Lifecycle status ───────────────────────────────────────────────\n\n/**\n * The program lifecycle codes the writers store. `active` is what PAVE 2.0's\n * `POST /organizations/:id/programs` writes; `retired` takes a program out of\n * every picker. Absent reads as active.\n */\nexport const PROGRAM_STATUS = {\n ACTIVE: \"active\",\n RETIRED: \"retired\",\n} as const;\n\nexport type ProgramStatus = (typeof PROGRAM_STATUS)[keyof typeof PROGRAM_STATUS];\n\nexport const PROGRAM_STATUS_VALUES = Object.freeze([\n PROGRAM_STATUS.ACTIVE,\n PROGRAM_STATUS.RETIRED,\n] as const);\n\n// ─── Live rule and name key ─────────────────────────────────────────\n\n/**\n * Whether a program row may be OFFERED (and matched by name on an add).\n * A retired or rejected program is not a place anyone can be enrolled today,\n * so pickers do not list it and typing its name mints a new row rather than\n * binding to it (the prototype's independent pre-review catch, 2026-10-06).\n */\nexport function isLiveOrganizationProgram(row: {\n status?: string | null | undefined;\n reviewStatus?: string | null | undefined;\n}): boolean {\n return (\n row.status !== PROGRAM_STATUS.RETIRED && row.reviewStatus !== PROGRAM_REVIEW_STATUS.REJECTED\n );\n}\n\n/**\n * The key two program names match on: trimmed, lower-cased. Canon:\n * `directoryProgramFor` (platform-prototype `src/surveys.jsx` L4233 @\n * `0fa04c1`); PAVE 2.0's `POST /organizations/:id/programs` dedupes the same\n * way.\n */\nexport function programNameKey(name: string): string {\n return name.trim().toLowerCase();\n}\n","/**\n * The stored program row of `organizations.programs[]`, as a Zod schema for\n * readers. The Mongoose definition (`schemas/organization-program.ts`,\n * `organizationProgramSchemaDefinition`) is the write-side twin; a test holds\n * the key lists equal.\n */\n\nimport { z } from \"zod\";\n\nimport { PROGRAM_REVIEW_STATUS_VALUES } from \"./vocabulary.js\";\n\n/** The program row's keys, in the fragment's declaration order. */\nexport const ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([\n \"programId\",\n \"name\",\n \"description\",\n \"status\",\n \"defaultSharingPolicy\",\n \"reviewStatus\",\n \"addedBy\",\n \"addedInApp\",\n \"addedInSession\",\n] as const);\n\nexport type OrganizationProgramRowKey = (typeof ORGANIZATION_PROGRAM_ROW_KEYS)[number];\n\nconst optionalText = z.string().nullish();\n\n/**\n * One stored program row. `programId` + `name` are required; the rest are\n * optional and may be null. Unknown keys are stripped (a reader never relies\n * on a key outside the fragment).\n */\nexport const OrganizationProgramRowSchema = z.object({\n programId: z.string().min(1),\n name: z.string().min(1),\n description: optionalText,\n status: optionalText,\n defaultSharingPolicy: optionalText,\n reviewStatus: z\n .enum(PROGRAM_REVIEW_STATUS_VALUES as unknown as [\"pending\", \"confirmed\", \"rejected\"])\n .nullish(),\n addedBy: optionalText,\n addedInApp: optionalText,\n addedInSession: optionalText,\n});\n\nexport type OrganizationProgramRow = z.infer<typeof OrganizationProgramRowSchema>;\n","/**\n * Read rules for the platform-db org directory: which row shape a document\n * is, when an organization is an agency's OWN record, and the normalised read\n * view every app shares. Pure: no mongoose, no driver.\n *\n * Contract: platform `docs/ORG_DIRECTORY_CONTRACT.md`.\n */\n\nimport { OrganizationProgramRowSchema, type OrganizationProgramRow } from \"./schemas.js\";\nimport { isLiveOrganizationProgram } from \"./vocabulary.js\";\n\n/** A driver ObjectId, or anything that prints itself as one. */\nexport interface ObjectIdLike {\n toHexString(): string;\n}\n\n/** An id's comparable string form: an ObjectId's hex, a string as-is, else null. */\nexport function directoryIdString(value: unknown): string | null {\n if (typeof value === \"string\") return value === \"\" ? null : value;\n if (\n value &&\n typeof value === \"object\" &&\n typeof (value as ObjectIdLike).toHexString === \"function\"\n ) {\n return (value as ObjectIdLike).toHexString();\n }\n return null;\n}\n\n/** Two ids name the same row (string vs ObjectId of one hex are the same). Absent is never a match. */\nexport function sameDirectoryId(a: unknown, b: unknown): boolean {\n const x = directoryIdString(a);\n const y = directoryIdString(b);\n return x !== null && y !== null && x.toLowerCase() === y.toLowerCase();\n}\n\n/**\n * Which writer's shape a stored `organizations` row has.\n *\n * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.\n * - `pave`: PAVE 2.0's legacy own shape (`packages/db/src/models/organization.ts`\n * before pl-958): a string `id`, `acronym`, `location`. Any ONE of the three\n * marks it.\n * - `thread`: the shared Thread / auth / Platform / PAVE (pl-958) shape: a\n * `type` in either stored form — the IC-58 Int (legacy) or a non-empty string\n * (the CVC-48 key). Whether the value is a VALID type is not this rule's\n * question (the subpath stays SOT-free); readers resolve it.\n * - `unknown`: none of the above (no type, `type: null`, an empty string —\n * e.g. the Platform's account rows).\n *\n * The FORM of `type` never decides `pave`: `organizations.type` is moving from\n * the IC-58 Int to the CVC-48 key on every shared row (auth's provisioning\n * writes the key; the `migrate:org-type-keys` script rewrites the rest), so a\n * key string is a shared-shape row. On platform-prod / platform-dev\n * (2026-10-16) every PAVE-shape row carries all three markers and no\n * Thread / auth / canonical row carries any of them.\n */\nexport type OrganizationRowShape = \"canonical\" | \"pave\" | \"thread\" | \"unknown\";\n\nexport function organizationRowShape(raw: unknown): OrganizationRowShape {\n if (!raw || typeof raw !== \"object\") return \"unknown\";\n const r = raw as Record<string, unknown>;\n if (typeof r.canonicalKey === \"string\" && r.canonicalKey !== \"\") return \"canonical\";\n if (typeof r.id === \"string\" || \"acronym\" in r || \"location\" in r) return \"pave\";\n if (typeof r.type === \"number\" || (typeof r.type === \"string\" && r.type.trim() !== \"\")) {\n return \"thread\";\n }\n return \"unknown\";\n}\n\n/**\n * Whether `org` is `agency`'s OWN organization record: both halves of the pair\n * auth's `ensureOrganizationForAgency` writes hold — `org.agencyId` names the\n * agency AND `agency.organizationId` names the org. Neither half alone counts;\n * `agencyScope` (tenancy) never counts.\n */\nexport function isAgencyOwnOrganization(\n org: { _id?: unknown; agencyId?: unknown } | null | undefined,\n agency: { _id?: unknown; organizationId?: unknown } | null | undefined,\n): boolean {\n if (!org || !agency) return false;\n return (\n sameDirectoryId(org.agencyId, agency._id) && sameDirectoryId(agency.organizationId, org._id)\n );\n}\n\nexport interface DirectoryProgramView {\n programId: string;\n name: string;\n description: string | null;\n status: string | null;\n reviewStatus: OrganizationProgramRow[\"reviewStatus\"] | null;\n addedBy: string | null;\n addedInApp: string | null;\n /** Offered in pickers: not retired, not rejected. */\n live: boolean;\n}\n\n/** One program row as readers see it, or null when it fails the row schema. */\nexport function toDirectoryProgramView(raw: unknown): DirectoryProgramView | null {\n const parsed = OrganizationProgramRowSchema.safeParse(raw);\n if (!parsed.success) return null;\n const p = parsed.data;\n return {\n programId: p.programId,\n name: p.name,\n description: p.description ?? null,\n status: p.status ?? null,\n reviewStatus: p.reviewStatus ?? null,\n addedBy: p.addedBy ?? null,\n addedInApp: p.addedInApp ?? null,\n live: isLiveOrganizationProgram(p),\n };\n}\n\nexport interface DirectoryOrganizationView {\n /** `String(_id)`: the id listings and sharing settings key on. */\n id: string;\n shape: OrganizationRowShape;\n name: string;\n /** The stored `type` when it is a number (the legacy IC-58 Int), else null. */\n type: number | null;\n /**\n * The stored `type` when it is a string, else null: a CVC-48\n * `referral_entity_category` key on a shared-shape row once it is migrated\n * (or written by auth's provisioning), and on a legacy PAVE-shape row.\n */\n typeKey: string | null;\n /** PAVE's own string `id` on a PAVE-shape row, else null. */\n paveId: string | null;\n community: string | null;\n /** auth's identity half; see {@link isAgencyOwnOrganization}. */\n agencyId: string | null;\n /** Tenancy scope. Never an identity. */\n agencyScope: string | null;\n canonicalKey: string | null;\n programs: DirectoryProgramView[];\n /** Program rows that failed the row schema (not returned). */\n invalidPrograms: number;\n}\n\nconst text = (v: unknown): string | null => (typeof v === \"string\" && v !== \"\" ? v : null);\n\n/** Normalise a stored row of any writer's shape into the shared read view. */\nexport function toDirectoryOrganizationView(raw: unknown): DirectoryOrganizationView | null {\n if (!raw || typeof raw !== \"object\") return null;\n const r = raw as Record<string, unknown>;\n const id = directoryIdString(r._id);\n if (!id) return null;\n const rows = Array.isArray(r.programs) ? r.programs : [];\n const programs = rows\n .map(toDirectoryProgramView)\n .filter((p): p is DirectoryProgramView => p !== null);\n return {\n id,\n shape: organizationRowShape(r),\n name: text(r.name) ?? \"\",\n type: typeof r.type === \"number\" ? r.type : null,\n typeKey: typeof r.type === \"string\" ? r.type : null,\n paveId: text(r.id),\n community: text(r.community),\n agencyId: directoryIdString(r.agencyId),\n agencyScope: directoryIdString(r.agencyScope),\n canonicalKey: text(r.canonicalKey),\n programs,\n invalidPrograms: rows.length - programs.length,\n };\n}\n","/**\n * The raw-driver reads of the platform-db org directory. No driver import: the\n * readers take any object with the MongoDB driver's\n * `collection(name).findOne(filter, options)` shape, plus a `toObjectId`\n * factory so a 24-hex string id also matches a row stored under an ObjectId\n * (production `organizations._id` and `agencies._id` are ObjectIds).\n */\n\nimport {\n isAgencyOwnOrganization,\n toDirectoryOrganizationView,\n type DirectoryOrganizationView,\n type ObjectIdLike,\n} from \"./rules.js\";\n\nexport const ORGANIZATIONS_COLLECTION = \"organizations\";\nexport const AGENCIES_COLLECTION = \"agencies\";\n\nexport interface OrgDirectoryDbLike {\n collection(name: string): {\n findOne(filter: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;\n };\n}\n\nexport interface OrgDirectoryReadOptions {\n /** Build the driver's ObjectId from 24-hex (e.g. `(h) => new ObjectId(h)`). */\n toObjectId?: (hex: string) => unknown;\n}\n\nconst HEX = /^[0-9a-fA-F]{24}$/;\n\n/** Every stored form of one id: the string, and its ObjectId when 24-hex. */\nexport function organizationIdForms(\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): unknown[] {\n if (typeof id !== \"string\") return [id.toHexString(), id];\n return HEX.test(id) && opts.toObjectId ? [id, opts.toObjectId(id.toLowerCase())] : [id];\n}\n\n/** One organization in the shared view, or null. Read errors propagate. */\nexport async function readDirectoryOrganization(\n db: OrgDirectoryDbLike,\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const raw = await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(id, opts) } });\n return raw ? toDirectoryOrganizationView(raw) : null;\n}\n\n/**\n * An agency's OWN organization (both halves of the pair hold), or null — never\n * an organization the agency merely scopes (`agencyScope`) or that points at it\n * from one side only. The agency is looked up by `_id`.\n */\nexport async function readAgencyOwnOrganization(\n db: OrgDirectoryDbLike,\n agencyId: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const agency = (await db\n .collection(AGENCIES_COLLECTION)\n .findOne(\n { _id: { $in: organizationIdForms(agencyId, opts) } },\n { projection: { _id: 1, organizationId: 1 } },\n )) as { _id?: unknown; organizationId?: unknown } | null;\n if (!agency || agency.organizationId == null) return null;\n const pointer = agency.organizationId as string | ObjectIdLike;\n const org = (await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(pointer, opts) } })) as {\n _id?: unknown;\n agencyId?: unknown;\n } | null;\n return org && isAgencyOwnOrganization(org, agency) ? toDirectoryOrganizationView(org) : null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcO,IAAM,wBAAwB;AAAA,EACnC,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AACZ;AAMO,IAAM,+BAA+B,OAAO,OAAO;AAAA,EACxD,sBAAsB;AAAA,EACtB,sBAAsB;AAAA,EACtB,sBAAsB;AACxB,CAAU;AAEH,SAAS,sBAAsB,OAA8C;AAClF,SACE,OAAO,UAAU,YAAa,6BAAmD,SAAS,KAAK;AAEnG;AASO,IAAM,iBAAiB;AAAA,EAC5B,QAAQ;AAAA,EACR,SAAS;AACX;AAIO,IAAM,wBAAwB,OAAO,OAAO;AAAA,EACjD,eAAe;AAAA,EACf,eAAe;AACjB,CAAU;AAUH,SAAS,0BAA0B,KAG9B;AACV,SACE,IAAI,WAAW,eAAe,WAAW,IAAI,iBAAiB,sBAAsB;AAExF;AAQO,SAAS,eAAe,MAAsB;AACnD,SAAO,KAAK,KAAK,EAAE,YAAY;AACjC;;;ACzEA,iBAAkB;AAKX,IAAM,gCAAgC,OAAO,OAAO;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAU;AAIV,IAAM,eAAe,aAAE,OAAO,EAAE,QAAQ;AAOjC,IAAM,+BAA+B,aAAE,OAAO;AAAA,EACnD,WAAW,aA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d,IACA,OAAgC,CAAC,GACtB;AACX,MAAI,OAAO,OAAO,SAAU,QAAO,CAAC,GAAG,YAAY,GAAG,EAAE;AACxD,SAAO,IAAI,KAAK,EAAE,KAAK,KAAK,aAAa,CAAC,IAAI,KAAK,WAAW,GAAG,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE;AACxF;AAGA,eAAsB,0BACpB,IACA,IACA,OAAgC,CAAC,GACU;AAC3C,QAAM,MAAM,MAAM,GACf,WAAW,wBAAwB,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,IAAI,IAAI,EAAE,EAAE,CAAC;AAC1D,SAAO,MAAM,4BAA4B,GAAG,IAAI;AAClD;AAOA,eAAsB,0BACpB,IACA,UACA,OAAgC,CAAC,GACU;AAC3C,QAAM,SAAU,MAAM,GACnB,WAAW,mBAAmB,EAC9B;AAAA,IACC,EAAE,KAAK,EAAE,KAAK,oBAAoB,UAAU,IAAI,EAAE,EAAE;AAAA,IACpD,EAAE,YAAY,EAAE,KAAK,GAAG,gBAAgB,EAAE,EAAE;AAAA,EAC9C;AACF,MAAI,CAAC,UAAU,OAAO,kBAAkB,KAAM,QAAO;AACrD,QAAM,UAAU,OAAO;AACvB,QAAM,MAAO,MAAM,GAChB,WAAW,wBAAwB,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,SAAS,IAAI,EAAE,EAAE,CAAC;AAI/D,SAAO,OAAO,wBAAwB,KAAK,MAAM,IAAI,4BAA4B,GAAG,IAAI;AAC1F;","names":[]}
@@ -69,10 +69,22 @@ declare function sameDirectoryId(a: unknown, b: unknown): boolean;
69
69
  * Which writer's shape a stored `organizations` row has.
70
70
  *
71
71
  * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.
72
- * - `pave`: PAVE 2.0's shape (`packages/db/src/models/organization.ts`):
73
- * a string `id`, `type` as a CVC-48 key, `acronym`, `location`.
74
- * - `thread`: the Thread / auth shape: `type` an IC-58 Int.
75
- * - `unknown`: none of the above.
72
+ * - `pave`: PAVE 2.0's legacy own shape (`packages/db/src/models/organization.ts`
73
+ * before pl-958): a string `id`, `acronym`, `location`. Any ONE of the three
74
+ * marks it.
75
+ * - `thread`: the shared Thread / auth / Platform / PAVE (pl-958) shape: a
76
+ * `type` in either stored form — the IC-58 Int (legacy) or a non-empty string
77
+ * (the CVC-48 key). Whether the value is a VALID type is not this rule's
78
+ * question (the subpath stays SOT-free); readers resolve it.
79
+ * - `unknown`: none of the above (no type, `type: null`, an empty string —
80
+ * e.g. the Platform's account rows).
81
+ *
82
+ * The FORM of `type` never decides `pave`: `organizations.type` is moving from
83
+ * the IC-58 Int to the CVC-48 key on every shared row (auth's provisioning
84
+ * writes the key; the `migrate:org-type-keys` script rewrites the rest), so a
85
+ * key string is a shared-shape row. On platform-prod / platform-dev
86
+ * (2026-10-16) every PAVE-shape row carries all three markers and no
87
+ * Thread / auth / canonical row carries any of them.
76
88
  */
77
89
  type OrganizationRowShape = "canonical" | "pave" | "thread" | "unknown";
78
90
  declare function organizationRowShape(raw: unknown): OrganizationRowShape;
@@ -107,9 +119,13 @@ interface DirectoryOrganizationView {
107
119
  id: string;
108
120
  shape: OrganizationRowShape;
109
121
  name: string;
110
- /** IC-58 Int (Thread / auth shape), else null. */
122
+ /** The stored `type` when it is a number (the legacy IC-58 Int), else null. */
111
123
  type: number | null;
112
- /** CVC-48 `referral_entity_category` key (PAVE shape), else null. */
124
+ /**
125
+ * The stored `type` when it is a string, else null: a CVC-48
126
+ * `referral_entity_category` key on a shared-shape row once it is migrated
127
+ * (or written by auth's provisioning), and on a legacy PAVE-shape row.
128
+ */
113
129
  typeKey: string | null;
114
130
  /** PAVE's own string `id` on a PAVE-shape row, else null. */
115
131
  paveId: string | null;
@@ -69,10 +69,22 @@ declare function sameDirectoryId(a: unknown, b: unknown): boolean;
69
69
  * Which writer's shape a stored `organizations` row has.
70
70
  *
71
71
  * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.
72
- * - `pave`: PAVE 2.0's shape (`packages/db/src/models/organization.ts`):
73
- * a string `id`, `type` as a CVC-48 key, `acronym`, `location`.
74
- * - `thread`: the Thread / auth shape: `type` an IC-58 Int.
75
- * - `unknown`: none of the above.
72
+ * - `pave`: PAVE 2.0's legacy own shape (`packages/db/src/models/organization.ts`
73
+ * before pl-958): a string `id`, `acronym`, `location`. Any ONE of the three
74
+ * marks it.
75
+ * - `thread`: the shared Thread / auth / Platform / PAVE (pl-958) shape: a
76
+ * `type` in either stored form — the IC-58 Int (legacy) or a non-empty string
77
+ * (the CVC-48 key). Whether the value is a VALID type is not this rule's
78
+ * question (the subpath stays SOT-free); readers resolve it.
79
+ * - `unknown`: none of the above (no type, `type: null`, an empty string —
80
+ * e.g. the Platform's account rows).
81
+ *
82
+ * The FORM of `type` never decides `pave`: `organizations.type` is moving from
83
+ * the IC-58 Int to the CVC-48 key on every shared row (auth's provisioning
84
+ * writes the key; the `migrate:org-type-keys` script rewrites the rest), so a
85
+ * key string is a shared-shape row. On platform-prod / platform-dev
86
+ * (2026-10-16) every PAVE-shape row carries all three markers and no
87
+ * Thread / auth / canonical row carries any of them.
76
88
  */
77
89
  type OrganizationRowShape = "canonical" | "pave" | "thread" | "unknown";
78
90
  declare function organizationRowShape(raw: unknown): OrganizationRowShape;
@@ -107,9 +119,13 @@ interface DirectoryOrganizationView {
107
119
  id: string;
108
120
  shape: OrganizationRowShape;
109
121
  name: string;
110
- /** IC-58 Int (Thread / auth shape), else null. */
122
+ /** The stored `type` when it is a number (the legacy IC-58 Int), else null. */
111
123
  type: number | null;
112
- /** CVC-48 `referral_entity_category` key (PAVE shape), else null. */
124
+ /**
125
+ * The stored `type` when it is a string, else null: a CVC-48
126
+ * `referral_entity_category` key on a shared-shape row once it is migrated
127
+ * (or written by auth's provisioning), and on a legacy PAVE-shape row.
128
+ */
113
129
  typeKey: string | null;
114
130
  /** PAVE's own string `id` on a PAVE-shape row, else null. */
115
131
  paveId: string | null;
@@ -70,9 +70,10 @@ function organizationRowShape(raw) {
70
70
  if (!raw || typeof raw !== "object") return "unknown";
71
71
  const r = raw;
72
72
  if (typeof r.canonicalKey === "string" && r.canonicalKey !== "") return "canonical";
73
- if (typeof r.id === "string" || typeof r.type === "string" || "acronym" in r || "location" in r)
74
- return "pave";
75
- if (typeof r.type === "number") return "thread";
73
+ if (typeof r.id === "string" || "acronym" in r || "location" in r) return "pave";
74
+ if (typeof r.type === "number" || typeof r.type === "string" && r.type.trim() !== "") {
75
+ return "thread";
76
+ }
76
77
  return "unknown";
77
78
  }
78
79
  function isAgencyOwnOrganization(org, agency) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/orgDirectory/vocabulary.ts","../../src/orgDirectory/schemas.ts","../../src/orgDirectory/rules.ts","../../src/orgDirectory/read.ts"],"sourcesContent":["/**\n * The program-row vocabulary of `organizations.programs[]`: the review\n * lifecycle, the lifecycle codes, the live rule and the name key. Pure (no\n * mongoose): `schemas/organization-program.ts` re-exports these, so the\n * Mongoose fragment and the `org-directory` read contract share one source.\n */\n\n// ─── Review status ──────────────────────────────────────────────────\n\n/**\n * Review lifecycle of a program named inside an app (UDD Program\n * `review_status`). The sheet's own words — a lane's `pending_steward` /\n * `declined` never reach the stored row.\n */\nexport const PROGRAM_REVIEW_STATUS = {\n PENDING: \"pending\",\n CONFIRMED: \"confirmed\",\n REJECTED: \"rejected\",\n} as const;\n\nexport type ProgramReviewStatus =\n (typeof PROGRAM_REVIEW_STATUS)[keyof typeof PROGRAM_REVIEW_STATUS];\n\n/** The three values, for `enum:` declarations and Zod `z.enum`. */\nexport const PROGRAM_REVIEW_STATUS_VALUES = Object.freeze([\n PROGRAM_REVIEW_STATUS.PENDING,\n PROGRAM_REVIEW_STATUS.CONFIRMED,\n PROGRAM_REVIEW_STATUS.REJECTED,\n] as const);\n\nexport function isProgramReviewStatus(value: unknown): value is ProgramReviewStatus {\n return (\n typeof value === \"string\" && (PROGRAM_REVIEW_STATUS_VALUES as readonly string[]).includes(value)\n );\n}\n\n// ─── Lifecycle status ───────────────────────────────────────────────\n\n/**\n * The program lifecycle codes the writers store. `active` is what PAVE 2.0's\n * `POST /organizations/:id/programs` writes; `retired` takes a program out of\n * every picker. Absent reads as active.\n */\nexport const PROGRAM_STATUS = {\n ACTIVE: \"active\",\n RETIRED: \"retired\",\n} as const;\n\nexport type ProgramStatus = (typeof PROGRAM_STATUS)[keyof typeof PROGRAM_STATUS];\n\nexport const PROGRAM_STATUS_VALUES = Object.freeze([\n PROGRAM_STATUS.ACTIVE,\n PROGRAM_STATUS.RETIRED,\n] as const);\n\n// ─── Live rule and name key ─────────────────────────────────────────\n\n/**\n * Whether a program row may be OFFERED (and matched by name on an add).\n * A retired or rejected program is not a place anyone can be enrolled today,\n * so pickers do not list it and typing its name mints a new row rather than\n * binding to it (the prototype's independent pre-review catch, 2026-10-06).\n */\nexport function isLiveOrganizationProgram(row: {\n status?: string | null | undefined;\n reviewStatus?: string | null | undefined;\n}): boolean {\n return (\n row.status !== PROGRAM_STATUS.RETIRED && row.reviewStatus !== PROGRAM_REVIEW_STATUS.REJECTED\n );\n}\n\n/**\n * The key two program names match on: trimmed, lower-cased. Canon:\n * `directoryProgramFor` (platform-prototype `src/surveys.jsx` L4233 @\n * `0fa04c1`); PAVE 2.0's `POST /organizations/:id/programs` dedupes the same\n * way.\n */\nexport function programNameKey(name: string): string {\n return name.trim().toLowerCase();\n}\n","/**\n * The stored program row of `organizations.programs[]`, as a Zod schema for\n * readers. The Mongoose definition (`schemas/organization-program.ts`,\n * `organizationProgramSchemaDefinition`) is the write-side twin; a test holds\n * the key lists equal.\n */\n\nimport { z } from \"zod\";\n\nimport { PROGRAM_REVIEW_STATUS_VALUES } from \"./vocabulary.js\";\n\n/** The program row's keys, in the fragment's declaration order. */\nexport const ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([\n \"programId\",\n \"name\",\n \"description\",\n \"status\",\n \"defaultSharingPolicy\",\n \"reviewStatus\",\n \"addedBy\",\n \"addedInApp\",\n \"addedInSession\",\n] as const);\n\nexport type OrganizationProgramRowKey = (typeof ORGANIZATION_PROGRAM_ROW_KEYS)[number];\n\nconst optionalText = z.string().nullish();\n\n/**\n * One stored program row. `programId` + `name` are required; the rest are\n * optional and may be null. Unknown keys are stripped (a reader never relies\n * on a key outside the fragment).\n */\nexport const OrganizationProgramRowSchema = z.object({\n programId: z.string().min(1),\n name: z.string().min(1),\n description: optionalText,\n status: optionalText,\n defaultSharingPolicy: optionalText,\n reviewStatus: z\n .enum(PROGRAM_REVIEW_STATUS_VALUES as unknown as [\"pending\", \"confirmed\", \"rejected\"])\n .nullish(),\n addedBy: optionalText,\n addedInApp: optionalText,\n addedInSession: optionalText,\n});\n\nexport type OrganizationProgramRow = z.infer<typeof OrganizationProgramRowSchema>;\n","/**\n * Read rules for the platform-db org directory: which row shape a document\n * is, when an organization is an agency's OWN record, and the normalised read\n * view every app shares. Pure: no mongoose, no driver.\n *\n * Contract: platform `docs/ORG_DIRECTORY_CONTRACT.md`.\n */\n\nimport { OrganizationProgramRowSchema, type OrganizationProgramRow } from \"./schemas.js\";\nimport { isLiveOrganizationProgram } from \"./vocabulary.js\";\n\n/** A driver ObjectId, or anything that prints itself as one. */\nexport interface ObjectIdLike {\n toHexString(): string;\n}\n\n/** An id's comparable string form: an ObjectId's hex, a string as-is, else null. */\nexport function directoryIdString(value: unknown): string | null {\n if (typeof value === \"string\") return value === \"\" ? null : value;\n if (\n value &&\n typeof value === \"object\" &&\n typeof (value as ObjectIdLike).toHexString === \"function\"\n ) {\n return (value as ObjectIdLike).toHexString();\n }\n return null;\n}\n\n/** Two ids name the same row (string vs ObjectId of one hex are the same). Absent is never a match. */\nexport function sameDirectoryId(a: unknown, b: unknown): boolean {\n const x = directoryIdString(a);\n const y = directoryIdString(b);\n return x !== null && y !== null && x.toLowerCase() === y.toLowerCase();\n}\n\n/**\n * Which writer's shape a stored `organizations` row has.\n *\n * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.\n * - `pave`: PAVE 2.0's shape (`packages/db/src/models/organization.ts`):\n * a string `id`, `type` as a CVC-48 key, `acronym`, `location`.\n * - `thread`: the Thread / auth shape: `type` an IC-58 Int.\n * - `unknown`: none of the above.\n */\nexport type OrganizationRowShape = \"canonical\" | \"pave\" | \"thread\" | \"unknown\";\n\nexport function organizationRowShape(raw: unknown): OrganizationRowShape {\n if (!raw || typeof raw !== \"object\") return \"unknown\";\n const r = raw as Record<string, unknown>;\n if (typeof r.canonicalKey === \"string\" && r.canonicalKey !== \"\") return \"canonical\";\n if (typeof r.id === \"string\" || typeof r.type === \"string\" || \"acronym\" in r || \"location\" in r)\n return \"pave\";\n if (typeof r.type === \"number\") return \"thread\";\n return \"unknown\";\n}\n\n/**\n * Whether `org` is `agency`'s OWN organization record: both halves of the pair\n * auth's `ensureOrganizationForAgency` writes hold — `org.agencyId` names the\n * agency AND `agency.organizationId` names the org. Neither half alone counts;\n * `agencyScope` (tenancy) never counts.\n */\nexport function isAgencyOwnOrganization(\n org: { _id?: unknown; agencyId?: unknown } | null | undefined,\n agency: { _id?: unknown; organizationId?: unknown } | null | undefined,\n): boolean {\n if (!org || !agency) return false;\n return (\n sameDirectoryId(org.agencyId, agency._id) && sameDirectoryId(agency.organizationId, org._id)\n );\n}\n\nexport interface DirectoryProgramView {\n programId: string;\n name: string;\n description: string | null;\n status: string | null;\n reviewStatus: OrganizationProgramRow[\"reviewStatus\"] | null;\n addedBy: string | null;\n addedInApp: string | null;\n /** Offered in pickers: not retired, not rejected. */\n live: boolean;\n}\n\n/** One program row as readers see it, or null when it fails the row schema. */\nexport function toDirectoryProgramView(raw: unknown): DirectoryProgramView | null {\n const parsed = OrganizationProgramRowSchema.safeParse(raw);\n if (!parsed.success) return null;\n const p = parsed.data;\n return {\n programId: p.programId,\n name: p.name,\n description: p.description ?? null,\n status: p.status ?? null,\n reviewStatus: p.reviewStatus ?? null,\n addedBy: p.addedBy ?? null,\n addedInApp: p.addedInApp ?? null,\n live: isLiveOrganizationProgram(p),\n };\n}\n\nexport interface DirectoryOrganizationView {\n /** `String(_id)`: the id listings and sharing settings key on. */\n id: string;\n shape: OrganizationRowShape;\n name: string;\n /** IC-58 Int (Thread / auth shape), else null. */\n type: number | null;\n /** CVC-48 `referral_entity_category` key (PAVE shape), else null. */\n typeKey: string | null;\n /** PAVE's own string `id` on a PAVE-shape row, else null. */\n paveId: string | null;\n community: string | null;\n /** auth's identity half; see {@link isAgencyOwnOrganization}. */\n agencyId: string | null;\n /** Tenancy scope. Never an identity. */\n agencyScope: string | null;\n canonicalKey: string | null;\n programs: DirectoryProgramView[];\n /** Program rows that failed the row schema (not returned). */\n invalidPrograms: number;\n}\n\nconst text = (v: unknown): string | null => (typeof v === \"string\" && v !== \"\" ? v : null);\n\n/** Normalise a stored row of any writer's shape into the shared read view. */\nexport function toDirectoryOrganizationView(raw: unknown): DirectoryOrganizationView | null {\n if (!raw || typeof raw !== \"object\") return null;\n const r = raw as Record<string, unknown>;\n const id = directoryIdString(r._id);\n if (!id) return null;\n const rows = Array.isArray(r.programs) ? r.programs : [];\n const programs = rows\n .map(toDirectoryProgramView)\n .filter((p): p is DirectoryProgramView => p !== null);\n return {\n id,\n shape: organizationRowShape(r),\n name: text(r.name) ?? \"\",\n type: typeof r.type === \"number\" ? r.type : null,\n typeKey: typeof r.type === \"string\" ? r.type : null,\n paveId: text(r.id),\n community: text(r.community),\n agencyId: directoryIdString(r.agencyId),\n agencyScope: directoryIdString(r.agencyScope),\n canonicalKey: text(r.canonicalKey),\n programs,\n invalidPrograms: rows.length - programs.length,\n };\n}\n","/**\n * The raw-driver reads of the platform-db org directory. No driver import: the\n * readers take any object with the MongoDB driver's\n * `collection(name).findOne(filter, options)` shape, plus a `toObjectId`\n * factory so a 24-hex string id also matches a row stored under an ObjectId\n * (production `organizations._id` and `agencies._id` are ObjectIds).\n */\n\nimport {\n isAgencyOwnOrganization,\n toDirectoryOrganizationView,\n type DirectoryOrganizationView,\n type ObjectIdLike,\n} from \"./rules.js\";\n\nexport const ORGANIZATIONS_COLLECTION = \"organizations\";\nexport const AGENCIES_COLLECTION = \"agencies\";\n\nexport interface OrgDirectoryDbLike {\n collection(name: string): {\n findOne(filter: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;\n };\n}\n\nexport interface OrgDirectoryReadOptions {\n /** Build the driver's ObjectId from 24-hex (e.g. `(h) => new ObjectId(h)`). */\n toObjectId?: (hex: string) => unknown;\n}\n\nconst HEX = /^[0-9a-fA-F]{24}$/;\n\n/** Every stored form of one id: the string, and its ObjectId when 24-hex. */\nexport function organizationIdForms(\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): unknown[] {\n if (typeof id !== \"string\") return [id.toHexString(), id];\n return HEX.test(id) && opts.toObjectId ? [id, opts.toObjectId(id.toLowerCase())] : [id];\n}\n\n/** One organization in the shared view, or null. Read errors propagate. */\nexport async function readDirectoryOrganization(\n db: OrgDirectoryDbLike,\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const raw = await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(id, opts) } });\n return raw ? toDirectoryOrganizationView(raw) : null;\n}\n\n/**\n * An agency's OWN organization (both halves of the pair hold), or null — never\n * an organization the agency merely scopes (`agencyScope`) or that points at it\n * from one side only. The agency is looked up by `_id`.\n */\nexport async function readAgencyOwnOrganization(\n db: OrgDirectoryDbLike,\n agencyId: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const agency = (await db\n .collection(AGENCIES_COLLECTION)\n .findOne(\n { _id: { $in: organizationIdForms(agencyId, opts) } },\n { projection: { _id: 1, organizationId: 1 } },\n )) as { _id?: unknown; organizationId?: unknown } | null;\n if (!agency || agency.organizationId == null) return null;\n const pointer = agency.organizationId as string | ObjectIdLike;\n const org = (await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(pointer, opts) } })) as {\n _id?: unknown;\n agencyId?: unknown;\n } | null;\n return org && isAgencyOwnOrganization(org, agency) ? toDirectoryOrganizationView(org) : null;\n}\n"],"mappings":";AAcO,IAAM,wBAAwB;AAAA,EACnC,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AACZ;AAMO,IAAM,+BAA+B,OAAO,OAAO;AAAA,EACxD,sBAAsB;AAAA,EACtB,sBAAsB;AAAA,EACtB,sBAAsB;AACxB,CAAU;AAEH,SAAS,sBAAsB,OAA8C;AAClF,SACE,OAAO,UAAU,YAAa,6BAAmD,SAAS,KAAK;AAEnG;AASO,IAAM,iBAAiB;AAAA,EAC5B,QAAQ;AAAA,EACR,SAAS;AACX;AAIO,IAAM,wBAAwB,OAAO,OAAO;AAAA,EACjD,eAAe;AAAA,EACf,eAAe;AACjB,CAAU;AAUH,SAAS,0BAA0B,KAG9B;AACV,SACE,IAAI,WAAW,eAAe,WAAW,IAAI,iBAAiB,sBAAsB;AAExF;AAQO,SAAS,eAAe,MAAsB;AACnD,SAAO,KAAK,KAAK,EAAE,YAAY;AACjC;;;ACzEA,SAAS,SAAS;AAKX,IAAM,gCAAgC,OAAO,OAAO;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAU;AAIV,IAAM,eAAe,EAAE,OAAO,EAAE,QAAQ;AAOjC,IAAM,+BAA+B,EAAE,OAAO;AAAA,EACnD,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC3B,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EACtB,aAAa;AAAA,EACb,QAAQ;AAAA,EACR,sBAAsB;AAAA,EACtB,cAAc,EACX,KAAK,4BAA+E,EACpF,QAAQ;AAAA,EACX,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,gBAAgB;AAClB,CAAC;;;AC5BM,SA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,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,IAAI,IAAI,EAAE,EAAE,CAAC;AAC1D,SAAO,MAAM,4BAA4B,GAAG,IAAI;AAClD;AAOA,eAAsB,0BACpB,IACA,UACA,OAAgC,CAAC,GACU;AAC3C,QAAM,SAAU,MAAM,GACnB,WAAW,mBAAmB,EAC9B;AAAA,IACC,EAAE,KAAK,EAAE,KAAK,oBAAoB,UAAU,IAAI,EAAE,EAAE;AAAA,IACpD,EAAE,YAAY,EAAE,KAAK,GAAG,gBAAgB,EAAE,EAAE;AAAA,EAC9C;AACF,MAAI,CAAC,UAAU,OAAO,kBAAkB,KAAM,QAAO;AACrD,QAAM,UAAU,OAAO;AACvB,QAAM,MAAO,MAAM,GAChB,WAAW,wBAAwB,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,SAAS,IAAI,EAAE,EAAE,CAAC;AAI/D,SAAO,OAAO,wBAAwB,KAAK,MAAM,IAAI,4BAA4B,GAAG,IAAI;AAC1F;","names":[]}
1
+ {"version":3,"sources":["../../src/orgDirectory/vocabulary.ts","../../src/orgDirectory/schemas.ts","../../src/orgDirectory/rules.ts","../../src/orgDirectory/read.ts"],"sourcesContent":["/**\n * The program-row vocabulary of `organizations.programs[]`: the review\n * lifecycle, the lifecycle codes, the live rule and the name key. Pure (no\n * mongoose): `schemas/organization-program.ts` re-exports these, so the\n * Mongoose fragment and the `org-directory` read contract share one source.\n */\n\n// ─── Review status ──────────────────────────────────────────────────\n\n/**\n * Review lifecycle of a program named inside an app (UDD Program\n * `review_status`). The sheet's own words — a lane's `pending_steward` /\n * `declined` never reach the stored row.\n */\nexport const PROGRAM_REVIEW_STATUS = {\n PENDING: \"pending\",\n CONFIRMED: \"confirmed\",\n REJECTED: \"rejected\",\n} as const;\n\nexport type ProgramReviewStatus =\n (typeof PROGRAM_REVIEW_STATUS)[keyof typeof PROGRAM_REVIEW_STATUS];\n\n/** The three values, for `enum:` declarations and Zod `z.enum`. */\nexport const PROGRAM_REVIEW_STATUS_VALUES = Object.freeze([\n PROGRAM_REVIEW_STATUS.PENDING,\n PROGRAM_REVIEW_STATUS.CONFIRMED,\n PROGRAM_REVIEW_STATUS.REJECTED,\n] as const);\n\nexport function isProgramReviewStatus(value: unknown): value is ProgramReviewStatus {\n return (\n typeof value === \"string\" && (PROGRAM_REVIEW_STATUS_VALUES as readonly string[]).includes(value)\n );\n}\n\n// ─── Lifecycle status ───────────────────────────────────────────────\n\n/**\n * The program lifecycle codes the writers store. `active` is what PAVE 2.0's\n * `POST /organizations/:id/programs` writes; `retired` takes a program out of\n * every picker. Absent reads as active.\n */\nexport const PROGRAM_STATUS = {\n ACTIVE: \"active\",\n RETIRED: \"retired\",\n} as const;\n\nexport type ProgramStatus = (typeof PROGRAM_STATUS)[keyof typeof PROGRAM_STATUS];\n\nexport const PROGRAM_STATUS_VALUES = Object.freeze([\n PROGRAM_STATUS.ACTIVE,\n PROGRAM_STATUS.RETIRED,\n] as const);\n\n// ─── Live rule and name key ─────────────────────────────────────────\n\n/**\n * Whether a program row may be OFFERED (and matched by name on an add).\n * A retired or rejected program is not a place anyone can be enrolled today,\n * so pickers do not list it and typing its name mints a new row rather than\n * binding to it (the prototype's independent pre-review catch, 2026-10-06).\n */\nexport function isLiveOrganizationProgram(row: {\n status?: string | null | undefined;\n reviewStatus?: string | null | undefined;\n}): boolean {\n return (\n row.status !== PROGRAM_STATUS.RETIRED && row.reviewStatus !== PROGRAM_REVIEW_STATUS.REJECTED\n );\n}\n\n/**\n * The key two program names match on: trimmed, lower-cased. Canon:\n * `directoryProgramFor` (platform-prototype `src/surveys.jsx` L4233 @\n * `0fa04c1`); PAVE 2.0's `POST /organizations/:id/programs` dedupes the same\n * way.\n */\nexport function programNameKey(name: string): string {\n return name.trim().toLowerCase();\n}\n","/**\n * The stored program row of `organizations.programs[]`, as a Zod schema for\n * readers. The Mongoose definition (`schemas/organization-program.ts`,\n * `organizationProgramSchemaDefinition`) is the write-side twin; a test holds\n * the key lists equal.\n */\n\nimport { z } from \"zod\";\n\nimport { PROGRAM_REVIEW_STATUS_VALUES } from \"./vocabulary.js\";\n\n/** The program row's keys, in the fragment's declaration order. */\nexport const ORGANIZATION_PROGRAM_ROW_KEYS = Object.freeze([\n \"programId\",\n \"name\",\n \"description\",\n \"status\",\n \"defaultSharingPolicy\",\n \"reviewStatus\",\n \"addedBy\",\n \"addedInApp\",\n \"addedInSession\",\n] as const);\n\nexport type OrganizationProgramRowKey = (typeof ORGANIZATION_PROGRAM_ROW_KEYS)[number];\n\nconst optionalText = z.string().nullish();\n\n/**\n * One stored program row. `programId` + `name` are required; the rest are\n * optional and may be null. Unknown keys are stripped (a reader never relies\n * on a key outside the fragment).\n */\nexport const OrganizationProgramRowSchema = z.object({\n programId: z.string().min(1),\n name: z.string().min(1),\n description: optionalText,\n status: optionalText,\n defaultSharingPolicy: optionalText,\n reviewStatus: z\n .enum(PROGRAM_REVIEW_STATUS_VALUES as unknown as [\"pending\", \"confirmed\", \"rejected\"])\n .nullish(),\n addedBy: optionalText,\n addedInApp: optionalText,\n addedInSession: optionalText,\n});\n\nexport type OrganizationProgramRow = z.infer<typeof OrganizationProgramRowSchema>;\n","/**\n * Read rules for the platform-db org directory: which row shape a document\n * is, when an organization is an agency's OWN record, and the normalised read\n * view every app shares. Pure: no mongoose, no driver.\n *\n * Contract: platform `docs/ORG_DIRECTORY_CONTRACT.md`.\n */\n\nimport { OrganizationProgramRowSchema, type OrganizationProgramRow } from \"./schemas.js\";\nimport { isLiveOrganizationProgram } from \"./vocabulary.js\";\n\n/** A driver ObjectId, or anything that prints itself as one. */\nexport interface ObjectIdLike {\n toHexString(): string;\n}\n\n/** An id's comparable string form: an ObjectId's hex, a string as-is, else null. */\nexport function directoryIdString(value: unknown): string | null {\n if (typeof value === \"string\") return value === \"\" ? null : value;\n if (\n value &&\n typeof value === \"object\" &&\n typeof (value as ObjectIdLike).toHexString === \"function\"\n ) {\n return (value as ObjectIdLike).toHexString();\n }\n return null;\n}\n\n/** Two ids name the same row (string vs ObjectId of one hex are the same). Absent is never a match. */\nexport function sameDirectoryId(a: unknown, b: unknown): boolean {\n const x = directoryIdString(a);\n const y = directoryIdString(b);\n return x !== null && y !== null && x.toLowerCase() === y.toLowerCase();\n}\n\n/**\n * Which writer's shape a stored `organizations` row has.\n *\n * - `canonical`: auth / Thread's global singletons (Unknown, Anonymous), marked by `canonicalKey`.\n * - `pave`: PAVE 2.0's legacy own shape (`packages/db/src/models/organization.ts`\n * before pl-958): a string `id`, `acronym`, `location`. Any ONE of the three\n * marks it.\n * - `thread`: the shared Thread / auth / Platform / PAVE (pl-958) shape: a\n * `type` in either stored form — the IC-58 Int (legacy) or a non-empty string\n * (the CVC-48 key). Whether the value is a VALID type is not this rule's\n * question (the subpath stays SOT-free); readers resolve it.\n * - `unknown`: none of the above (no type, `type: null`, an empty string —\n * e.g. the Platform's account rows).\n *\n * The FORM of `type` never decides `pave`: `organizations.type` is moving from\n * the IC-58 Int to the CVC-48 key on every shared row (auth's provisioning\n * writes the key; the `migrate:org-type-keys` script rewrites the rest), so a\n * key string is a shared-shape row. On platform-prod / platform-dev\n * (2026-10-16) every PAVE-shape row carries all three markers and no\n * Thread / auth / canonical row carries any of them.\n */\nexport type OrganizationRowShape = \"canonical\" | \"pave\" | \"thread\" | \"unknown\";\n\nexport function organizationRowShape(raw: unknown): OrganizationRowShape {\n if (!raw || typeof raw !== \"object\") return \"unknown\";\n const r = raw as Record<string, unknown>;\n if (typeof r.canonicalKey === \"string\" && r.canonicalKey !== \"\") return \"canonical\";\n if (typeof r.id === \"string\" || \"acronym\" in r || \"location\" in r) return \"pave\";\n if (typeof r.type === \"number\" || (typeof r.type === \"string\" && r.type.trim() !== \"\")) {\n return \"thread\";\n }\n return \"unknown\";\n}\n\n/**\n * Whether `org` is `agency`'s OWN organization record: both halves of the pair\n * auth's `ensureOrganizationForAgency` writes hold — `org.agencyId` names the\n * agency AND `agency.organizationId` names the org. Neither half alone counts;\n * `agencyScope` (tenancy) never counts.\n */\nexport function isAgencyOwnOrganization(\n org: { _id?: unknown; agencyId?: unknown } | null | undefined,\n agency: { _id?: unknown; organizationId?: unknown } | null | undefined,\n): boolean {\n if (!org || !agency) return false;\n return (\n sameDirectoryId(org.agencyId, agency._id) && sameDirectoryId(agency.organizationId, org._id)\n );\n}\n\nexport interface DirectoryProgramView {\n programId: string;\n name: string;\n description: string | null;\n status: string | null;\n reviewStatus: OrganizationProgramRow[\"reviewStatus\"] | null;\n addedBy: string | null;\n addedInApp: string | null;\n /** Offered in pickers: not retired, not rejected. */\n live: boolean;\n}\n\n/** One program row as readers see it, or null when it fails the row schema. */\nexport function toDirectoryProgramView(raw: unknown): DirectoryProgramView | null {\n const parsed = OrganizationProgramRowSchema.safeParse(raw);\n if (!parsed.success) return null;\n const p = parsed.data;\n return {\n programId: p.programId,\n name: p.name,\n description: p.description ?? null,\n status: p.status ?? null,\n reviewStatus: p.reviewStatus ?? null,\n addedBy: p.addedBy ?? null,\n addedInApp: p.addedInApp ?? null,\n live: isLiveOrganizationProgram(p),\n };\n}\n\nexport interface DirectoryOrganizationView {\n /** `String(_id)`: the id listings and sharing settings key on. */\n id: string;\n shape: OrganizationRowShape;\n name: string;\n /** The stored `type` when it is a number (the legacy IC-58 Int), else null. */\n type: number | null;\n /**\n * The stored `type` when it is a string, else null: a CVC-48\n * `referral_entity_category` key on a shared-shape row once it is migrated\n * (or written by auth's provisioning), and on a legacy PAVE-shape row.\n */\n typeKey: string | null;\n /** PAVE's own string `id` on a PAVE-shape row, else null. */\n paveId: string | null;\n community: string | null;\n /** auth's identity half; see {@link isAgencyOwnOrganization}. */\n agencyId: string | null;\n /** Tenancy scope. Never an identity. */\n agencyScope: string | null;\n canonicalKey: string | null;\n programs: DirectoryProgramView[];\n /** Program rows that failed the row schema (not returned). */\n invalidPrograms: number;\n}\n\nconst text = (v: unknown): string | null => (typeof v === \"string\" && v !== \"\" ? v : null);\n\n/** Normalise a stored row of any writer's shape into the shared read view. */\nexport function toDirectoryOrganizationView(raw: unknown): DirectoryOrganizationView | null {\n if (!raw || typeof raw !== \"object\") return null;\n const r = raw as Record<string, unknown>;\n const id = directoryIdString(r._id);\n if (!id) return null;\n const rows = Array.isArray(r.programs) ? r.programs : [];\n const programs = rows\n .map(toDirectoryProgramView)\n .filter((p): p is DirectoryProgramView => p !== null);\n return {\n id,\n shape: organizationRowShape(r),\n name: text(r.name) ?? \"\",\n type: typeof r.type === \"number\" ? r.type : null,\n typeKey: typeof r.type === \"string\" ? r.type : null,\n paveId: text(r.id),\n community: text(r.community),\n agencyId: directoryIdString(r.agencyId),\n agencyScope: directoryIdString(r.agencyScope),\n canonicalKey: text(r.canonicalKey),\n programs,\n invalidPrograms: rows.length - programs.length,\n };\n}\n","/**\n * The raw-driver reads of the platform-db org directory. No driver import: the\n * readers take any object with the MongoDB driver's\n * `collection(name).findOne(filter, options)` shape, plus a `toObjectId`\n * factory so a 24-hex string id also matches a row stored under an ObjectId\n * (production `organizations._id` and `agencies._id` are ObjectIds).\n */\n\nimport {\n isAgencyOwnOrganization,\n toDirectoryOrganizationView,\n type DirectoryOrganizationView,\n type ObjectIdLike,\n} from \"./rules.js\";\n\nexport const ORGANIZATIONS_COLLECTION = \"organizations\";\nexport const AGENCIES_COLLECTION = \"agencies\";\n\nexport interface OrgDirectoryDbLike {\n collection(name: string): {\n findOne(filter: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;\n };\n}\n\nexport interface OrgDirectoryReadOptions {\n /** Build the driver's ObjectId from 24-hex (e.g. `(h) => new ObjectId(h)`). */\n toObjectId?: (hex: string) => unknown;\n}\n\nconst HEX = /^[0-9a-fA-F]{24}$/;\n\n/** Every stored form of one id: the string, and its ObjectId when 24-hex. */\nexport function organizationIdForms(\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): unknown[] {\n if (typeof id !== \"string\") return [id.toHexString(), id];\n return HEX.test(id) && opts.toObjectId ? [id, opts.toObjectId(id.toLowerCase())] : [id];\n}\n\n/** One organization in the shared view, or null. Read errors propagate. */\nexport async function readDirectoryOrganization(\n db: OrgDirectoryDbLike,\n id: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const raw = await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(id, opts) } });\n return raw ? toDirectoryOrganizationView(raw) : null;\n}\n\n/**\n * An agency's OWN organization (both halves of the pair hold), or null — never\n * an organization the agency merely scopes (`agencyScope`) or that points at it\n * from one side only. The agency is looked up by `_id`.\n */\nexport async function readAgencyOwnOrganization(\n db: OrgDirectoryDbLike,\n agencyId: string | ObjectIdLike,\n opts: OrgDirectoryReadOptions = {},\n): Promise<DirectoryOrganizationView | null> {\n const agency = (await db\n .collection(AGENCIES_COLLECTION)\n .findOne(\n { _id: { $in: organizationIdForms(agencyId, opts) } },\n { projection: { _id: 1, organizationId: 1 } },\n )) as { _id?: unknown; organizationId?: unknown } | null;\n if (!agency || agency.organizationId == null) return null;\n const pointer = agency.organizationId as string | ObjectIdLike;\n const org = (await db\n .collection(ORGANIZATIONS_COLLECTION)\n .findOne({ _id: { $in: organizationIdForms(pointer, opts) } })) as {\n _id?: unknown;\n agencyId?: unknown;\n } | null;\n return org && isAgencyOwnOrganization(org, agency) ? toDirectoryOrganizationView(org) : null;\n}\n"],"mappings":";AAcO,IAAM,wBAAwB;AAAA,EACnC,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AACZ;AAMO,IAAM,+BAA+B,OAAO,OAAO;AAAA,EACxD,sBAAsB;AAAA,EACtB,sBAAsB;AAAA,EACtB,sBAAsB;AACxB,CAAU;AAEH,SAAS,sBAAsB,OAA8C;AAClF,SACE,OAAO,UAAU,YAAa,6BAAmD,SAAS,KAAK;AAEnG;AASO,IAAM,iBAAiB;AAAA,EAC5B,QAAQ;AAAA,EACR,SAAS;AACX;AAIO,IAAM,wBAAwB,OAAO,OAAO;AAAA,EACjD,eAAe;AA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,EACnC,QAAQ,EAAE,KAAK,EAAE,KAAK,oBAAoB,SAAS,IAAI,EAAE,EAAE,CAAC;AAI/D,SAAO,OAAO,wBAAwB,KAAK,MAAM,IAAI,4BAA4B,GAAG,IAAI;AAC1F;","names":[]}
@@ -470,6 +470,8 @@ var PointOfContactShapeError = class extends Error {
470
470
  this.issues = issues;
471
471
  this.name = "PointOfContactShapeError";
472
472
  }
473
+ contactId;
474
+ issues;
473
475
  };
474
476
  function normalizePointOfContact(raw) {
475
477
  const parsed = PointOfContactDocSchema.safeParse(raw);
@@ -856,9 +858,10 @@ function organizationRowShape(raw) {
856
858
  if (!raw || typeof raw !== "object") return "unknown";
857
859
  const r = raw;
858
860
  if (typeof r.canonicalKey === "string" && r.canonicalKey !== "") return "canonical";
859
- if (typeof r.id === "string" || typeof r.type === "string" || "acronym" in r || "location" in r)
860
- return "pave";
861
- if (typeof r.type === "number") return "thread";
861
+ if (typeof r.id === "string" || "acronym" in r || "location" in r) return "pave";
862
+ if (typeof r.type === "number" || typeof r.type === "string" && r.type.trim() !== "") {
863
+ return "thread";
864
+ }
862
865
  return "unknown";
863
866
  }
864
867
  function isAgencyOwnOrganization(org, agency) {