@htilabs/platform-common-models 0.64.0 → 0.66.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.
@@ -439,6 +439,26 @@ type TeamDirectoryFields = Partial<InferRawDocType<typeof teamDirectorySchemaDef
439
439
  * Canon and stored names: see `orgDirectory/orgRecord.ts`.
440
440
  */
441
441
 
442
+ /** The name of the unique index on `key` (the same in every app, so they never conflict). */
443
+ declare const ORG_KEY_INDEX_NAME = "org_key_unique";
444
+ /**
445
+ * The unique index on `key`, as one definition: the mount below declares it,
446
+ * and the registry fill's key sheet builds the same index before it writes.
447
+ * Partial: a row with no key, or a null one, is not indexed, so it never
448
+ * collides. Copy these before handing them to a driver (they are frozen).
449
+ */
450
+ declare const ORG_KEY_INDEX_SPEC: Readonly<{
451
+ key: 1;
452
+ }>;
453
+ declare const ORG_KEY_INDEX_OPTIONS: Readonly<{
454
+ name: "org_key_unique";
455
+ unique: true;
456
+ partialFilterExpression: Readonly<{
457
+ key: Readonly<{
458
+ $type: "string";
459
+ }>;
460
+ }>;
461
+ }>;
442
462
  /** An id in auth's stored form: an ObjectId, or a non-empty string (a dev seed's slug). */
443
463
  declare function isStoredDirectoryId(value: unknown): boolean;
444
464
  /**
@@ -447,6 +467,37 @@ declare function isStoredDirectoryId(value: unknown): boolean;
447
467
  * so "not captured" stays distinct from an answer.
448
468
  */
449
469
  declare const orgRecordSchemaDefinition: {
470
+ /**
471
+ * The Org's identifier (the UDD `id`): the same value in every database.
472
+ * Never taken from a request body.
473
+ *
474
+ * `immutable` means the model stores a key only while it is creating the
475
+ * row. Measured on Mongoose 9.3.1 against MongoDB (the fill's test file
476
+ * holds each line):
477
+ *
478
+ * - `new Model({ key }).save()` and `Model.create({ key })` store it.
479
+ * - An upsert stores it when the upsert inserts the row (`$setOnInsert`, or
480
+ * `$set`, which Mongoose moves to `$setOnInsert`); on a row that already
481
+ * exists the upsert leaves `key` as it was.
482
+ * - On a row that already exists, `doc.key = …; doc.save()` and
483
+ * `updateOne` / `findOneAndUpdate` with `$set: { key }` do NOT write it,
484
+ * and do not report an error. That holds whether the row has a key or has
485
+ * none yet, so a model cannot give an old row its first key either.
486
+ *
487
+ * So an app stores `key: mintOrgKey(_id)` where it creates the
488
+ * organization (`new Model().save()`, `create()`, or `$setOnInsert` on an
489
+ * upsert). Rows that already exist get theirs from the registry fill's key
490
+ * sheet, which writes with the plain MongoDB driver.
491
+ */
492
+ key: {
493
+ type: StringConstructor;
494
+ trim: boolean;
495
+ immutable: boolean;
496
+ validate: {
497
+ validator: (value: unknown) => boolean;
498
+ message: string;
499
+ };
500
+ };
450
501
  /** The canonical reporting name (HMIS, funders). Readers fall back to `name`. */
451
502
  legalName: {
452
503
  type: StringConstructor;
@@ -782,4 +833,4 @@ type OrganizationProgram = InferRawDocType<typeof organizationProgramSchemaDefin
782
833
  typeKey: "type";
783
834
  }>;
784
835
 
785
- export { AddAgencyOrgTypeFieldsOptions, type AddOrganizationProgramsFieldOptions, type Address, type Demographics, type Email, ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS, type OrganizationProgram, type PersonName, type Phone, TEAM_MODEL_NAME, type TeamDirectoryFields, addOrgRecordFields, addOrganizationProgramsField, addTeamDirectoryFields, addressSchema, buildOrganizationProgramSchema, buildOrganizationProgramsFieldDefinition, demographicsSchema, emailSchema, orgAsBuiltSchemaDefinition, orgRecordSchemaDefinition, organizationProgramSchemaDefinition, organizationSchemaIssues, personNameSchema, phoneSchema, teamDirectorySchemaDefinition };
836
+ export { AddAgencyOrgTypeFieldsOptions, type AddOrganizationProgramsFieldOptions, type Address, type Demographics, type Email, ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS, ORG_KEY_INDEX_NAME, ORG_KEY_INDEX_OPTIONS, ORG_KEY_INDEX_SPEC, type OrganizationProgram, type PersonName, type Phone, TEAM_MODEL_NAME, type TeamDirectoryFields, addOrgRecordFields, addOrganizationProgramsField, addTeamDirectoryFields, addressSchema, buildOrganizationProgramSchema, buildOrganizationProgramsFieldDefinition, demographicsSchema, emailSchema, orgAsBuiltSchemaDefinition, orgRecordSchemaDefinition, organizationProgramSchemaDefinition, organizationSchemaIssues, personNameSchema, phoneSchema, teamDirectorySchemaDefinition };
@@ -439,6 +439,26 @@ type TeamDirectoryFields = Partial<InferRawDocType<typeof teamDirectorySchemaDef
439
439
  * Canon and stored names: see `orgDirectory/orgRecord.ts`.
440
440
  */
441
441
 
442
+ /** The name of the unique index on `key` (the same in every app, so they never conflict). */
443
+ declare const ORG_KEY_INDEX_NAME = "org_key_unique";
444
+ /**
445
+ * The unique index on `key`, as one definition: the mount below declares it,
446
+ * and the registry fill's key sheet builds the same index before it writes.
447
+ * Partial: a row with no key, or a null one, is not indexed, so it never
448
+ * collides. Copy these before handing them to a driver (they are frozen).
449
+ */
450
+ declare const ORG_KEY_INDEX_SPEC: Readonly<{
451
+ key: 1;
452
+ }>;
453
+ declare const ORG_KEY_INDEX_OPTIONS: Readonly<{
454
+ name: "org_key_unique";
455
+ unique: true;
456
+ partialFilterExpression: Readonly<{
457
+ key: Readonly<{
458
+ $type: "string";
459
+ }>;
460
+ }>;
461
+ }>;
442
462
  /** An id in auth's stored form: an ObjectId, or a non-empty string (a dev seed's slug). */
443
463
  declare function isStoredDirectoryId(value: unknown): boolean;
444
464
  /**
@@ -447,6 +467,37 @@ declare function isStoredDirectoryId(value: unknown): boolean;
447
467
  * so "not captured" stays distinct from an answer.
448
468
  */
449
469
  declare const orgRecordSchemaDefinition: {
470
+ /**
471
+ * The Org's identifier (the UDD `id`): the same value in every database.
472
+ * Never taken from a request body.
473
+ *
474
+ * `immutable` means the model stores a key only while it is creating the
475
+ * row. Measured on Mongoose 9.3.1 against MongoDB (the fill's test file
476
+ * holds each line):
477
+ *
478
+ * - `new Model({ key }).save()` and `Model.create({ key })` store it.
479
+ * - An upsert stores it when the upsert inserts the row (`$setOnInsert`, or
480
+ * `$set`, which Mongoose moves to `$setOnInsert`); on a row that already
481
+ * exists the upsert leaves `key` as it was.
482
+ * - On a row that already exists, `doc.key = …; doc.save()` and
483
+ * `updateOne` / `findOneAndUpdate` with `$set: { key }` do NOT write it,
484
+ * and do not report an error. That holds whether the row has a key or has
485
+ * none yet, so a model cannot give an old row its first key either.
486
+ *
487
+ * So an app stores `key: mintOrgKey(_id)` where it creates the
488
+ * organization (`new Model().save()`, `create()`, or `$setOnInsert` on an
489
+ * upsert). Rows that already exist get theirs from the registry fill's key
490
+ * sheet, which writes with the plain MongoDB driver.
491
+ */
492
+ key: {
493
+ type: StringConstructor;
494
+ trim: boolean;
495
+ immutable: boolean;
496
+ validate: {
497
+ validator: (value: unknown) => boolean;
498
+ message: string;
499
+ };
500
+ };
450
501
  /** The canonical reporting name (HMIS, funders). Readers fall back to `name`. */
451
502
  legalName: {
452
503
  type: StringConstructor;
@@ -782,4 +833,4 @@ type OrganizationProgram = InferRawDocType<typeof organizationProgramSchemaDefin
782
833
  typeKey: "type";
783
834
  }>;
784
835
 
785
- export { AddAgencyOrgTypeFieldsOptions, type AddOrganizationProgramsFieldOptions, type Address, type Demographics, type Email, ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS, type OrganizationProgram, type PersonName, type Phone, TEAM_MODEL_NAME, type TeamDirectoryFields, addOrgRecordFields, addOrganizationProgramsField, addTeamDirectoryFields, addressSchema, buildOrganizationProgramSchema, buildOrganizationProgramsFieldDefinition, demographicsSchema, emailSchema, orgAsBuiltSchemaDefinition, orgRecordSchemaDefinition, organizationProgramSchemaDefinition, organizationSchemaIssues, personNameSchema, phoneSchema, teamDirectorySchemaDefinition };
836
+ export { AddAgencyOrgTypeFieldsOptions, type AddOrganizationProgramsFieldOptions, type Address, type Demographics, type Email, ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS, ORG_KEY_INDEX_NAME, ORG_KEY_INDEX_OPTIONS, ORG_KEY_INDEX_SPEC, type OrganizationProgram, type PersonName, type Phone, TEAM_MODEL_NAME, type TeamDirectoryFields, addOrgRecordFields, addOrganizationProgramsField, addTeamDirectoryFields, addressSchema, buildOrganizationProgramSchema, buildOrganizationProgramsFieldDefinition, demographicsSchema, emailSchema, orgAsBuiltSchemaDefinition, orgRecordSchemaDefinition, organizationProgramSchemaDefinition, organizationSchemaIssues, personNameSchema, phoneSchema, teamDirectorySchemaDefinition };
@@ -5847,6 +5847,7 @@ function isLiveOrganizationProgram(row) {
5847
5847
  // src/orgDirectory/orgRecord.ts
5848
5848
  var ORG_REVIEW_STATUS_VALUES = PROGRAM_REVIEW_STATUS_VALUES;
5849
5849
  var ORG_RECORD_FIELD_NAMES = Object.freeze([
5850
+ "key",
5850
5851
  "legalName",
5851
5852
  "commonName",
5852
5853
  "shortName",
@@ -5896,6 +5897,10 @@ var ORGANIZATION_ENVELOPE_KEYS = Object.freeze([
5896
5897
  "updatedUser",
5897
5898
  "updatedTime"
5898
5899
  ]);
5900
+ var ORG_KEY_PATTERN = /^[a-z0-9][a-z0-9_-]{0,119}$/;
5901
+ function isOrgKey(value) {
5902
+ return typeof value === "string" && ORG_KEY_PATTERN.test(value);
5903
+ }
5899
5904
  var line = (max) => z.string().trim().min(1).max(max).nullable();
5900
5905
  var ORG_VSP_TWO_STEP_MESSAGE = "vspPrivateNonprofit is step 2 of the HMIS VSP two-step and requires vsp: true";
5901
5906
  var OrgIdentityWriteSchema = z.object({
@@ -5916,12 +5921,50 @@ var OrgVspWriteSchema = z.object({
5916
5921
 
5917
5922
  // src/schemas/organization.ts
5918
5923
  var HEX_24 = /^[0-9a-fA-F]{24}$/;
5924
+ var ORG_KEY_INDEX_NAME = "org_key_unique";
5925
+ var ORG_KEY_INDEX_SPEC = Object.freeze({ key: 1 });
5926
+ var ORG_KEY_INDEX_OPTIONS = Object.freeze({
5927
+ name: ORG_KEY_INDEX_NAME,
5928
+ unique: true,
5929
+ partialFilterExpression: Object.freeze({ key: Object.freeze({ $type: "string" }) })
5930
+ });
5919
5931
  function isStoredDirectoryId(value) {
5920
5932
  if (value == null) return true;
5921
5933
  if (typeof value === "string") return value.trim() !== "";
5922
5934
  return typeof value === "object" && typeof value.toHexString === "function" && HEX_24.test(value.toHexString());
5923
5935
  }
5924
5936
  var orgRecordSchemaDefinition = {
5937
+ /**
5938
+ * The Org's identifier (the UDD `id`): the same value in every database.
5939
+ * Never taken from a request body.
5940
+ *
5941
+ * `immutable` means the model stores a key only while it is creating the
5942
+ * row. Measured on Mongoose 9.3.1 against MongoDB (the fill's test file
5943
+ * holds each line):
5944
+ *
5945
+ * - `new Model({ key }).save()` and `Model.create({ key })` store it.
5946
+ * - An upsert stores it when the upsert inserts the row (`$setOnInsert`, or
5947
+ * `$set`, which Mongoose moves to `$setOnInsert`); on a row that already
5948
+ * exists the upsert leaves `key` as it was.
5949
+ * - On a row that already exists, `doc.key = …; doc.save()` and
5950
+ * `updateOne` / `findOneAndUpdate` with `$set: { key }` do NOT write it,
5951
+ * and do not report an error. That holds whether the row has a key or has
5952
+ * none yet, so a model cannot give an old row its first key either.
5953
+ *
5954
+ * So an app stores `key: mintOrgKey(_id)` where it creates the
5955
+ * organization (`new Model().save()`, `create()`, or `$setOnInsert` on an
5956
+ * upsert). Rows that already exist get theirs from the registry fill's key
5957
+ * sheet, which writes with the plain MongoDB driver.
5958
+ */
5959
+ key: {
5960
+ type: String,
5961
+ trim: true,
5962
+ immutable: true,
5963
+ validate: {
5964
+ validator: (value) => value == null || isOrgKey(value),
5965
+ message: "key must be a lower-case slug (a-z, 0-9, _ and -)"
5966
+ }
5967
+ },
5925
5968
  /** The canonical reporting name (HMIS, funders). Readers fall back to `name`. */
5926
5969
  legalName: { type: String, trim: true },
5927
5970
  /** The name people use, when it differs from `name`. */
@@ -5970,6 +6013,13 @@ var orgAsBuiltSchemaDefinition = {
5970
6013
  };
5971
6014
  function addOrgRecordFields(schema, options = {}) {
5972
6015
  schema.add(orgRecordSchemaDefinition);
6016
+ schema.index(
6017
+ { ...ORG_KEY_INDEX_SPEC },
6018
+ {
6019
+ ...ORG_KEY_INDEX_OPTIONS,
6020
+ partialFilterExpression: { key: { ...ORG_KEY_INDEX_OPTIONS.partialFilterExpression.key } }
6021
+ }
6022
+ );
5973
6023
  for (const key of ORG_AS_BUILT_FIELD_NAMES) {
5974
6024
  if (schema.pathType(key) === "adhocOrUndefined") {
5975
6025
  schema.add({ [key]: orgAsBuiltSchemaDefinition[key] });
@@ -5978,6 +6028,7 @@ function addOrgRecordFields(schema, options = {}) {
5978
6028
  return installVspTwoStepHooks(schema, options);
5979
6029
  }
5980
6030
  var EXPECTED_INSTANCE = {
6031
+ key: "String",
5981
6032
  legalName: "String",
5982
6033
  commonName: "String",
5983
6034
  shortName: "String",
@@ -6099,6 +6150,9 @@ function addOrganizationProgramsField(schema, options = {}) {
6099
6150
  export {
6100
6151
  AGENCY_ORG_TYPE_VALIDATED_UPDATE_OPS,
6101
6152
  ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS,
6153
+ ORG_KEY_INDEX_NAME,
6154
+ ORG_KEY_INDEX_OPTIONS,
6155
+ ORG_KEY_INDEX_SPEC,
6102
6156
  ORG_TYPE_FIELD_NAMES,
6103
6157
  PROGRAM_REVIEW_STATUS,
6104
6158
  PROGRAM_REVIEW_STATUS_VALUES,