@htilabs/platform-common-models 0.29.0 → 0.30.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/README.md +268 -1
- package/dist/{index-Dr6GBCoY.d.ts → index-ClTjjJ_S.d.ts} +1 -1
- package/dist/{index-BTqHP4DM.d.cts → index-Pt82NxQg.d.cts} +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/models/index.d.cts +2 -2
- package/dist/models/index.d.ts +2 -2
- package/dist/points-of-contact/index.cjs +826 -21
- package/dist/points-of-contact/index.cjs.map +1 -1
- package/dist/points-of-contact/index.d.cts +936 -87
- package/dist/points-of-contact/index.d.ts +936 -87
- package/dist/points-of-contact/index.js +799 -21
- package/dist/points-of-contact/index.js.map +1 -1
- package/dist/schemas/index.d.cts +25 -25
- package/dist/schemas/index.d.ts +25 -25
- package/package.json +1 -1
- package/src/pointsOfContact/__tests__/mongoDouble.ts +194 -0
- package/src/pointsOfContact/__tests__/pointsOfContact.test.ts +379 -26
- package/src/pointsOfContact/__tests__/writer.mongo.test.ts +190 -0
- package/src/pointsOfContact/__tests__/writer.test.ts +782 -0
- package/src/pointsOfContact/index.ts +12 -7
- package/src/pointsOfContact/readRules.ts +73 -12
- package/src/pointsOfContact/reader.ts +75 -9
- package/src/pointsOfContact/schemas.ts +304 -34
- package/src/pointsOfContact/vocabulary.ts +40 -5
- package/src/pointsOfContact/writer.ts +967 -0
- package/dist/{cvcEntry-BqAGctqK.d.cts → cvcEntry-Asg7mxjB.d.cts} +8 -8
- package/dist/{cvcEntry-BqAGctqK.d.ts → cvcEntry-Asg7mxjB.d.ts} +8 -8
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared in-app partner add against a REAL MongoDB: the server's own
|
|
3
|
+
* partial unique indexes and their E11000 errors, a repeated index creation
|
|
4
|
+
* from every process, and ObjectId-keyed `agencies` / `organizations` rows. The package's default suite stays free
|
|
5
|
+
* of a real MongoDB: this runs only when `POC_WRITER_MONGO_URI` names a
|
|
6
|
+
* server, e.g.
|
|
7
|
+
*
|
|
8
|
+
* mongod --dbpath "$(mktemp -d)" --port 27999 --fork --logpath /tmp/mongod.log
|
|
9
|
+
* POC_WRITER_MONGO_URI=mongodb://127.0.0.1:27999 npx vitest run src/pointsOfContact
|
|
10
|
+
*
|
|
11
|
+
* Each run uses a fresh throwaway database and drops it.
|
|
12
|
+
*/
|
|
13
|
+
import mongoose from "mongoose";
|
|
14
|
+
import type { Db, MongoClient as MongoClientType } from "mongodb";
|
|
15
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
POC_IN_APP_PENDING_CAP_PER_PARTNER,
|
|
19
|
+
POC_IN_APP_PENDING_INDEX,
|
|
20
|
+
POINTS_OF_CONTACT_COLLECTION,
|
|
21
|
+
POINT_OF_CONTACT_INDEXES,
|
|
22
|
+
ensurePocIndexes,
|
|
23
|
+
mintPartnerContact,
|
|
24
|
+
type PartnerContactResult,
|
|
25
|
+
} from "../index.js";
|
|
26
|
+
|
|
27
|
+
const URI = process.env.POC_WRITER_MONGO_URI;
|
|
28
|
+
const { MongoClient, ObjectId } = mongoose.mongo;
|
|
29
|
+
const DRIVER = { ObjectId };
|
|
30
|
+
|
|
31
|
+
const AGENCY_A = new ObjectId();
|
|
32
|
+
const ORG_A = new ObjectId();
|
|
33
|
+
const ORG_C = new ObjectId();
|
|
34
|
+
const ADVOCATE = { id: "auth0|advocate" };
|
|
35
|
+
const NOW = new Date("2026-10-14T15:00:00Z");
|
|
36
|
+
|
|
37
|
+
describe.skipIf(!URI)("mintPartnerContact against a real MongoDB", () => {
|
|
38
|
+
let client: MongoClientType;
|
|
39
|
+
let dbName: string;
|
|
40
|
+
let db: Db;
|
|
41
|
+
const fresh = () => client.db(dbName); // a new handle object each call
|
|
42
|
+
const add = (partner: Record<string, string>, contact: Record<string, unknown>, h: Db = db) =>
|
|
43
|
+
mintPartnerContact(h, { app: "pave", partner, contact: contact as never, actor: ADVOCATE, now: NOW }, DRIVER);
|
|
44
|
+
const idOf = (r: PartnerContactResult) => (r.ok ? r.value.id : "(refused)");
|
|
45
|
+
const rows = () => db.collection(POINTS_OF_CONTACT_COLLECTION).find({}).toArray();
|
|
46
|
+
|
|
47
|
+
beforeAll(async () => {
|
|
48
|
+
client = await new MongoClient(URI!).connect();
|
|
49
|
+
});
|
|
50
|
+
afterAll(async () => {
|
|
51
|
+
await client?.close();
|
|
52
|
+
});
|
|
53
|
+
beforeEach(async () => {
|
|
54
|
+
dbName = `poc_writer_test_${new ObjectId().toHexString()}`;
|
|
55
|
+
db = fresh();
|
|
56
|
+
await db.collection("agencies").insertOne({ _id: AGENCY_A, entity_id: "agency-a", organizationId: ORG_A });
|
|
57
|
+
await db.collection("organizations").insertMany([
|
|
58
|
+
{ _id: ORG_A, name: "A", agencyId: AGENCY_A },
|
|
59
|
+
{ _id: ORG_C, name: "C (no agency)" },
|
|
60
|
+
]);
|
|
61
|
+
await ensurePocIndexes(fresh()); // the platform's job, on its own handle
|
|
62
|
+
return async () => {
|
|
63
|
+
await db.dropDatabase();
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("creates the full index set; every later process (a new handle) finds it in place", async () => {
|
|
68
|
+
await ensurePocIndexes(db);
|
|
69
|
+
const coll = db.collection(POINTS_OF_CONTACT_COLLECTION);
|
|
70
|
+
const before = await coll.indexes();
|
|
71
|
+
const byName = Object.fromEntries(before.map((i) => [i.name, i]));
|
|
72
|
+
expect(byName[POC_IN_APP_PENDING_INDEX]).toMatchObject({
|
|
73
|
+
key: { agency_id: 1, subject_type: 1, subject_id: 1, identity_key: 1, createdBy: 1 },
|
|
74
|
+
unique: true,
|
|
75
|
+
partialFilterExpression: { status: "pending", origin: "in_app", active: true },
|
|
76
|
+
});
|
|
77
|
+
for (const name of Object.values(POINT_OF_CONTACT_INDEXES)) {
|
|
78
|
+
expect(byName[name], name).toBeDefined();
|
|
79
|
+
}
|
|
80
|
+
for (let i = 0; i < 2; i++) await ensurePocIndexes(fresh());
|
|
81
|
+
expect(await coll.indexes()).toEqual(before);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("resolves ObjectId-keyed agencies and orgs: an agency by entity_id or hex, an org with a genuine mirror onto its agency", async () => {
|
|
85
|
+
const hex = AGENCY_A.toHexString();
|
|
86
|
+
for (const [i, form] of ["agency-a", hex, hex.toUpperCase()].entries()) {
|
|
87
|
+
expect(await add({ agency_id: form }, { display_name: form, emails: [{ value: `form${i}@a.org` }] }), form).toMatchObject({
|
|
88
|
+
ok: true,
|
|
89
|
+
value: { subject_type: "agency", subject_id: hex, agency_id: hex, org_id: ORG_A.toHexString(), created: true },
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
expect(await add({ org_id: ORG_A.toHexString() }, { display_name: "agency-a", emails: [{ value: "form0@a.org" }] })).toMatchObject({
|
|
93
|
+
value: { subject_type: "agency", subject_id: hex, created: false },
|
|
94
|
+
});
|
|
95
|
+
expect(await add({ agency_id: "agency-a", org_id: ORG_C.toHexString() }, { display_name: "X", emails: [{ value: "x@x.org" }] })).toMatchObject({
|
|
96
|
+
reason: "owner_mismatch",
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("the server's pending index settles a race of one identity: one row, the loser answers with it; a shared mailbox is not one identity", async () => {
|
|
101
|
+
const org = { org_id: ORG_C.toHexString() };
|
|
102
|
+
const [a, b] = await Promise.all([
|
|
103
|
+
add(org, { display_name: "Desk", emails: [{ value: "desk@c.org" }, { value: "shared@c.org" }] }, fresh()),
|
|
104
|
+
add(org, { display_name: "desk", emails: [{ value: "DESK@c.org" }, { value: "other@c.org" }] }, fresh()),
|
|
105
|
+
]);
|
|
106
|
+
expect(await rows()).toHaveLength(1);
|
|
107
|
+
expect(idOf(a)).toBe(idOf(b));
|
|
108
|
+
expect([a, b].map((r) => (r.ok ? r.value.created : null)).sort()).toEqual([false, true]);
|
|
109
|
+
// Jane and Dan share the mailbox with Desk: each is their own row, pending side by side.
|
|
110
|
+
for (const name of ["Jane", "Dan"]) {
|
|
111
|
+
const r = await add(org, { display_name: name, emails: [{ value: `${name}@c.org` }, { value: "shared@c.org" }] });
|
|
112
|
+
expect(r, name).toMatchObject({ ok: true, value: { created: true } });
|
|
113
|
+
}
|
|
114
|
+
expect(await rows()).toHaveLength(3);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("the real driver's E11000 on the pending index maps to duplicate_pending_add and is answered with the winner's row", async () => {
|
|
118
|
+
const org = { org_id: ORG_C.toHexString() };
|
|
119
|
+
const first = await add(org, { display_name: "Desk", emails: [{ value: "desk@c.org" }, { value: "shared@c.org" }] });
|
|
120
|
+
// Hide the existing row from the first read only: the add reaches the insert, as the
|
|
121
|
+
// loser of a race does, and meets the server's partial unique index on identity_key.
|
|
122
|
+
let hidden = false;
|
|
123
|
+
const racing = {
|
|
124
|
+
collection: (name: string) => {
|
|
125
|
+
const c = db.collection(name);
|
|
126
|
+
if (name !== POINTS_OF_CONTACT_COLLECTION) return c;
|
|
127
|
+
return Object.assign(Object.create(c) as typeof c, {
|
|
128
|
+
find: (f: Record<string, unknown>, o?: Record<string, unknown>) =>
|
|
129
|
+
hidden ? c.find(f, o) : ((hidden = true), c.find({ id: "(none)" })),
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
} as unknown as Db;
|
|
133
|
+
const r = await add(org, { display_name: "DESK", emails: [{ value: "desk@c.org" }] }, racing);
|
|
134
|
+
expect(r).toEqual({ ok: true, value: { ...(first.ok && first.value), created: false } });
|
|
135
|
+
expect(await rows()).toHaveLength(1);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("a parallel burst on the server stays within the cap; every survivor answered ok, every 429 left no row", async () => {
|
|
139
|
+
const org = { org_id: ORG_C.toHexString() };
|
|
140
|
+
for (let round = 0; round < 3; round++) {
|
|
141
|
+
await db.collection(POINTS_OF_CONTACT_COLLECTION).deleteMany({});
|
|
142
|
+
const results = await Promise.all(
|
|
143
|
+
Array.from({ length: 12 }, (_, i) => add(org, { display_name: `Par${round}-${i}`, emails: [{ value: `par${round}-${i}@c.org` }] }, fresh())),
|
|
144
|
+
);
|
|
145
|
+
const kept = await rows();
|
|
146
|
+
expect(kept.length, `round ${round}`).toBeLessThanOrEqual(POC_IN_APP_PENDING_CAP_PER_PARTNER);
|
|
147
|
+
const okIds = results.filter((r) => r.ok).map(idOf).sort();
|
|
148
|
+
expect(okIds).toEqual(kept.map((r) => r.id as string).sort());
|
|
149
|
+
for (const r of results.filter((x) => !x.ok)) expect(r).toMatchObject({ status: 429, reason: "too_many_pending_adds" });
|
|
150
|
+
}
|
|
151
|
+
// Once it settles, sequential adds fill up to the cap and no further.
|
|
152
|
+
for (let i = (await rows()).length; i < POC_IN_APP_PENDING_CAP_PER_PARTNER; i++) {
|
|
153
|
+
expect((await add(org, { display_name: `Seq${i}`, emails: [{ value: `seq${i}@c.org` }] })).ok).toBe(true);
|
|
154
|
+
}
|
|
155
|
+
expect(await add(org, { display_name: "After", emails: [{ value: "after@c.org" }] })).toMatchObject({
|
|
156
|
+
status: 429,
|
|
157
|
+
reason: "too_many_pending_adds",
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("fails closed without the indexes: no collection (NamespaceNotFound) or a wrong spec is indexes_missing, and nothing is created", async () => {
|
|
162
|
+
const bareName = `poc_writer_bare_${new ObjectId().toHexString()}`;
|
|
163
|
+
const bare = client.db(bareName);
|
|
164
|
+
try {
|
|
165
|
+
await bare.collection("organizations").insertOne({ _id: ORG_C, name: "C (no agency)" });
|
|
166
|
+
const org = { org_id: ORG_C.toHexString() };
|
|
167
|
+
const desk = { display_name: "Desk", emails: [{ value: "desk@c.org" }] };
|
|
168
|
+
expect(await add(org, desk, bare)).toMatchObject({ ok: false, status: 503, reason: "indexes_missing" });
|
|
169
|
+
const created = await bare.listCollections({ name: POINTS_OF_CONTACT_COLLECTION }).toArray();
|
|
170
|
+
expect(created).toHaveLength(0);
|
|
171
|
+
|
|
172
|
+
// The interim multikey variant under the right name is not the index.
|
|
173
|
+
await bare.collection(POINTS_OF_CONTACT_COLLECTION).createIndex({ id: 1 }, { unique: true, name: "pointsofcontact_id_unique" });
|
|
174
|
+
await bare.collection(POINTS_OF_CONTACT_COLLECTION).createIndex(
|
|
175
|
+
{ agency_id: 1, subject_type: 1, subject_id: 1, "emails.value": 1, createdBy: 1 },
|
|
176
|
+
{ name: POC_IN_APP_PENDING_INDEX, unique: true, partialFilterExpression: { status: "pending", origin: "in_app", active: true } },
|
|
177
|
+
);
|
|
178
|
+
const h = client.db(bareName);
|
|
179
|
+
expect(await add(org, desk, h)).toMatchObject({ reason: "indexes_missing" });
|
|
180
|
+
expect(await bare.collection(POINTS_OF_CONTACT_COLLECTION).countDocuments({})).toBe(0);
|
|
181
|
+
|
|
182
|
+
// The platform fixes it; the same handle then writes (a refusal is not memoized).
|
|
183
|
+
await bare.collection(POINTS_OF_CONTACT_COLLECTION).dropIndex(POC_IN_APP_PENDING_INDEX);
|
|
184
|
+
await ensurePocIndexes(client.db(bareName));
|
|
185
|
+
expect(await add(org, desk, h)).toMatchObject({ ok: true, value: { created: true } });
|
|
186
|
+
} finally {
|
|
187
|
+
await bare.dropDatabase();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|