@proconnect-gouv/proconnect.identite.runtime 0.1.0 → 0.3.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 (62) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/activity/activity-context-schemas.d.ts +46 -0
  3. package/dist/activity/activity-context-schemas.d.ts.map +1 -0
  4. package/dist/activity/activity-context-schemas.js +8 -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 +103 -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 +8 -0
  30. package/dist/journal/insert-activity.d.ts.map +1 -0
  31. package/dist/journal/insert-activity.js +22 -0
  32. package/dist/memberships/activities.d.ts +30 -0
  33. package/dist/memberships/activities.d.ts.map +1 -0
  34. package/dist/memberships/activities.js +14 -0
  35. package/dist/memberships/index.d.ts +2 -0
  36. package/dist/memberships/index.d.ts.map +1 -0
  37. package/dist/memberships/index.js +2 -0
  38. package/dist/runtime/create-identite-runtime.d.ts +32 -0
  39. package/dist/runtime/create-identite-runtime.d.ts.map +1 -0
  40. package/dist/runtime/create-identite-runtime.js +14 -0
  41. package/dist/runtime/index.d.ts +2 -0
  42. package/dist/runtime/index.d.ts.map +1 -0
  43. package/dist/runtime/index.js +2 -0
  44. package/package.json +11 -5
  45. package/src/activity/activity-context-schemas.ts +14 -0
  46. package/src/activity/index.ts +4 -0
  47. package/src/activity/types.ts +39 -0
  48. package/src/{database.ts → database/index.ts} +4 -0
  49. package/src/domains/activities.ts +19 -0
  50. package/src/domains/index.ts +4 -0
  51. package/src/domains/mark-domain-as-verified.test.ts +423 -0
  52. package/src/{mark-domain-as-verified.ts → domains/mark-domain-as-verified.ts} +54 -11
  53. package/src/index.ts +3 -12
  54. package/src/journal/index.ts +3 -0
  55. package/src/journal/insert-activity.ts +43 -0
  56. package/src/memberships/activities.ts +17 -0
  57. package/src/memberships/index.ts +3 -0
  58. package/src/runtime/create-identite-runtime.test.ts +148 -0
  59. package/src/runtime/create-identite-runtime.ts +33 -0
  60. package/src/runtime/index.ts +3 -0
  61. package/tsconfig.json +6 -3
  62. package/src/mark-domain-as-verified.test.ts +0 -226
@@ -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,
@@ -70,6 +76,13 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
70
76
  members,
71
77
  });
72
78
 
79
+ const promoted_members = memberships
80
+ .filter(({ user_id }) => promoted_member_ids.includes(user_id))
81
+ .map(({ user_id, verification_type }) => ({
82
+ previous_verification_type: verification_type,
83
+ user_id,
84
+ }));
85
+
73
86
  const replaced = await tx
74
87
  .delete(email_domains)
75
88
  .where(
@@ -95,6 +108,24 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
95
108
  })
96
109
  .returning({ id: email_domains.id });
97
110
 
111
+ const replaced_verification_types = replaced.map(
112
+ ({ verification_type }) => verification_type,
113
+ );
114
+
115
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
116
+ action: "domain.verified",
117
+ actor,
118
+ app,
119
+ context: {
120
+ inputs: { domain, verification_type: domain_verification_type },
121
+ outcome: {
122
+ email_domain_id: email_domain.id,
123
+ replaced_verification_types,
124
+ },
125
+ },
126
+ organization_id,
127
+ });
128
+
98
129
  if (promoted_member_ids.length > 0) {
99
130
  await tx
100
131
  .update(users_organizations)
@@ -108,23 +139,35 @@ export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
108
139
  inArray(users_organizations.user_id, promoted_member_ids),
109
140
  ),
110
141
  );
142
+
143
+ for (const {
144
+ previous_verification_type,
145
+ user_id,
146
+ } of promoted_members) {
147
+ await insertActivity<ActivityContexts>(tx, {
148
+ action: "member.promoted",
149
+ actor: { type: "app", job: "domain.promote_members" },
150
+ app,
151
+ cause_activity_id: activity_id,
152
+ context: {
153
+ inputs: { previous_verification_type },
154
+ outcome: { verification_type: LinkEnum.enum.domain },
155
+ },
156
+ organization_id,
157
+ user_id,
158
+ });
159
+ }
111
160
  }
112
161
 
113
162
  return {
114
163
  kind: "domain_verified",
164
+ activity_id,
115
165
  email_domain: {
116
166
  id: email_domain.id,
117
167
  verification_type: domain_verification_type,
118
168
  },
119
- promoted_members: memberships
120
- .filter(({ user_id }) => promoted_member_ids.includes(user_id))
121
- .map(({ user_id, verification_type }) => ({
122
- previous_verification_type: verification_type,
123
- user_id,
124
- })),
125
- replaced_verification_types: replaced.map(
126
- ({ verification_type }) => verification_type,
127
- ),
169
+ promoted_members,
170
+ replaced_verification_types,
128
171
  };
129
172
  });
130
173
  };
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"> & { cause_activity_id?: number },
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,17 @@
1
+ //
2
+
3
+ import { LinkEnum } from "@proconnect-gouv/proconnect.identite/types";
4
+ import * as z from "zod";
5
+
6
+ //
7
+
8
+ export const membershipActivities = {
9
+ "member.promoted": z.object({
10
+ inputs: z.object({
11
+ previous_verification_type: z.string(),
12
+ }),
13
+ outcome: z.object({
14
+ verification_type: LinkEnum,
15
+ }),
16
+ }),
17
+ };
@@ -0,0 +1,3 @@
1
+ //
2
+
3
+ export * from "./activities.js";
@@ -0,0 +1,148 @@
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
+ cause_activity_id: null,
85
+ context: darkangels_refused.context,
86
+ created_at: new Date("5555-05-05"),
87
+ id: 1,
88
+ interaction_cid: null,
89
+ moderation_id: null,
90
+ organization_id: 1,
91
+ user_id: null,
92
+ },
93
+ ],
94
+ );
95
+ });
96
+
97
+ it("❎ leaves no row when the caller's transaction rolls back", async () => {
98
+ const { db, moderator, pg } = await setup();
99
+
100
+ await assert.rejects(
101
+ db.transaction(async (tx) => {
102
+ await moderator.record(tx, darkangels_refused);
103
+ tx.rollback();
104
+ }),
105
+ TransactionRollbackError,
106
+ );
107
+
108
+ assert.deepEqual((await pg.query("SELECT * FROM activity_logs")).rows, []);
109
+ });
110
+
111
+ it("🚫 does not compile another owner's action, a mismatched context, the bare database or an overlapping owner", () => {
112
+ const db = drizzle(new PGlite(), { schema });
113
+ const { record } = createIdentiteRuntime<HyyypertoolActivityContexts>({
114
+ app: "hyyypertool",
115
+ db,
116
+ }).as({ type: "moderator", sub: "moderator" });
117
+
118
+ void ((tx: IdentiteTransaction) =>
119
+ record(tx, {
120
+ // @ts-expect-error domain.verified belongs to the runtime, only markDomainAsVerified writes it
121
+ action: "domain.verified",
122
+ context: darkangels_refused.context,
123
+ }));
124
+
125
+ void ((tx: IdentiteTransaction) =>
126
+ record(tx, {
127
+ action: "domain.refused",
128
+ // @ts-expect-error the context does not match domain.refused
129
+ context: { inputs: { domain: "darkangels.world" }, outcome: {} },
130
+ }));
131
+
132
+ void (() =>
133
+ // @ts-expect-error the bare database is not a transaction
134
+ record(db, darkangels_refused));
135
+
136
+ type OverlappingContexts = {
137
+ "domain.verified": HyyypertoolActivityContexts["domain.refused"];
138
+ };
139
+ // @ts-expect-error domain.verified is already defined by the runtime
140
+ void createIdentiteRuntime<OverlappingContexts>({ app: "hyyypertool", db });
141
+
142
+ void ((tx: IdentiteTransaction) =>
143
+ createIdentiteRuntime({ app: "proconnect-identite", db })
144
+ .as({ type: "app", job: "join.official_contact" })
145
+ // @ts-expect-error proconnect-identite has no app-only action to record
146
+ .record(tx, darkangels_refused));
147
+ });
148
+ });
@@ -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";
package/tsconfig.json CHANGED
@@ -3,9 +3,12 @@
3
3
  "declaration": true,
4
4
  "declarationMap": true,
5
5
  "outDir": "./dist",
6
- "rootDir": "src",
7
- "types": ["node"]
6
+ "types": ["node"],
7
+ "paths": {
8
+ "#src/*": ["./src/*/index.ts"]
9
+ }
8
10
  },
9
11
  "extends": "@proconnect-gouv/proconnect.devtools.typescript/base/tsconfig.json",
10
- "references": []
12
+ "references": [],
13
+ "include": ["./src/**/*"]
11
14
  }
@@ -1,226 +0,0 @@
1
- //
2
-
3
- import { PGlite } from "@electric-sql/pglite";
4
- import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
5
- import { migrate } from "@proconnect-gouv/proconnect.identite.database/pglite/migrator";
6
- import { drizzle } from "drizzle-orm/pglite";
7
- import assert from "node:assert/strict";
8
- import { describe, it } from "node:test";
9
- import { createIdentiteRuntime } from "./index.js";
10
-
11
- //
12
-
13
- async function setup() {
14
- const pg = new PGlite();
15
- await migrate(pg);
16
-
17
- await pg.exec(`
18
- SET search_path TO public;
19
-
20
- INSERT INTO organizations
21
- (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
22
- VALUES
23
- (1, 'Dark Angels', 'Organization verifying darkangels.world', '🦁', '4444-04-04', '4444-04-04'),
24
- (2, 'Space Wolves', 'Other organization using darkangels.world', '🐺', '4444-04-04', '4444-04-04');
25
-
26
- INSERT INTO users
27
- (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
28
- VALUES
29
- (1, 'lion.eljonson+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', '0', 'Primarque'),
30
- (2, 'azrael+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Azrael', 'Supreme', '0', 'Grand Master'),
31
- (3, 'luther+kept-dirigeant@darkangels.world', '4444-04-04', '4444-04-04', 'Luther', 'Fallen', '0', 'Second'),
32
- (4, 'leman.russ+kept-other-domain@spacewolves.world', '4444-04-04', '4444-04-04', 'Leman', 'Russ', '0', 'Primarque');
33
-
34
- INSERT INTO users_organizations
35
- (user_id, organization_id, created_at, updated_at, is_external, verification_type)
36
- VALUES
37
- (1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_entreprise_unipersonnelle'),
38
- (2, 1, '4444-04-04', '4444-04-04', false, 'domain_not_verified_yet'),
39
- (3, 1, '4444-04-04', '4444-04-04', false, 'organization_dirigeant'),
40
- (4, 1, '4444-04-04', '4444-04-04', false, 'no_validation_means_available'),
41
- (1, 2, '4444-04-04', '4444-04-04', false, 'no_validation_means_available');
42
-
43
- INSERT INTO email_domains
44
- (id, organization_id, domain, verification_type, created_at, updated_at)
45
- VALUES
46
- (1, 1, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04'),
47
- (2, 1, 'darkangels.world', 'refused', '4444-04-04', '4444-04-04'),
48
- (3, 1, 'calibanites.world', 'verified', '4444-04-04', '4444-04-04'),
49
- (4, 2, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04');
50
- `);
51
-
52
- return {
53
- pg,
54
- runtime: createIdentiteRuntime({ db: drizzle(pg, { schema }) }),
55
- };
56
- }
57
-
58
- describe("markDomainAsVerified", () => {
59
- it("❎ returns organization_not_found for an unknown organization", async () => {
60
- const { pg, runtime } = await setup();
61
- const before = await snapshot(pg);
62
-
63
- const outcome = await runtime.markDomainAsVerified({
64
- domain: "notfound.world",
65
- domain_verification_type: "official_contact",
66
- organization_id: 404,
67
- });
68
-
69
- assert.deepEqual(outcome, {
70
- kind: "organization_not_found",
71
- organization_id: 404,
72
- });
73
- assert.deepEqual(await snapshot(pg), before);
74
- });
75
-
76
- it("✅ replaces the domain states and promotes the domain members", async () => {
77
- const { pg, runtime } = await setup();
78
-
79
- const outcome = await runtime.markDomainAsVerified({
80
- domain: "darkangels.world",
81
- domain_verification_type: "official_contact",
82
- organization_id: 1,
83
- });
84
-
85
- const {
86
- rows: [official_contact_domain],
87
- } = await pg.query<{ id: number }>(
88
- "SELECT id FROM email_domains WHERE verification_type = 'official_contact'",
89
- );
90
- assert.deepEqual(outcome, {
91
- kind: "domain_verified",
92
- email_domain: {
93
- id: official_contact_domain?.id,
94
- verification_type: "official_contact",
95
- },
96
- promoted_members: [
97
- {
98
- previous_verification_type:
99
- "no_verification_means_for_entreprise_unipersonnelle",
100
- user_id: 1,
101
- },
102
- { previous_verification_type: "domain_not_verified_yet", user_id: 2 },
103
- ],
104
- replaced_verification_types: ["not_verified_yet", "refused"],
105
- });
106
- assert.deepEqual(
107
- (
108
- await pg.query(`
109
- SELECT
110
- organization_id,
111
- domain,
112
- verification_type,
113
- can_be_suggested,
114
- verified_at IS NOT NULL AS is_verified,
115
- created_at = '4444-04-04' AS is_seeded
116
- FROM email_domains
117
- ORDER BY organization_id, domain, verification_type
118
- `)
119
- ).rows,
120
- [
121
- {
122
- can_be_suggested: true,
123
- domain: "calibanites.world",
124
- is_seeded: true,
125
- is_verified: false,
126
- organization_id: 1,
127
- verification_type: "verified",
128
- },
129
- {
130
- can_be_suggested: true,
131
- domain: "darkangels.world",
132
- is_seeded: false,
133
- is_verified: true,
134
- organization_id: 1,
135
- verification_type: "official_contact",
136
- },
137
- {
138
- can_be_suggested: true,
139
- domain: "darkangels.world",
140
- is_seeded: true,
141
- is_verified: false,
142
- organization_id: 2,
143
- verification_type: "not_verified_yet",
144
- },
145
- ],
146
- );
147
- assert.deepEqual(
148
- (
149
- await pg.query(`
150
- SELECT
151
- organization_id,
152
- user_id,
153
- verification_type,
154
- updated_at <> '4444-04-04' AS is_updated
155
- FROM users_organizations
156
- ORDER BY organization_id, user_id
157
- `)
158
- ).rows,
159
- [
160
- {
161
- is_updated: true,
162
- organization_id: 1,
163
- user_id: 1,
164
- verification_type: "domain",
165
- },
166
- {
167
- is_updated: true,
168
- organization_id: 1,
169
- user_id: 2,
170
- verification_type: "domain",
171
- },
172
- {
173
- is_updated: false,
174
- organization_id: 1,
175
- user_id: 3,
176
- verification_type: "organization_dirigeant",
177
- },
178
- {
179
- is_updated: false,
180
- organization_id: 1,
181
- user_id: 4,
182
- verification_type: "no_validation_means_available",
183
- },
184
- {
185
- is_updated: false,
186
- organization_id: 2,
187
- user_id: 1,
188
- verification_type: "no_validation_means_available",
189
- },
190
- ],
191
- );
192
- });
193
-
194
- it("❎ rolls back the domain state when a member update fails", async () => {
195
- const { pg, runtime } = await setup();
196
- await pg.exec(`
197
- ALTER TABLE users_organizations
198
- ADD CONSTRAINT users_organizations_never_domain
199
- CHECK (verification_type <> 'domain');
200
- `);
201
- const before = await snapshot(pg);
202
-
203
- await assert.rejects(
204
- runtime.markDomainAsVerified({
205
- domain: "darkangels.world",
206
- domain_verification_type: "official_contact",
207
- organization_id: 1,
208
- }),
209
- (error: Error) =>
210
- error.cause instanceof Error &&
211
- error.cause.message.includes("users_organizations_never_domain"),
212
- );
213
- assert.deepEqual(await snapshot(pg), before);
214
- });
215
- });
216
-
217
- async function snapshot(pg: PGlite) {
218
- const [{ rows: email_domains }, { rows: users_organizations }] =
219
- await Promise.all([
220
- pg.query("SELECT * FROM email_domains ORDER BY id"),
221
- pg.query(
222
- "SELECT * FROM users_organizations ORDER BY organization_id, user_id",
223
- ),
224
- ]);
225
- return { email_domains, users_organizations };
226
- }