@proconnect-gouv/proconnect.identite.runtime 0.1.0 → 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.
Files changed (53) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/activity/activity-context-schemas.d.ts +20 -0
  3. package/dist/activity/activity-context-schemas.d.ts.map +1 -0
  4. package/dist/activity/activity-context-schemas.js +4 -0
  5. package/dist/activity/index.d.ts +3 -0
  6. package/dist/activity/index.d.ts.map +1 -0
  7. package/dist/activity/index.js +3 -0
  8. package/dist/activity/types.d.ts +36 -0
  9. package/dist/activity/types.d.ts.map +1 -0
  10. package/dist/activity/types.js +2 -0
  11. package/dist/database/index.d.ts +5 -0
  12. package/dist/database/index.d.ts.map +1 -0
  13. package/dist/database/index.js +2 -0
  14. package/dist/domains/activities.d.ts +19 -0
  15. package/dist/domains/activities.d.ts.map +1 -0
  16. package/dist/domains/activities.js +16 -0
  17. package/dist/domains/index.d.ts +3 -0
  18. package/dist/domains/index.d.ts.map +1 -0
  19. package/dist/domains/index.js +3 -0
  20. package/dist/domains/mark-domain-as-verified.d.ts +25 -0
  21. package/dist/domains/mark-domain-as-verified.d.ts.map +1 -0
  22. package/dist/domains/mark-domain-as-verified.js +88 -0
  23. package/dist/index.d.ts +4 -33
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +4 -88
  26. package/dist/journal/index.d.ts +2 -0
  27. package/dist/journal/index.d.ts.map +1 -0
  28. package/dist/journal/index.js +2 -0
  29. package/dist/journal/insert-activity.d.ts +6 -0
  30. package/dist/journal/insert-activity.d.ts.map +1 -0
  31. package/dist/journal/insert-activity.js +22 -0
  32. package/dist/runtime/create-identite-runtime.d.ts +32 -0
  33. package/dist/runtime/create-identite-runtime.d.ts.map +1 -0
  34. package/dist/runtime/create-identite-runtime.js +14 -0
  35. package/dist/runtime/index.d.ts +2 -0
  36. package/dist/runtime/index.d.ts.map +1 -0
  37. package/dist/runtime/index.js +2 -0
  38. package/package.json +11 -5
  39. package/src/activity/activity-context-schemas.ts +10 -0
  40. package/src/activity/index.ts +4 -0
  41. package/src/activity/types.ts +39 -0
  42. package/src/{database.ts → database/index.ts} +4 -0
  43. package/src/domains/activities.ts +19 -0
  44. package/src/domains/index.ts +4 -0
  45. package/src/{mark-domain-as-verified.test.ts → domains/mark-domain-as-verified.test.ts} +143 -23
  46. package/src/{mark-domain-as-verified.ts → domains/mark-domain-as-verified.ts} +28 -5
  47. package/src/index.ts +3 -12
  48. package/src/journal/index.ts +3 -0
  49. package/src/journal/insert-activity.ts +43 -0
  50. package/src/runtime/create-identite-runtime.test.ts +147 -0
  51. package/src/runtime/create-identite-runtime.ts +33 -0
  52. package/src/runtime/index.ts +3 -0
  53. package/tsconfig.json +6 -3
@@ -0,0 +1,39 @@
1
+ //
2
+
3
+ import type { IdentiteDatabase } from "#src/database";
4
+ import type * as z from "zod";
5
+
6
+ //
7
+
8
+ export type Actor =
9
+ | { type: "app"; job: string }
10
+ | { type: "moderator"; sub: string };
11
+
12
+ export type App = "hyyypertool" | "proconnect-identite";
13
+
14
+ export type RuntimeContext = {
15
+ actor: Actor;
16
+ app: App;
17
+ db: IdentiteDatabase;
18
+ };
19
+
20
+ type ContextShape = { inputs: unknown; outcome: unknown };
21
+
22
+ export type ContextMap = Record<string, ContextShape>;
23
+
24
+ export type ContextsOf<
25
+ Schemas extends Record<string, z.ZodType<ContextShape>>,
26
+ > = { [Action in keyof Schemas]: z.infer<Schemas[Action]> };
27
+
28
+ export type ActivityOf<Contexts extends ContextMap> = {
29
+ [Action in keyof Contexts & string]: {
30
+ action: Action;
31
+ context: Contexts[Action];
32
+ };
33
+ }[keyof Contexts & string];
34
+
35
+ export type ActivityTargets = {
36
+ moderation_id?: number;
37
+ organization_id?: number;
38
+ user_id?: number;
39
+ };
@@ -6,3 +6,7 @@ import type { PgDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core";
6
6
  //
7
7
 
8
8
  export type IdentiteDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;
9
+
10
+ export type IdentiteTransaction = Parameters<
11
+ Parameters<IdentiteDatabase["transaction"]>[0]
12
+ >[0];
@@ -0,0 +1,19 @@
1
+ //
2
+
3
+ import { EmailDomainApprovedVerificationEnum } from "@proconnect-gouv/proconnect.identite/types";
4
+ import * as z from "zod";
5
+
6
+ //
7
+
8
+ export const domainActivities = {
9
+ "domain.verified": z.object({
10
+ inputs: z.object({
11
+ domain: z.string(),
12
+ verification_type: EmailDomainApprovedVerificationEnum,
13
+ }),
14
+ outcome: z.object({
15
+ email_domain_id: z.number(),
16
+ replaced_verification_types: z.array(z.string()),
17
+ }),
18
+ }),
19
+ };
@@ -0,0 +1,4 @@
1
+ //
2
+
3
+ export * from "./activities.js";
4
+ export * from "./mark-domain-as-verified.js";
@@ -1,16 +1,22 @@
1
1
  //
2
2
 
3
+ import type { RuntimeContext } from "#src/activity";
4
+ import { createIdentiteRuntime } from "#src/runtime";
3
5
  import { PGlite } from "@electric-sql/pglite";
4
6
  import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
5
7
  import { migrate } from "@proconnect-gouv/proconnect.identite.database/pglite/migrator";
6
8
  import { drizzle } from "drizzle-orm/pglite";
7
9
  import assert from "node:assert/strict";
8
- import { describe, it } from "node:test";
9
- import { createIdentiteRuntime } from "./index.js";
10
+ import { before, describe, it, mock } from "node:test";
10
11
 
11
12
  //
12
13
 
13
- async function setup() {
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">) {
14
20
  const pg = new PGlite();
15
21
  await migrate(pg);
16
22
 
@@ -51,13 +57,20 @@ async function setup() {
51
57
 
52
58
  return {
53
59
  pg,
54
- runtime: createIdentiteRuntime({ db: drizzle(pg, { schema }) }),
60
+ runtime: createIdentiteRuntime({
61
+ app,
62
+ db: drizzle(pg, { schema }),
63
+ }).as(actor),
55
64
  };
56
65
  }
57
66
 
58
67
  describe("markDomainAsVerified", () => {
68
+ before(() => {
69
+ mock.timers.enable({ apis: ["Date"], now: new Date("5555-05-05") });
70
+ });
71
+
59
72
  it("❎ returns organization_not_found for an unknown organization", async () => {
60
- const { pg, runtime } = await setup();
73
+ const { pg, runtime } = await setup(join_official_contact);
61
74
  const before = await snapshot(pg);
62
75
 
63
76
  const outcome = await runtime.markDomainAsVerified({
@@ -71,10 +84,11 @@ describe("markDomainAsVerified", () => {
71
84
  organization_id: 404,
72
85
  });
73
86
  assert.deepEqual(await snapshot(pg), before);
87
+ assert.deepEqual(await readActivityLogs(pg), []);
74
88
  });
75
89
 
76
- it("✅ replaces the domain states and promotes the domain members", async () => {
77
- const { pg, runtime } = await setup();
90
+ it("✅ replaces the domain states, promotes the domain members and journals the verification", async () => {
91
+ const { pg, runtime } = await setup(join_official_contact);
78
92
 
79
93
  const outcome = await runtime.markDomainAsVerified({
80
94
  domain: "darkangels.world",
@@ -89,6 +103,7 @@ describe("markDomainAsVerified", () => {
89
103
  );
90
104
  assert.deepEqual(outcome, {
91
105
  kind: "domain_verified",
106
+ activity_id: 1,
92
107
  email_domain: {
93
108
  id: official_contact_domain?.id,
94
109
  verification_type: "official_contact",
@@ -111,8 +126,8 @@ describe("markDomainAsVerified", () => {
111
126
  domain,
112
127
  verification_type,
113
128
  can_be_suggested,
114
- verified_at IS NOT NULL AS is_verified,
115
- created_at = '4444-04-04' AS is_seeded
129
+ created_at,
130
+ verified_at
116
131
  FROM email_domains
117
132
  ORDER BY organization_id, domain, verification_type
118
133
  `)
@@ -120,27 +135,27 @@ describe("markDomainAsVerified", () => {
120
135
  [
121
136
  {
122
137
  can_be_suggested: true,
138
+ created_at: new Date("4444-04-04"),
123
139
  domain: "calibanites.world",
124
- is_seeded: true,
125
- is_verified: false,
126
140
  organization_id: 1,
127
141
  verification_type: "verified",
142
+ verified_at: null,
128
143
  },
129
144
  {
130
145
  can_be_suggested: true,
146
+ created_at: new Date("5555-05-05"),
131
147
  domain: "darkangels.world",
132
- is_seeded: false,
133
- is_verified: true,
134
148
  organization_id: 1,
135
149
  verification_type: "official_contact",
150
+ verified_at: new Date("5555-05-05"),
136
151
  },
137
152
  {
138
153
  can_be_suggested: true,
154
+ created_at: new Date("4444-04-04"),
139
155
  domain: "darkangels.world",
140
- is_seeded: true,
141
- is_verified: false,
142
156
  organization_id: 2,
143
157
  verification_type: "not_verified_yet",
158
+ verified_at: null,
144
159
  },
145
160
  ],
146
161
  );
@@ -151,48 +166,132 @@ describe("markDomainAsVerified", () => {
151
166
  organization_id,
152
167
  user_id,
153
168
  verification_type,
154
- updated_at <> '4444-04-04' AS is_updated
169
+ updated_at
155
170
  FROM users_organizations
156
171
  ORDER BY organization_id, user_id
157
172
  `)
158
173
  ).rows,
159
174
  [
160
175
  {
161
- is_updated: true,
162
176
  organization_id: 1,
177
+ updated_at: new Date("5555-05-05"),
163
178
  user_id: 1,
164
179
  verification_type: "domain",
165
180
  },
166
181
  {
167
- is_updated: true,
168
182
  organization_id: 1,
183
+ updated_at: new Date("5555-05-05"),
169
184
  user_id: 2,
170
185
  verification_type: "domain",
171
186
  },
172
187
  {
173
- is_updated: false,
174
188
  organization_id: 1,
189
+ updated_at: new Date("4444-04-04"),
175
190
  user_id: 3,
176
191
  verification_type: "organization_dirigeant",
177
192
  },
178
193
  {
179
- is_updated: false,
180
194
  organization_id: 1,
195
+ updated_at: new Date("4444-04-04"),
181
196
  user_id: 4,
182
197
  verification_type: "no_validation_means_available",
183
198
  },
184
199
  {
185
- is_updated: false,
186
200
  organization_id: 2,
201
+ updated_at: new Date("4444-04-04"),
187
202
  user_id: 1,
188
203
  verification_type: "no_validation_means_available",
189
204
  },
190
205
  ],
191
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
+ ]);
192
291
  });
193
292
 
194
- it("❎ rolls back the domain state when a member update fails", async () => {
195
- const { pg, runtime } = await setup();
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);
196
295
  await pg.exec(`
197
296
  ALTER TABLE users_organizations
198
297
  ADD CONSTRAINT users_organizations_never_domain
@@ -211,6 +310,7 @@ describe("markDomainAsVerified", () => {
211
310
  error.cause.message.includes("users_organizations_never_domain"),
212
311
  );
213
312
  assert.deepEqual(await snapshot(pg), before);
313
+ assert.deepEqual(await readActivityLogs(pg), []);
214
314
  });
215
315
  });
216
316
 
@@ -224,3 +324,23 @@ async function snapshot(pg: PGlite) {
224
324
  ]);
225
325
  return { email_domains, users_organizations };
226
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
+ }
@@ -1,5 +1,7 @@
1
1
  //
2
2
 
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
3
5
  import {
4
6
  email_domains,
5
7
  users_organizations,
@@ -13,13 +15,13 @@ import {
13
15
  type EmailDomainApprovedVerificationType,
14
16
  } from "@proconnect-gouv/proconnect.identite/types";
15
17
  import { and, eq, inArray, sql } from "drizzle-orm";
16
- import type { IdentiteDatabase } from "./database.js";
17
18
 
18
19
  //
19
20
 
20
21
  type MarkDomainAsVerifiedOutcome =
21
22
  | {
22
23
  kind: "domain_verified";
24
+ activity_id: number;
23
25
  email_domain: {
24
26
  id: number;
25
27
  verification_type: EmailDomainApprovedVerificationType;
@@ -32,7 +34,11 @@ type MarkDomainAsVerifiedOutcome =
32
34
  }
33
35
  | { kind: "organization_not_found"; organization_id: number };
34
36
 
35
- export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
37
+ export function markDomainAsVerifiedFactory({
38
+ actor,
39
+ app,
40
+ db,
41
+ }: RuntimeContext) {
36
42
  return function markDomainAsVerified({
37
43
  domain,
38
44
  domain_verification_type,
@@ -95,6 +101,24 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
95
101
  })
96
102
  .returning({ id: email_domains.id });
97
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
+
98
122
  if (promoted_member_ids.length > 0) {
99
123
  await tx
100
124
  .update(users_organizations)
@@ -112,6 +136,7 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
112
136
 
113
137
  return {
114
138
  kind: "domain_verified",
139
+ activity_id,
115
140
  email_domain: {
116
141
  id: email_domain.id,
117
142
  verification_type: domain_verification_type,
@@ -122,9 +147,7 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
122
147
  previous_verification_type: verification_type,
123
148
  user_id,
124
149
  })),
125
- replaced_verification_types: replaced.map(
126
- ({ verification_type }) => verification_type,
127
- ),
150
+ replaced_verification_types,
128
151
  };
129
152
  });
130
153
  };
package/src/index.ts CHANGED
@@ -1,14 +1,5 @@
1
1
  //
2
2
 
3
- import type { IdentiteDatabase } from "./database.js";
4
- import { markDomainAsVerifiedFactory } from "./mark-domain-as-verified.js";
5
-
6
- //
7
-
8
- export type { IdentiteDatabase } from "./database.js";
9
-
10
- export function createIdentiteRuntime({ db }: { db: IdentiteDatabase }) {
11
- return {
12
- markDomainAsVerified: markDomainAsVerifiedFactory({ db }),
13
- };
14
- }
3
+ export * from "./activity/index.js";
4
+ export * from "./database/index.js";
5
+ export * from "./runtime/index.js";
@@ -0,0 +1,3 @@
1
+ //
2
+
3
+ export * from "./insert-activity.js";
@@ -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
+ });
@@ -0,0 +1,33 @@
1
+ //
2
+
3
+ import type {
4
+ ActivityContexts,
5
+ ActivityOf,
6
+ ActivityTargets,
7
+ Actor,
8
+ ContextMap,
9
+ RuntimeContext,
10
+ } from "#src/activity";
11
+ import type { IdentiteTransaction } from "#src/database";
12
+ import { markDomainAsVerifiedFactory } from "#src/domains";
13
+ import { insertActivity } from "#src/journal";
14
+
15
+ //
16
+
17
+ export function createIdentiteRuntime<
18
+ OwnContexts extends ContextMap & {
19
+ [Action in keyof ActivityContexts]?: never;
20
+ } = {},
21
+ >({ app, db }: Omit<RuntimeContext, "actor">) {
22
+ return {
23
+ as(actor: Actor) {
24
+ return {
25
+ markDomainAsVerified: markDomainAsVerifiedFactory({ actor, app, db }),
26
+ record: (
27
+ tx: IdentiteTransaction,
28
+ activity: ActivityOf<OwnContexts> & ActivityTargets,
29
+ ) => insertActivity<OwnContexts>(tx, { ...activity, actor, app }),
30
+ };
31
+ },
32
+ };
33
+ }
@@ -0,0 +1,3 @@
1
+ //
2
+
3
+ export * from "./create-identite-runtime.js";