@proconnect-gouv/proconnect.identite.runtime 0.0.0-stage → 0.2.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/CHANGELOG.md +24 -0
- package/dist/activity/activity-context-schemas.d.ts +20 -0
- package/dist/activity/activity-context-schemas.d.ts.map +1 -0
- package/dist/activity/activity-context-schemas.js +4 -0
- package/dist/activity/index.d.ts +3 -0
- package/dist/activity/index.d.ts.map +1 -0
- package/dist/activity/index.js +3 -0
- package/dist/activity/types.d.ts +36 -0
- package/dist/activity/types.d.ts.map +1 -0
- package/dist/activity/types.js +2 -0
- package/dist/database/index.d.ts +5 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +2 -0
- package/dist/domains/activities.d.ts +19 -0
- package/dist/domains/activities.d.ts.map +1 -0
- package/dist/domains/activities.js +16 -0
- package/dist/domains/index.d.ts +3 -0
- package/dist/domains/index.d.ts.map +1 -0
- package/dist/domains/index.js +3 -0
- package/dist/domains/mark-domain-as-verified.d.ts +25 -0
- package/dist/domains/mark-domain-as-verified.d.ts.map +1 -0
- package/dist/domains/mark-domain-as-verified.js +88 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/journal/index.d.ts +2 -0
- package/dist/journal/index.d.ts.map +1 -0
- package/dist/journal/index.js +2 -0
- package/dist/journal/insert-activity.d.ts +6 -0
- package/dist/journal/insert-activity.d.ts.map +1 -0
- package/dist/journal/insert-activity.js +22 -0
- package/dist/runtime/create-identite-runtime.d.ts +32 -0
- package/dist/runtime/create-identite-runtime.d.ts.map +1 -0
- package/dist/runtime/create-identite-runtime.js +14 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +2 -0
- package/package.json +50 -4
- package/src/activity/activity-context-schemas.ts +10 -0
- package/src/activity/index.ts +4 -0
- package/src/activity/types.ts +39 -0
- package/src/database/index.ts +12 -0
- package/src/domains/activities.ts +19 -0
- package/src/domains/index.ts +4 -0
- package/src/domains/mark-domain-as-verified.test.ts +346 -0
- package/src/domains/mark-domain-as-verified.ts +154 -0
- package/src/index.ts +5 -0
- package/src/journal/index.ts +3 -0
- package/src/journal/insert-activity.ts +43 -0
- package/src/runtime/create-identite-runtime.test.ts +147 -0
- package/src/runtime/create-identite-runtime.ts +33 -0
- package/src/runtime/index.ts +3 -0
- package/tsconfig.json +14 -0
- package/tsconfig.lib.json +9 -0
- package/README.md +0 -3
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { RuntimeContext } from "#src/activity";
|
|
4
|
+
import { createIdentiteRuntime } from "#src/runtime";
|
|
5
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
6
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
7
|
+
import { migrate } from "@proconnect-gouv/proconnect.identite.database/pglite/migrator";
|
|
8
|
+
import { drizzle } from "drizzle-orm/pglite";
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { before, describe, it, mock } from "node:test";
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
|
|
14
|
+
const join_official_contact = {
|
|
15
|
+
actor: { type: "app", job: "join.official_contact" },
|
|
16
|
+
app: "proconnect-identite",
|
|
17
|
+
} satisfies Pick<RuntimeContext, "actor" | "app">;
|
|
18
|
+
|
|
19
|
+
async function setup({ actor, app }: Pick<RuntimeContext, "actor" | "app">) {
|
|
20
|
+
const pg = new PGlite();
|
|
21
|
+
await migrate(pg);
|
|
22
|
+
|
|
23
|
+
await pg.exec(`
|
|
24
|
+
SET search_path TO public;
|
|
25
|
+
|
|
26
|
+
INSERT INTO organizations
|
|
27
|
+
(id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
|
|
28
|
+
VALUES
|
|
29
|
+
(1, 'Dark Angels', 'Organization verifying darkangels.world', '🦁', '4444-04-04', '4444-04-04'),
|
|
30
|
+
(2, 'Space Wolves', 'Other organization using darkangels.world', '🐺', '4444-04-04', '4444-04-04');
|
|
31
|
+
|
|
32
|
+
INSERT INTO users
|
|
33
|
+
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
|
|
34
|
+
VALUES
|
|
35
|
+
(1, 'lion.eljonson+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', '0', 'Primarque'),
|
|
36
|
+
(2, 'azrael+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Azrael', 'Supreme', '0', 'Grand Master'),
|
|
37
|
+
(3, 'luther+kept-dirigeant@darkangels.world', '4444-04-04', '4444-04-04', 'Luther', 'Fallen', '0', 'Second'),
|
|
38
|
+
(4, 'leman.russ+kept-other-domain@spacewolves.world', '4444-04-04', '4444-04-04', 'Leman', 'Russ', '0', 'Primarque');
|
|
39
|
+
|
|
40
|
+
INSERT INTO users_organizations
|
|
41
|
+
(user_id, organization_id, created_at, updated_at, is_external, verification_type)
|
|
42
|
+
VALUES
|
|
43
|
+
(1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_entreprise_unipersonnelle'),
|
|
44
|
+
(2, 1, '4444-04-04', '4444-04-04', false, 'domain_not_verified_yet'),
|
|
45
|
+
(3, 1, '4444-04-04', '4444-04-04', false, 'organization_dirigeant'),
|
|
46
|
+
(4, 1, '4444-04-04', '4444-04-04', false, 'no_validation_means_available'),
|
|
47
|
+
(1, 2, '4444-04-04', '4444-04-04', false, 'no_validation_means_available');
|
|
48
|
+
|
|
49
|
+
INSERT INTO email_domains
|
|
50
|
+
(id, organization_id, domain, verification_type, created_at, updated_at)
|
|
51
|
+
VALUES
|
|
52
|
+
(1, 1, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04'),
|
|
53
|
+
(2, 1, 'darkangels.world', 'refused', '4444-04-04', '4444-04-04'),
|
|
54
|
+
(3, 1, 'calibanites.world', 'verified', '4444-04-04', '4444-04-04'),
|
|
55
|
+
(4, 2, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04');
|
|
56
|
+
`);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
pg,
|
|
60
|
+
runtime: createIdentiteRuntime({
|
|
61
|
+
app,
|
|
62
|
+
db: drizzle(pg, { schema }),
|
|
63
|
+
}).as(actor),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("markDomainAsVerified", () => {
|
|
68
|
+
before(() => {
|
|
69
|
+
mock.timers.enable({ apis: ["Date"], now: new Date("5555-05-05") });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("❎ returns organization_not_found for an unknown organization", async () => {
|
|
73
|
+
const { pg, runtime } = await setup(join_official_contact);
|
|
74
|
+
const before = await snapshot(pg);
|
|
75
|
+
|
|
76
|
+
const outcome = await runtime.markDomainAsVerified({
|
|
77
|
+
domain: "notfound.world",
|
|
78
|
+
domain_verification_type: "official_contact",
|
|
79
|
+
organization_id: 404,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
assert.deepEqual(outcome, {
|
|
83
|
+
kind: "organization_not_found",
|
|
84
|
+
organization_id: 404,
|
|
85
|
+
});
|
|
86
|
+
assert.deepEqual(await snapshot(pg), before);
|
|
87
|
+
assert.deepEqual(await readActivityLogs(pg), []);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("✅ replaces the domain states, promotes the domain members and journals the verification", async () => {
|
|
91
|
+
const { pg, runtime } = await setup(join_official_contact);
|
|
92
|
+
|
|
93
|
+
const outcome = await runtime.markDomainAsVerified({
|
|
94
|
+
domain: "darkangels.world",
|
|
95
|
+
domain_verification_type: "official_contact",
|
|
96
|
+
organization_id: 1,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const {
|
|
100
|
+
rows: [official_contact_domain],
|
|
101
|
+
} = await pg.query<{ id: number }>(
|
|
102
|
+
"SELECT id FROM email_domains WHERE verification_type = 'official_contact'",
|
|
103
|
+
);
|
|
104
|
+
assert.deepEqual(outcome, {
|
|
105
|
+
kind: "domain_verified",
|
|
106
|
+
activity_id: 1,
|
|
107
|
+
email_domain: {
|
|
108
|
+
id: official_contact_domain?.id,
|
|
109
|
+
verification_type: "official_contact",
|
|
110
|
+
},
|
|
111
|
+
promoted_members: [
|
|
112
|
+
{
|
|
113
|
+
previous_verification_type:
|
|
114
|
+
"no_verification_means_for_entreprise_unipersonnelle",
|
|
115
|
+
user_id: 1,
|
|
116
|
+
},
|
|
117
|
+
{ previous_verification_type: "domain_not_verified_yet", user_id: 2 },
|
|
118
|
+
],
|
|
119
|
+
replaced_verification_types: ["not_verified_yet", "refused"],
|
|
120
|
+
});
|
|
121
|
+
assert.deepEqual(
|
|
122
|
+
(
|
|
123
|
+
await pg.query(`
|
|
124
|
+
SELECT
|
|
125
|
+
organization_id,
|
|
126
|
+
domain,
|
|
127
|
+
verification_type,
|
|
128
|
+
can_be_suggested,
|
|
129
|
+
created_at,
|
|
130
|
+
verified_at
|
|
131
|
+
FROM email_domains
|
|
132
|
+
ORDER BY organization_id, domain, verification_type
|
|
133
|
+
`)
|
|
134
|
+
).rows,
|
|
135
|
+
[
|
|
136
|
+
{
|
|
137
|
+
can_be_suggested: true,
|
|
138
|
+
created_at: new Date("4444-04-04"),
|
|
139
|
+
domain: "calibanites.world",
|
|
140
|
+
organization_id: 1,
|
|
141
|
+
verification_type: "verified",
|
|
142
|
+
verified_at: null,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
can_be_suggested: true,
|
|
146
|
+
created_at: new Date("5555-05-05"),
|
|
147
|
+
domain: "darkangels.world",
|
|
148
|
+
organization_id: 1,
|
|
149
|
+
verification_type: "official_contact",
|
|
150
|
+
verified_at: new Date("5555-05-05"),
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
can_be_suggested: true,
|
|
154
|
+
created_at: new Date("4444-04-04"),
|
|
155
|
+
domain: "darkangels.world",
|
|
156
|
+
organization_id: 2,
|
|
157
|
+
verification_type: "not_verified_yet",
|
|
158
|
+
verified_at: null,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
);
|
|
162
|
+
assert.deepEqual(
|
|
163
|
+
(
|
|
164
|
+
await pg.query(`
|
|
165
|
+
SELECT
|
|
166
|
+
organization_id,
|
|
167
|
+
user_id,
|
|
168
|
+
verification_type,
|
|
169
|
+
updated_at
|
|
170
|
+
FROM users_organizations
|
|
171
|
+
ORDER BY organization_id, user_id
|
|
172
|
+
`)
|
|
173
|
+
).rows,
|
|
174
|
+
[
|
|
175
|
+
{
|
|
176
|
+
organization_id: 1,
|
|
177
|
+
updated_at: new Date("5555-05-05"),
|
|
178
|
+
user_id: 1,
|
|
179
|
+
verification_type: "domain",
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
organization_id: 1,
|
|
183
|
+
updated_at: new Date("5555-05-05"),
|
|
184
|
+
user_id: 2,
|
|
185
|
+
verification_type: "domain",
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
organization_id: 1,
|
|
189
|
+
updated_at: new Date("4444-04-04"),
|
|
190
|
+
user_id: 3,
|
|
191
|
+
verification_type: "organization_dirigeant",
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
organization_id: 1,
|
|
195
|
+
updated_at: new Date("4444-04-04"),
|
|
196
|
+
user_id: 4,
|
|
197
|
+
verification_type: "no_validation_means_available",
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
organization_id: 2,
|
|
201
|
+
updated_at: new Date("4444-04-04"),
|
|
202
|
+
user_id: 1,
|
|
203
|
+
verification_type: "no_validation_means_available",
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
);
|
|
207
|
+
assert.deepEqual(await readActivityLogs(pg), [
|
|
208
|
+
{
|
|
209
|
+
action: "domain.verified",
|
|
210
|
+
actor_ref: "join.official_contact",
|
|
211
|
+
actor_type: "app",
|
|
212
|
+
app: "proconnect-identite",
|
|
213
|
+
context: {
|
|
214
|
+
inputs: {
|
|
215
|
+
domain: "darkangels.world",
|
|
216
|
+
verification_type: "official_contact",
|
|
217
|
+
},
|
|
218
|
+
outcome: {
|
|
219
|
+
email_domain_id: official_contact_domain?.id,
|
|
220
|
+
replaced_verification_types: ["not_verified_yet", "refused"],
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
created_at: new Date("5555-05-05"),
|
|
224
|
+
id: 1,
|
|
225
|
+
interaction_cid: null,
|
|
226
|
+
moderation_id: null,
|
|
227
|
+
organization_id: 1,
|
|
228
|
+
user_id: null,
|
|
229
|
+
},
|
|
230
|
+
]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("✅ journals the verification under the moderator", async () => {
|
|
234
|
+
const { pg, runtime } = await setup({
|
|
235
|
+
actor: { type: "moderator", sub: "moderator+verifies-darkangels.world" },
|
|
236
|
+
app: "hyyypertool",
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const outcome = await runtime.markDomainAsVerified({
|
|
240
|
+
domain: "darkangels.world",
|
|
241
|
+
domain_verification_type: "verified",
|
|
242
|
+
organization_id: 1,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const {
|
|
246
|
+
rows: [verified_domain],
|
|
247
|
+
} = await pg.query<{ id: number }>(
|
|
248
|
+
"SELECT id FROM email_domains WHERE domain = 'darkangels.world' AND verification_type = 'verified'",
|
|
249
|
+
);
|
|
250
|
+
assert.deepEqual(outcome, {
|
|
251
|
+
kind: "domain_verified",
|
|
252
|
+
activity_id: 1,
|
|
253
|
+
email_domain: {
|
|
254
|
+
id: verified_domain?.id,
|
|
255
|
+
verification_type: "verified",
|
|
256
|
+
},
|
|
257
|
+
promoted_members: [
|
|
258
|
+
{
|
|
259
|
+
previous_verification_type:
|
|
260
|
+
"no_verification_means_for_entreprise_unipersonnelle",
|
|
261
|
+
user_id: 1,
|
|
262
|
+
},
|
|
263
|
+
{ previous_verification_type: "domain_not_verified_yet", user_id: 2 },
|
|
264
|
+
],
|
|
265
|
+
replaced_verification_types: ["not_verified_yet", "refused"],
|
|
266
|
+
});
|
|
267
|
+
assert.deepEqual(await readActivityLogs(pg), [
|
|
268
|
+
{
|
|
269
|
+
action: "domain.verified",
|
|
270
|
+
actor_ref: "moderator+verifies-darkangels.world",
|
|
271
|
+
actor_type: "moderator",
|
|
272
|
+
app: "hyyypertool",
|
|
273
|
+
context: {
|
|
274
|
+
inputs: {
|
|
275
|
+
domain: "darkangels.world",
|
|
276
|
+
verification_type: "verified",
|
|
277
|
+
},
|
|
278
|
+
outcome: {
|
|
279
|
+
email_domain_id: verified_domain?.id,
|
|
280
|
+
replaced_verification_types: ["not_verified_yet", "refused"],
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
created_at: new Date("5555-05-05"),
|
|
284
|
+
id: 1,
|
|
285
|
+
interaction_cid: null,
|
|
286
|
+
moderation_id: null,
|
|
287
|
+
organization_id: 1,
|
|
288
|
+
user_id: null,
|
|
289
|
+
},
|
|
290
|
+
]);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("❎ rolls back the domain state and its journal row when a member update fails", async () => {
|
|
294
|
+
const { pg, runtime } = await setup(join_official_contact);
|
|
295
|
+
await pg.exec(`
|
|
296
|
+
ALTER TABLE users_organizations
|
|
297
|
+
ADD CONSTRAINT users_organizations_never_domain
|
|
298
|
+
CHECK (verification_type <> 'domain');
|
|
299
|
+
`);
|
|
300
|
+
const before = await snapshot(pg);
|
|
301
|
+
|
|
302
|
+
await assert.rejects(
|
|
303
|
+
runtime.markDomainAsVerified({
|
|
304
|
+
domain: "darkangels.world",
|
|
305
|
+
domain_verification_type: "official_contact",
|
|
306
|
+
organization_id: 1,
|
|
307
|
+
}),
|
|
308
|
+
(error: Error) =>
|
|
309
|
+
error.cause instanceof Error &&
|
|
310
|
+
error.cause.message.includes("users_organizations_never_domain"),
|
|
311
|
+
);
|
|
312
|
+
assert.deepEqual(await snapshot(pg), before);
|
|
313
|
+
assert.deepEqual(await readActivityLogs(pg), []);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
async function snapshot(pg: PGlite) {
|
|
318
|
+
const [{ rows: email_domains }, { rows: users_organizations }] =
|
|
319
|
+
await Promise.all([
|
|
320
|
+
pg.query("SELECT * FROM email_domains ORDER BY id"),
|
|
321
|
+
pg.query(
|
|
322
|
+
"SELECT * FROM users_organizations ORDER BY organization_id, user_id",
|
|
323
|
+
),
|
|
324
|
+
]);
|
|
325
|
+
return { email_domains, users_organizations };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function readActivityLogs(pg: PGlite) {
|
|
329
|
+
const { rows } = await pg.query(`
|
|
330
|
+
SELECT
|
|
331
|
+
action,
|
|
332
|
+
actor_ref,
|
|
333
|
+
actor_type,
|
|
334
|
+
app,
|
|
335
|
+
context,
|
|
336
|
+
created_at,
|
|
337
|
+
id,
|
|
338
|
+
interaction_cid,
|
|
339
|
+
moderation_id,
|
|
340
|
+
organization_id,
|
|
341
|
+
user_id
|
|
342
|
+
FROM activity_logs
|
|
343
|
+
ORDER BY id
|
|
344
|
+
`);
|
|
345
|
+
return rows;
|
|
346
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ActivityContexts, RuntimeContext } from "#src/activity";
|
|
4
|
+
import { insertActivity } from "#src/journal";
|
|
5
|
+
import {
|
|
6
|
+
email_domains,
|
|
7
|
+
users_organizations,
|
|
8
|
+
} from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
9
|
+
import { getMemberIdsPromotedByDomain } from "@proconnect-gouv/proconnect.identite/services/organization";
|
|
10
|
+
import {
|
|
11
|
+
EmailDomainApprovedVerificationValues,
|
|
12
|
+
EmailDomainPendingVerificationValues,
|
|
13
|
+
EmailDomainRejectedVerificationValues,
|
|
14
|
+
LinkEnum,
|
|
15
|
+
type EmailDomainApprovedVerificationType,
|
|
16
|
+
} from "@proconnect-gouv/proconnect.identite/types";
|
|
17
|
+
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
18
|
+
|
|
19
|
+
//
|
|
20
|
+
|
|
21
|
+
type MarkDomainAsVerifiedOutcome =
|
|
22
|
+
| {
|
|
23
|
+
kind: "domain_verified";
|
|
24
|
+
activity_id: number;
|
|
25
|
+
email_domain: {
|
|
26
|
+
id: number;
|
|
27
|
+
verification_type: EmailDomainApprovedVerificationType;
|
|
28
|
+
};
|
|
29
|
+
promoted_members: ReadonlyArray<{
|
|
30
|
+
previous_verification_type: string;
|
|
31
|
+
user_id: number;
|
|
32
|
+
}>;
|
|
33
|
+
replaced_verification_types: ReadonlyArray<string>;
|
|
34
|
+
}
|
|
35
|
+
| { kind: "organization_not_found"; organization_id: number };
|
|
36
|
+
|
|
37
|
+
export function markDomainAsVerifiedFactory({
|
|
38
|
+
actor,
|
|
39
|
+
app,
|
|
40
|
+
db,
|
|
41
|
+
}: RuntimeContext) {
|
|
42
|
+
return function markDomainAsVerified({
|
|
43
|
+
domain,
|
|
44
|
+
domain_verification_type,
|
|
45
|
+
organization_id,
|
|
46
|
+
}: {
|
|
47
|
+
domain: string;
|
|
48
|
+
domain_verification_type: EmailDomainApprovedVerificationType;
|
|
49
|
+
organization_id: number;
|
|
50
|
+
}): Promise<MarkDomainAsVerifiedOutcome> {
|
|
51
|
+
return db.transaction(async (tx) => {
|
|
52
|
+
const organization = await tx.query.organizations.findFirst({
|
|
53
|
+
columns: { id: true },
|
|
54
|
+
where: (table, { eq }) => eq(table.id, organization_id),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!organization) {
|
|
58
|
+
return { kind: "organization_not_found", organization_id };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const memberships = await tx.query.users_organizations.findMany({
|
|
62
|
+
columns: { user_id: true, verification_type: true },
|
|
63
|
+
where: (table, { eq }) => eq(table.organization_id, organization_id),
|
|
64
|
+
with: { user: { columns: { email: true } } },
|
|
65
|
+
});
|
|
66
|
+
const members = memberships.map(
|
|
67
|
+
({ user: { email }, user_id, verification_type }) => ({
|
|
68
|
+
email,
|
|
69
|
+
id: user_id,
|
|
70
|
+
verification_type,
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const promoted_member_ids = getMemberIdsPromotedByDomain({
|
|
75
|
+
domain,
|
|
76
|
+
members,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const replaced = await tx
|
|
80
|
+
.delete(email_domains)
|
|
81
|
+
.where(
|
|
82
|
+
and(
|
|
83
|
+
eq(email_domains.organization_id, organization_id),
|
|
84
|
+
eq(email_domains.domain, domain),
|
|
85
|
+
inArray(email_domains.verification_type, [
|
|
86
|
+
...EmailDomainApprovedVerificationValues,
|
|
87
|
+
...EmailDomainPendingVerificationValues,
|
|
88
|
+
...EmailDomainRejectedVerificationValues,
|
|
89
|
+
]),
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
.returning({ verification_type: email_domains.verification_type });
|
|
93
|
+
|
|
94
|
+
const [email_domain] = await tx
|
|
95
|
+
.insert(email_domains)
|
|
96
|
+
.values({
|
|
97
|
+
domain,
|
|
98
|
+
organization_id,
|
|
99
|
+
verification_type: domain_verification_type,
|
|
100
|
+
verified_at: sql`now()`,
|
|
101
|
+
})
|
|
102
|
+
.returning({ id: email_domains.id });
|
|
103
|
+
|
|
104
|
+
const replaced_verification_types = replaced.map(
|
|
105
|
+
({ verification_type }) => verification_type,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
109
|
+
action: "domain.verified",
|
|
110
|
+
actor,
|
|
111
|
+
app,
|
|
112
|
+
context: {
|
|
113
|
+
inputs: { domain, verification_type: domain_verification_type },
|
|
114
|
+
outcome: {
|
|
115
|
+
email_domain_id: email_domain.id,
|
|
116
|
+
replaced_verification_types,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
organization_id,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (promoted_member_ids.length > 0) {
|
|
123
|
+
await tx
|
|
124
|
+
.update(users_organizations)
|
|
125
|
+
.set({
|
|
126
|
+
updated_at: sql`now()`,
|
|
127
|
+
verification_type: LinkEnum.enum.domain,
|
|
128
|
+
})
|
|
129
|
+
.where(
|
|
130
|
+
and(
|
|
131
|
+
eq(users_organizations.organization_id, organization_id),
|
|
132
|
+
inArray(users_organizations.user_id, promoted_member_ids),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
kind: "domain_verified",
|
|
139
|
+
activity_id,
|
|
140
|
+
email_domain: {
|
|
141
|
+
id: email_domain.id,
|
|
142
|
+
verification_type: domain_verification_type,
|
|
143
|
+
},
|
|
144
|
+
promoted_members: memberships
|
|
145
|
+
.filter(({ user_id }) => promoted_member_ids.includes(user_id))
|
|
146
|
+
.map(({ user_id, verification_type }) => ({
|
|
147
|
+
previous_verification_type: verification_type,
|
|
148
|
+
user_id,
|
|
149
|
+
})),
|
|
150
|
+
replaced_verification_types,
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ActivityOf,
|
|
5
|
+
ActivityTargets,
|
|
6
|
+
ContextMap,
|
|
7
|
+
RuntimeContext,
|
|
8
|
+
} from "#src/activity";
|
|
9
|
+
import type { IdentiteTransaction } from "#src/database";
|
|
10
|
+
import { activity_logs } from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
|
|
14
|
+
export async function insertActivity<Contexts extends ContextMap>(
|
|
15
|
+
tx: IdentiteTransaction,
|
|
16
|
+
{
|
|
17
|
+
actor,
|
|
18
|
+
app,
|
|
19
|
+
...activity
|
|
20
|
+
}: ActivityOf<Contexts> &
|
|
21
|
+
ActivityTargets &
|
|
22
|
+
Pick<RuntimeContext, "actor" | "app">,
|
|
23
|
+
) {
|
|
24
|
+
const [row] = await tx
|
|
25
|
+
.insert(activity_logs)
|
|
26
|
+
.values({ ...activity, ...toActorColumns({ actor, app }) })
|
|
27
|
+
.returning({ id: activity_logs.id });
|
|
28
|
+
|
|
29
|
+
return { activity_id: row.id };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function toActorColumns({ actor, app }: Pick<RuntimeContext, "actor" | "app">) {
|
|
33
|
+
switch (actor.type) {
|
|
34
|
+
case "app":
|
|
35
|
+
return { actor_ref: actor.job, actor_type: "app", app } as const;
|
|
36
|
+
case "moderator":
|
|
37
|
+
return { actor_ref: actor.sub, actor_type: "moderator", app } as const;
|
|
38
|
+
default: {
|
|
39
|
+
const unhandled_actor: never = actor;
|
|
40
|
+
throw new Error(`Unhandled actor: ${JSON.stringify(unhandled_actor)}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ContextsOf } from "#src/activity";
|
|
4
|
+
import type { IdentiteTransaction } from "#src/database";
|
|
5
|
+
import { createIdentiteRuntime } from "#src/runtime";
|
|
6
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
7
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
8
|
+
import { migrate } from "@proconnect-gouv/proconnect.identite.database/pglite/migrator";
|
|
9
|
+
import { TransactionRollbackError } from "drizzle-orm";
|
|
10
|
+
import { drizzle } from "drizzle-orm/pglite";
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import { before, describe, it, mock } from "node:test";
|
|
13
|
+
import * as z from "zod";
|
|
14
|
+
|
|
15
|
+
//
|
|
16
|
+
|
|
17
|
+
const hyyypertool_activities = {
|
|
18
|
+
"domain.refused": z.object({
|
|
19
|
+
inputs: z.object({
|
|
20
|
+
domain: z.string(),
|
|
21
|
+
verification_type: z.literal("refused"),
|
|
22
|
+
}),
|
|
23
|
+
outcome: z.object({
|
|
24
|
+
email_domain_id: z.number(),
|
|
25
|
+
replaced_verification_types: z.array(z.string()),
|
|
26
|
+
}),
|
|
27
|
+
}),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type HyyypertoolActivityContexts = ContextsOf<typeof hyyypertool_activities>;
|
|
31
|
+
|
|
32
|
+
const darkangels_refused = {
|
|
33
|
+
action: "domain.refused" as const,
|
|
34
|
+
context: {
|
|
35
|
+
inputs: {
|
|
36
|
+
domain: "darkangels.world",
|
|
37
|
+
verification_type: "refused" as const,
|
|
38
|
+
},
|
|
39
|
+
outcome: {
|
|
40
|
+
email_domain_id: 7,
|
|
41
|
+
replaced_verification_types: ["not_verified_yet"],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
organization_id: 1,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
async function setup() {
|
|
48
|
+
const pg = new PGlite();
|
|
49
|
+
await migrate(pg);
|
|
50
|
+
await pg.exec("SET search_path TO public;");
|
|
51
|
+
const db = drizzle(pg, { schema });
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
db,
|
|
55
|
+
moderator: createIdentiteRuntime<HyyypertoolActivityContexts>({
|
|
56
|
+
app: "hyyypertool",
|
|
57
|
+
db,
|
|
58
|
+
}).as({ type: "moderator", sub: "moderator+refuses-darkangels.world" }),
|
|
59
|
+
pg,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe("record", () => {
|
|
64
|
+
before(() => {
|
|
65
|
+
mock.timers.enable({ apis: ["Date"], now: new Date("5555-05-05") });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("✅ journals the caller's own action under its actor, in the caller's transaction", async () => {
|
|
69
|
+
const { db, moderator, pg } = await setup();
|
|
70
|
+
|
|
71
|
+
const outcome = await db.transaction((tx) =>
|
|
72
|
+
moderator.record(tx, darkangels_refused),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
assert.deepEqual(outcome, { activity_id: 1 });
|
|
76
|
+
assert.deepEqual(
|
|
77
|
+
(await pg.query("SELECT * FROM activity_logs ORDER BY id")).rows,
|
|
78
|
+
[
|
|
79
|
+
{
|
|
80
|
+
action: "domain.refused",
|
|
81
|
+
actor_ref: "moderator+refuses-darkangels.world",
|
|
82
|
+
actor_type: "moderator",
|
|
83
|
+
app: "hyyypertool",
|
|
84
|
+
context: darkangels_refused.context,
|
|
85
|
+
created_at: new Date("5555-05-05"),
|
|
86
|
+
id: 1,
|
|
87
|
+
interaction_cid: null,
|
|
88
|
+
moderation_id: null,
|
|
89
|
+
organization_id: 1,
|
|
90
|
+
user_id: null,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("❎ leaves no row when the caller's transaction rolls back", async () => {
|
|
97
|
+
const { db, moderator, pg } = await setup();
|
|
98
|
+
|
|
99
|
+
await assert.rejects(
|
|
100
|
+
db.transaction(async (tx) => {
|
|
101
|
+
await moderator.record(tx, darkangels_refused);
|
|
102
|
+
tx.rollback();
|
|
103
|
+
}),
|
|
104
|
+
TransactionRollbackError,
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
assert.deepEqual((await pg.query("SELECT * FROM activity_logs")).rows, []);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("🚫 does not compile another owner's action, a mismatched context, the bare database or an overlapping owner", () => {
|
|
111
|
+
const db = drizzle(new PGlite(), { schema });
|
|
112
|
+
const { record } = createIdentiteRuntime<HyyypertoolActivityContexts>({
|
|
113
|
+
app: "hyyypertool",
|
|
114
|
+
db,
|
|
115
|
+
}).as({ type: "moderator", sub: "moderator" });
|
|
116
|
+
|
|
117
|
+
void ((tx: IdentiteTransaction) =>
|
|
118
|
+
record(tx, {
|
|
119
|
+
// @ts-expect-error domain.verified belongs to the runtime, only markDomainAsVerified writes it
|
|
120
|
+
action: "domain.verified",
|
|
121
|
+
context: darkangels_refused.context,
|
|
122
|
+
}));
|
|
123
|
+
|
|
124
|
+
void ((tx: IdentiteTransaction) =>
|
|
125
|
+
record(tx, {
|
|
126
|
+
action: "domain.refused",
|
|
127
|
+
// @ts-expect-error the context does not match domain.refused
|
|
128
|
+
context: { inputs: { domain: "darkangels.world" }, outcome: {} },
|
|
129
|
+
}));
|
|
130
|
+
|
|
131
|
+
void (() =>
|
|
132
|
+
// @ts-expect-error the bare database is not a transaction
|
|
133
|
+
record(db, darkangels_refused));
|
|
134
|
+
|
|
135
|
+
type OverlappingContexts = {
|
|
136
|
+
"domain.verified": HyyypertoolActivityContexts["domain.refused"];
|
|
137
|
+
};
|
|
138
|
+
// @ts-expect-error domain.verified is already defined by the runtime
|
|
139
|
+
void createIdentiteRuntime<OverlappingContexts>({ app: "hyyypertool", db });
|
|
140
|
+
|
|
141
|
+
void ((tx: IdentiteTransaction) =>
|
|
142
|
+
createIdentiteRuntime({ app: "proconnect-identite", db })
|
|
143
|
+
.as({ type: "app", job: "join.official_contact" })
|
|
144
|
+
// @ts-expect-error proconnect-identite has no app-only action to record
|
|
145
|
+
.record(tx, darkangels_refused));
|
|
146
|
+
});
|
|
147
|
+
});
|