@htilabs/platform-common-models 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cvcs/index.cjs +449 -50
- package/dist/cvcs/index.cjs.map +1 -1
- package/dist/cvcs/index.d.cts +491 -55
- package/dist/cvcs/index.d.ts +491 -55
- package/dist/cvcs/index.js +449 -50
- package/dist/cvcs/index.js.map +1 -1
- package/dist/enums/index.cjs +36 -1
- package/dist/enums/index.cjs.map +1 -1
- package/dist/enums/index.d.cts +37 -2
- package/dist/enums/index.d.ts +37 -2
- package/dist/enums/index.js +36 -1
- package/dist/enums/index.js.map +1 -1
- package/dist/index.cjs +589 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +580 -51
- package/dist/index.js.map +1 -1
- package/dist/schemas/index.cjs +104 -0
- package/dist/schemas/index.cjs.map +1 -1
- package/dist/schemas/index.d.cts +240 -1
- package/dist/schemas/index.d.ts +240 -1
- package/dist/schemas/index.js +95 -0
- package/dist/schemas/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__sot__/README.md +1 -0
- package/src/__sot__/coreFunctionFields.ts +121 -0
- package/src/__sot__/coreFunctions.pin.json +1546 -0
- package/src/__sot__/coreFunctions.published.json +233 -0
- package/src/__tests__/__snapshots__/cvc-sot.snap +2 -2
- package/src/__tests__/core-functions-canon.test.ts +308 -24
- package/src/__tests__/cvc-sot-snapshot.test.ts +7 -4
- package/src/__tests__/cvc-status-parser.test.ts +1 -1
- package/src/__tests__/cvc-structural.test.ts +1 -1
- package/src/__tests__/organization-program.test.ts +254 -0
- package/src/cvcs/{coreFunctions.v1.1.0.json → coreFunctions.v1.2.0.json} +446 -47
- package/src/cvcs/index.ts +1 -1
- package/src/generated/cvc-enums.ts +37 -2
- package/src/schemas/index.ts +18 -0
- package/src/schemas/organization-program.ts +297 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Organization program fragment (`schemas/organization-program.ts`) — the ONE
|
|
3
|
+
* definition of `organizations.programs[]` both apps read (PAVE 2.0 writes it).
|
|
4
|
+
*
|
|
5
|
+
* Pins: the review-status vocabulary is the UDD Program object's three words;
|
|
6
|
+
* the mounted field is absent until written (no implicit `[]`); a row with a
|
|
7
|
+
* review status outside the vocabulary fails validation; the live-row rule
|
|
8
|
+
* excludes retired and rejected programs.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import mongoose, { Schema } from "mongoose";
|
|
12
|
+
import { describe, it, expect, vi } from "vitest";
|
|
13
|
+
|
|
14
|
+
import { applyRunValidatorsIfUnset } from "../schemas/agency.js";
|
|
15
|
+
import {
|
|
16
|
+
type OrganizationProgram,
|
|
17
|
+
ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS,
|
|
18
|
+
PROGRAM_REVIEW_STATUS,
|
|
19
|
+
PROGRAM_REVIEW_STATUS_VALUES,
|
|
20
|
+
addOrganizationProgramsField,
|
|
21
|
+
buildOrganizationProgramSchema,
|
|
22
|
+
isLiveOrganizationProgram,
|
|
23
|
+
isProgramReviewStatus,
|
|
24
|
+
} from "../schemas/organization-program.js";
|
|
25
|
+
|
|
26
|
+
function modelWithPrograms(name: string, select?: boolean) {
|
|
27
|
+
const schema = new Schema({ id: String, name: String });
|
|
28
|
+
addOrganizationProgramsField(schema, select === undefined ? {} : { select });
|
|
29
|
+
return mongoose.model(name, schema);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("review status vocabulary", () => {
|
|
33
|
+
it("is the UDD Program object's three words", () => {
|
|
34
|
+
expect([...PROGRAM_REVIEW_STATUS_VALUES]).toEqual([
|
|
35
|
+
"pending",
|
|
36
|
+
"confirmed",
|
|
37
|
+
"rejected",
|
|
38
|
+
]);
|
|
39
|
+
expect(PROGRAM_REVIEW_STATUS.PENDING).toBe("pending");
|
|
40
|
+
expect(isProgramReviewStatus("confirmed")).toBe(true);
|
|
41
|
+
// The lane's words never reach the stored row.
|
|
42
|
+
expect(isProgramReviewStatus("pending_steward")).toBe(false);
|
|
43
|
+
expect(isProgramReviewStatus("declined")).toBe(false);
|
|
44
|
+
expect(isProgramReviewStatus(null)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("the mounted programs[] field", () => {
|
|
49
|
+
it("is absent until written — no implicit empty array on a fresh document", () => {
|
|
50
|
+
const Org = modelWithPrograms("OrgProgFragmentAbsent");
|
|
51
|
+
const doc = new Org({ id: "org-1", name: "Willow Bend" });
|
|
52
|
+
expect(doc.get("programs")).toBeUndefined();
|
|
53
|
+
expect(doc.toObject()).not.toHaveProperty("programs");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("carries a program row with identity, label and the arrival facts, with no _id", () => {
|
|
57
|
+
const Org = modelWithPrograms("OrgProgFragmentRow");
|
|
58
|
+
const doc = new Org({
|
|
59
|
+
id: "org-1",
|
|
60
|
+
name: "Willow Bend",
|
|
61
|
+
programs: [
|
|
62
|
+
{
|
|
63
|
+
programId: "prog-1",
|
|
64
|
+
name: "Night Intake Unit",
|
|
65
|
+
status: "active",
|
|
66
|
+
reviewStatus: "pending",
|
|
67
|
+
addedBy: "user-1",
|
|
68
|
+
addedInApp: "pave",
|
|
69
|
+
addedInSession: "sess-1",
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
expect(doc.validateSync()).toBeUndefined();
|
|
74
|
+
const row = (
|
|
75
|
+
doc.toObject() as unknown as { programs: Record<string, unknown>[] }
|
|
76
|
+
).programs[0];
|
|
77
|
+
expect(row).toMatchObject({
|
|
78
|
+
programId: "prog-1",
|
|
79
|
+
name: "Night Intake Unit",
|
|
80
|
+
reviewStatus: "pending",
|
|
81
|
+
addedInApp: "pave",
|
|
82
|
+
});
|
|
83
|
+
expect(row).not.toHaveProperty("_id");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("refuses a review status outside the vocabulary and a row with no identity", () => {
|
|
87
|
+
const Org = modelWithPrograms("OrgProgFragmentInvalid");
|
|
88
|
+
const bad = new Org({
|
|
89
|
+
id: "org-1",
|
|
90
|
+
programs: [{ programId: "p", name: "X", reviewStatus: "declined" }],
|
|
91
|
+
});
|
|
92
|
+
expect(bad.validateSync()?.errors["programs.0.reviewStatus"]).toBeDefined();
|
|
93
|
+
const noId = new Org({ id: "org-1", programs: [{ name: "X" }] });
|
|
94
|
+
expect(noId.validateSync()?.errors["programs.0.programId"]).toBeDefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("the steward's own copy carries no review status (optional, not defaulted)", () => {
|
|
98
|
+
const Org = modelWithPrograms("OrgProgFragmentSteward");
|
|
99
|
+
const doc = new Org({
|
|
100
|
+
id: "org-1",
|
|
101
|
+
programs: [{ programId: "prog-s", name: "Steward Row" }],
|
|
102
|
+
});
|
|
103
|
+
expect(doc.validateSync()).toBeUndefined();
|
|
104
|
+
const row = (
|
|
105
|
+
doc.toObject() as unknown as { programs: Record<string, unknown>[] }
|
|
106
|
+
).programs[0];
|
|
107
|
+
expect(row).not.toHaveProperty("reviewStatus");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("honours a consumer's `select: false` (Thread's read posture)", () => {
|
|
111
|
+
const Org = modelWithPrograms("OrgProgFragmentSelect", false);
|
|
112
|
+
expect(Org.schema.path("programs").options.select).toBe(false);
|
|
113
|
+
const sub = buildOrganizationProgramSchema();
|
|
114
|
+
expect(sub.path("programId")).toBeDefined();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("the OrganizationProgram type", () => {
|
|
119
|
+
it("represents a steward row (id + name only) and a named row; carries no _id", () => {
|
|
120
|
+
// Type-level: these assignments compile only if the inferred type has
|
|
121
|
+
// programId/name required, everything else optional, and no `_id`.
|
|
122
|
+
const steward: OrganizationProgram = { programId: "p-s", name: "Steward Row" };
|
|
123
|
+
const named: OrganizationProgram = {
|
|
124
|
+
programId: "p-n",
|
|
125
|
+
name: "Night Intake Unit",
|
|
126
|
+
reviewStatus: "pending",
|
|
127
|
+
addedBy: "user-1",
|
|
128
|
+
addedInApp: "pave",
|
|
129
|
+
addedInSession: null,
|
|
130
|
+
};
|
|
131
|
+
// @ts-expect-error — a word outside the vocabulary is not a review status
|
|
132
|
+
const bad: OrganizationProgram = { programId: "p", name: "X", reviewStatus: "approved" };
|
|
133
|
+
expect(steward.programId).toBe("p-s");
|
|
134
|
+
expect(named.reviewStatus).toBe("pending");
|
|
135
|
+
expect(bad.programId).toBe("p");
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("addOrganizationProgramsField on a schema that already declares programs", () => {
|
|
140
|
+
it("replaces the path with the shared fragment and warns", () => {
|
|
141
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
142
|
+
const schema = new Schema({
|
|
143
|
+
id: String,
|
|
144
|
+
programs: { type: [new Schema({ programId: String, legacy: String })], select: false },
|
|
145
|
+
});
|
|
146
|
+
addOrganizationProgramsField(schema, { select: false });
|
|
147
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
148
|
+
expect(schema.path("programs").options.select).toBe(false);
|
|
149
|
+
const Org = mongoose.model("OrgProgFragmentReplaced", schema);
|
|
150
|
+
const doc = new Org({ id: "o", programs: [{ programId: "p", name: "N", legacy: "x" }] });
|
|
151
|
+
expect(doc.validateSync()).toBeUndefined();
|
|
152
|
+
const row = (doc.toObject() as unknown as { programs: Record<string, unknown>[] }).programs[0];
|
|
153
|
+
expect(row).not.toHaveProperty("legacy");
|
|
154
|
+
warn.mockRestore();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("drops the consumer's own select:false when options are omitted — the hazard the warning names", () => {
|
|
158
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
159
|
+
const schema = new Schema({
|
|
160
|
+
id: String,
|
|
161
|
+
programs: { type: [new Schema({ programId: String })], select: false },
|
|
162
|
+
});
|
|
163
|
+
addOrganizationProgramsField(schema);
|
|
164
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
165
|
+
expect(schema.path("programs").options.select).toBeUndefined();
|
|
166
|
+
warn.mockRestore();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("honours select:true as well as select:false", () => {
|
|
170
|
+
const schema = addOrganizationProgramsField(new Schema({ id: String }), {
|
|
171
|
+
select: true,
|
|
172
|
+
});
|
|
173
|
+
expect(schema.path("programs").options.select).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("each mount gets its own sub-schema instance", () => {
|
|
178
|
+
it("a hook, virtual or index one consumer adds to its rows does not reach another consumer", () => {
|
|
179
|
+
const a = addOrganizationProgramsField(new Schema({ id: String }));
|
|
180
|
+
const b = addOrganizationProgramsField(new Schema({ id: String }));
|
|
181
|
+
const subA = a.path("programs").schema as Schema;
|
|
182
|
+
const subB = b.path("programs").schema as Schema;
|
|
183
|
+
expect(subA).not.toBe(subB);
|
|
184
|
+
subA.index({ name: 1 });
|
|
185
|
+
subA.virtual("shout").get(function (this: { name: string }) {
|
|
186
|
+
return this.name.toUpperCase();
|
|
187
|
+
});
|
|
188
|
+
expect(subB.indexes()).toHaveLength(0);
|
|
189
|
+
expect(subB.virtualpath("shout")).toBeFalsy();
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("validators on the update paths", () => {
|
|
194
|
+
it("installs runValidators on updateOne / updateMany / findOneAndUpdate by default; opt-out installs nothing", () => {
|
|
195
|
+
type Hooked = { s: { hooks: { hasHooks: (n: string) => boolean } } };
|
|
196
|
+
const hooksOf = (schema: Schema) => (schema as unknown as Hooked).s.hooks;
|
|
197
|
+
const on = hooksOf(addOrganizationProgramsField(new Schema({ id: String })));
|
|
198
|
+
for (const op of ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS)
|
|
199
|
+
expect(on.hasHooks(op), op).toBe(true);
|
|
200
|
+
expect(on.hasHooks("save")).toBe(false);
|
|
201
|
+
const off = hooksOf(
|
|
202
|
+
addOrganizationProgramsField(new Schema({ id: String }), {
|
|
203
|
+
validateOnUpdate: false,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
for (const op of ORGANIZATION_PROGRAM_VALIDATED_UPDATE_OPS)
|
|
207
|
+
expect(off.hasHooks(op), op).toBe(false);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("the hook body sets runValidators unless the caller already decided (shared with agency.ts)", () => {
|
|
211
|
+
const q = (initial: Record<string, unknown>) => {
|
|
212
|
+
let opts = { ...initial };
|
|
213
|
+
const self = {
|
|
214
|
+
getOptions: () => opts,
|
|
215
|
+
setOptions: (o: Record<string, unknown>) => {
|
|
216
|
+
opts = { ...opts, ...o };
|
|
217
|
+
return self;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
return self;
|
|
221
|
+
};
|
|
222
|
+
const a = q({});
|
|
223
|
+
applyRunValidatorsIfUnset(a as never);
|
|
224
|
+
expect(a.getOptions().runValidators).toBe(true);
|
|
225
|
+
const b = q({ runValidators: false });
|
|
226
|
+
applyRunValidatorsIfUnset(b as never);
|
|
227
|
+
expect(b.getOptions().runValidators).toBe(false);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("with runValidators on, a $push row outside the vocabulary or without an id is refused by the array validator", () => {
|
|
231
|
+
// What the hook buys on a real update: Mongoose's update validator casts
|
|
232
|
+
// the $push payload against the sub-schema when runValidators is set.
|
|
233
|
+
// Exercised without a connection through the sub-schema's own validation,
|
|
234
|
+
// which is what the update validator runs for an array-of-subdoc path.
|
|
235
|
+
const Org = mongoose.model(
|
|
236
|
+
"OrgProgFragmentUpdateValidate",
|
|
237
|
+
addOrganizationProgramsField(new Schema({ id: String })),
|
|
238
|
+
);
|
|
239
|
+
const bad = new Org({ id: "o", programs: [{ programId: "p", name: "X", reviewStatus: "declined" }] });
|
|
240
|
+
expect(bad.validateSync()?.errors["programs.0.reviewStatus"]).toBeDefined();
|
|
241
|
+
const noId = new Org({ id: "o", programs: [{ name: "X" }] });
|
|
242
|
+
expect(noId.validateSync()?.errors["programs.0.programId"]).toBeDefined();
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe("isLiveOrganizationProgram", () => {
|
|
247
|
+
it("offers active / confirmed / pending rows, never retired or rejected ones", () => {
|
|
248
|
+
expect(isLiveOrganizationProgram({ status: "active" })).toBe(true);
|
|
249
|
+
expect(isLiveOrganizationProgram({ reviewStatus: "pending" })).toBe(true);
|
|
250
|
+
expect(isLiveOrganizationProgram({})).toBe(true);
|
|
251
|
+
expect(isLiveOrganizationProgram({ status: "retired" })).toBe(false);
|
|
252
|
+
expect(isLiveOrganizationProgram({ reviewStatus: "rejected" })).toBe(false);
|
|
253
|
+
});
|
|
254
|
+
});
|