@proconnect-gouv/proconnect.identite.runtime 0.3.1 → 0.4.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 (91) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/activity/activity-context-schemas.d.ts +75 -0
  3. package/dist/activity/activity-context-schemas.d.ts.map +1 -1
  4. package/dist/activity/activity-context-schemas.js +6 -0
  5. package/dist/activity/types.d.ts +3 -0
  6. package/dist/activity/types.d.ts.map +1 -1
  7. package/dist/franceconnect/activities.d.ts +16 -0
  8. package/dist/franceconnect/activities.d.ts.map +1 -0
  9. package/dist/franceconnect/activities.js +17 -0
  10. package/dist/franceconnect/connect-franceconnect.d.ts +20 -0
  11. package/dist/franceconnect/connect-franceconnect.d.ts.map +1 -0
  12. package/dist/franceconnect/connect-franceconnect.js +59 -0
  13. package/dist/franceconnect/disconnect-franceconnect.d.ts +14 -0
  14. package/dist/franceconnect/disconnect-franceconnect.d.ts.map +1 -0
  15. package/dist/franceconnect/disconnect-franceconnect.js +29 -0
  16. package/dist/franceconnect/index.d.ts +4 -0
  17. package/dist/franceconnect/index.d.ts.map +1 -0
  18. package/dist/franceconnect/index.js +4 -0
  19. package/dist/journal/insert-activity.js +6 -0
  20. package/dist/memberships/activities.d.ts +27 -0
  21. package/dist/memberships/activities.d.ts.map +1 -1
  22. package/dist/memberships/activities.js +27 -0
  23. package/dist/memberships/add-membership.d.ts +22 -0
  24. package/dist/memberships/add-membership.d.ts.map +1 -0
  25. package/dist/memberships/add-membership.js +46 -0
  26. package/dist/memberships/index.d.ts +3 -0
  27. package/dist/memberships/index.d.ts.map +1 -1
  28. package/dist/memberships/index.js +3 -0
  29. package/dist/memberships/remove-membership.d.ts +19 -0
  30. package/dist/memberships/remove-membership.d.ts.map +1 -0
  31. package/dist/memberships/remove-membership.js +29 -0
  32. package/dist/memberships/update-membership.d.ts +31 -0
  33. package/dist/memberships/update-membership.d.ts.map +1 -0
  34. package/dist/memberships/update-membership.js +61 -0
  35. package/dist/moderations/activities.d.ts +11 -0
  36. package/dist/moderations/activities.d.ts.map +1 -0
  37. package/dist/moderations/activities.js +12 -0
  38. package/dist/moderations/index.d.ts +3 -0
  39. package/dist/moderations/index.d.ts.map +1 -0
  40. package/dist/moderations/index.js +3 -0
  41. package/dist/moderations/open-moderation.d.ts +18 -0
  42. package/dist/moderations/open-moderation.d.ts.map +1 -0
  43. package/dist/moderations/open-moderation.js +31 -0
  44. package/dist/runtime/create-identite-runtime.d.ts +116 -0
  45. package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
  46. package/dist/runtime/create-identite-runtime.js +16 -0
  47. package/dist/users/activities.d.ts +33 -0
  48. package/dist/users/activities.d.ts.map +1 -0
  49. package/dist/users/activities.js +34 -0
  50. package/dist/users/delete-user.d.ts +14 -0
  51. package/dist/users/delete-user.d.ts.map +1 -0
  52. package/dist/users/delete-user.js +39 -0
  53. package/dist/users/index.d.ts +4 -0
  54. package/dist/users/index.d.ts.map +1 -0
  55. package/dist/users/index.js +4 -0
  56. package/dist/users/reset-mfa.d.ts +14 -0
  57. package/dist/users/reset-mfa.d.ts.map +1 -0
  58. package/dist/users/reset-mfa.js +53 -0
  59. package/package.json +4 -3
  60. package/src/activity/activity-context-schemas.ts +6 -0
  61. package/src/activity/types.ts +2 -1
  62. package/src/domains/mark-domain-as-verified.test.ts +162 -243
  63. package/src/franceconnect/activities.ts +20 -0
  64. package/src/franceconnect/connect-franceconnect.test.ts +197 -0
  65. package/src/franceconnect/connect-franceconnect.ts +96 -0
  66. package/src/franceconnect/disconnect-franceconnect.test.ts +94 -0
  67. package/src/franceconnect/disconnect-franceconnect.ts +53 -0
  68. package/src/franceconnect/index.ts +5 -0
  69. package/src/journal/insert-activity.ts +6 -0
  70. package/src/memberships/activities.ts +27 -0
  71. package/src/memberships/add-membership.test.ts +129 -0
  72. package/src/memberships/add-membership.ts +80 -0
  73. package/src/memberships/index.ts +3 -0
  74. package/src/memberships/remove-membership.test.ts +122 -0
  75. package/src/memberships/remove-membership.ts +59 -0
  76. package/src/memberships/update-membership.test.ts +202 -0
  77. package/src/memberships/update-membership.ts +106 -0
  78. package/src/moderations/activities.ts +15 -0
  79. package/src/moderations/index.ts +4 -0
  80. package/src/moderations/open-moderation.test.ts +99 -0
  81. package/src/moderations/open-moderation.ts +59 -0
  82. package/src/runtime/create-identite-runtime.test.ts +20 -51
  83. package/src/runtime/create-identite-runtime.ts +23 -0
  84. package/src/users/activities.ts +39 -0
  85. package/src/users/delete-user.test.ts +142 -0
  86. package/src/users/delete-user.ts +60 -0
  87. package/src/users/index.ts +5 -0
  88. package/src/users/reset-mfa.test.ts +128 -0
  89. package/src/users/reset-mfa.ts +79 -0
  90. package/testing/index.ts +25 -0
  91. package/tsconfig.json +1 -1
@@ -1,3 +1,6 @@
1
1
  //
2
2
 
3
3
  export * from "./activities.js";
4
+ export * from "./add-membership.js";
5
+ export * from "./remove-membership.js";
6
+ export * from "./update-membership.js";
@@ -0,0 +1,122 @@
1
+ //
2
+
3
+ import type { IdentiteDatabase } from "#src/database";
4
+ import { createIdentiteRuntime } from "#src/runtime";
5
+ import { createTestDatabase, readActivityLogs } from "#testing";
6
+ import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
7
+ import assert from "node:assert/strict";
8
+ import { describe, it } from "node:test";
9
+
10
+ //
11
+
12
+ async function setup() {
13
+ const db = await createTestDatabase();
14
+ await db.insert(schema.organizations).values([
15
+ { id: 1, siret: "🦁" },
16
+ { id: 2, siret: "🐺" },
17
+ ]);
18
+ await db.insert(schema.users).values([
19
+ {
20
+ created_at: "4444-04-04",
21
+ email: "lion.eljonson@darkangels.world",
22
+ id: 1,
23
+ updated_at: "4444-04-04",
24
+ },
25
+ {
26
+ created_at: "4444-04-04",
27
+ email: "azrael@darkangels.world",
28
+ id: 2,
29
+ updated_at: "4444-04-04",
30
+ },
31
+ ]);
32
+ await db.insert(schema.users_organizations).values([
33
+ {
34
+ is_external: true,
35
+ organization_id: 1,
36
+ user_id: 1,
37
+ verification_type: "domain_not_verified_yet",
38
+ },
39
+ { organization_id: 1, user_id: 2, verification_type: "domain" },
40
+ { organization_id: 2, user_id: 1, verification_type: "domain" },
41
+ ]);
42
+
43
+ return {
44
+ db,
45
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
46
+ type: "user",
47
+ user_id: 1,
48
+ }),
49
+ };
50
+ }
51
+
52
+ describe("removeMembership", () => {
53
+ it("✅ removes the membership and journals it under the user", async () => {
54
+ const { db, runtime } = await setup();
55
+
56
+ const outcome = await runtime.removeMembership({
57
+ organization_id: 1,
58
+ user_id: 1,
59
+ });
60
+
61
+ assert.deepEqual(outcome, {
62
+ kind: "member_removed",
63
+ activity_id: 1,
64
+ previous: {
65
+ is_external: true,
66
+ verification_type: "domain_not_verified_yet",
67
+ },
68
+ });
69
+ assert.deepEqual(await memberships(db), [
70
+ { organization_id: 1, user_id: 2 },
71
+ { organization_id: 2, user_id: 1 },
72
+ ]);
73
+ assert.deepEqual(await readActivityLogs(db), [
74
+ {
75
+ action: "member.removed",
76
+ actor_ref: "1",
77
+ actor_type: "user",
78
+ app: "proconnect-identite",
79
+ cause_activity_id: null,
80
+ context: {
81
+ inputs: {},
82
+ outcome: {
83
+ is_external: true,
84
+ verification_type: "domain_not_verified_yet",
85
+ },
86
+ },
87
+ interaction_cid: null,
88
+ moderation_id: null,
89
+ organization_id: 1,
90
+ user_id: 1,
91
+ },
92
+ ]);
93
+ });
94
+
95
+ it("❎ returns membership_not_found and journals nothing for a missing link", async () => {
96
+ const { db, runtime } = await setup();
97
+ const before = await memberships(db);
98
+
99
+ const outcome = await runtime.removeMembership({
100
+ organization_id: 1,
101
+ user_id: 999,
102
+ });
103
+
104
+ assert.deepEqual(outcome, {
105
+ kind: "membership_not_found",
106
+ organization_id: 1,
107
+ user_id: 999,
108
+ });
109
+ assert.deepEqual(await memberships(db), before);
110
+ assert.deepEqual(await readActivityLogs(db), []);
111
+ });
112
+ });
113
+
114
+ function memberships(db: IdentiteDatabase) {
115
+ return db.query.users_organizations.findMany({
116
+ columns: { organization_id: true, user_id: true },
117
+ orderBy: (table, { asc }) => [
118
+ asc(table.organization_id),
119
+ asc(table.user_id),
120
+ ],
121
+ });
122
+ }
@@ -0,0 +1,59 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users_organizations } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import { and, eq } from "drizzle-orm";
7
+
8
+ //
9
+
10
+ type RemoveMembershipOutcome =
11
+ | {
12
+ kind: "member_removed";
13
+ activity_id: number;
14
+ previous: {
15
+ is_external: boolean;
16
+ verification_type: string;
17
+ };
18
+ }
19
+ | { kind: "membership_not_found"; organization_id: number; user_id: number };
20
+
21
+ export function removeMembershipFactory({ actor, app, db }: RuntimeContext) {
22
+ return function removeMembership({
23
+ organization_id,
24
+ user_id,
25
+ }: {
26
+ organization_id: number;
27
+ user_id: number;
28
+ }): Promise<RemoveMembershipOutcome> {
29
+ return db.transaction(async (tx) => {
30
+ const [previous] = await tx
31
+ .delete(users_organizations)
32
+ .where(
33
+ and(
34
+ eq(users_organizations.organization_id, organization_id),
35
+ eq(users_organizations.user_id, user_id),
36
+ ),
37
+ )
38
+ .returning({
39
+ is_external: users_organizations.is_external,
40
+ verification_type: users_organizations.verification_type,
41
+ });
42
+
43
+ if (!previous) {
44
+ return { kind: "membership_not_found", organization_id, user_id };
45
+ }
46
+
47
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
48
+ action: "member.removed",
49
+ actor,
50
+ app,
51
+ context: { inputs: {}, outcome: previous },
52
+ organization_id,
53
+ user_id,
54
+ });
55
+
56
+ return { kind: "member_removed", activity_id, previous };
57
+ });
58
+ };
59
+ }
@@ -0,0 +1,202 @@
1
+ //
2
+
3
+ import type { IdentiteDatabase } from "#src/database";
4
+ import { createIdentiteRuntime } from "#src/runtime";
5
+ import { createTestDatabase, readActivityLogs } from "#testing";
6
+ import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
7
+ import assert from "node:assert/strict";
8
+ import { describe, it } from "node:test";
9
+
10
+ //
11
+
12
+ async function setup() {
13
+ const db = await createTestDatabase();
14
+ await db.insert(schema.organizations).values([{ id: 1, siret: "🦁" }]);
15
+ await db.insert(schema.users).values([
16
+ {
17
+ created_at: "4444-04-04",
18
+ email: "lion.eljonson@darkangels.world",
19
+ id: 1,
20
+ updated_at: "4444-04-04",
21
+ },
22
+ {
23
+ created_at: "4444-04-04",
24
+ email: "azrael@darkangels.world",
25
+ id: 2,
26
+ updated_at: "4444-04-04",
27
+ },
28
+ {
29
+ created_at: "4444-04-04",
30
+ email: "sanguinius@darkangels.world",
31
+ id: 3,
32
+ updated_at: "4444-04-04",
33
+ },
34
+ ]);
35
+ await db.insert(schema.users_organizations).values([
36
+ {
37
+ is_external: true,
38
+ organization_id: 1,
39
+ user_id: 1,
40
+ verification_type: "domain_not_verified_yet",
41
+ },
42
+ {
43
+ organization_id: 1,
44
+ user_id: 2,
45
+ verification_type: "domain",
46
+ verified_at: "4444-04-04",
47
+ },
48
+ {
49
+ organization_id: 1,
50
+ user_id: 3,
51
+ verification_type: "organization_dirigeant",
52
+ verified_at: "4444-04-04",
53
+ },
54
+ ]);
55
+
56
+ return {
57
+ db,
58
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
59
+ type: "user",
60
+ user_id: 1,
61
+ }),
62
+ };
63
+ }
64
+
65
+ describe("updateMembership", () => {
66
+ it("✅ updates the membership, verifies it and journals it under the user", async () => {
67
+ const { db, runtime } = await setup();
68
+
69
+ const outcome = await runtime.updateMembership({
70
+ organization_id: 1,
71
+ user_id: 1,
72
+ verification_type: "organization_dirigeant",
73
+ });
74
+
75
+ assert.deepEqual(outcome, {
76
+ kind: "member_updated",
77
+ activity_id: 1,
78
+ previous: {
79
+ is_external: true,
80
+ verification_type: "domain_not_verified_yet",
81
+ },
82
+ });
83
+ const [membership] = await memberships(db);
84
+ assert.equal(membership?.is_external, true);
85
+ assert.equal(membership?.verification_type, "organization_dirigeant");
86
+ assert.notEqual(membership?.verified_at, null);
87
+ assert.deepEqual(await readActivityLogs(db), [
88
+ {
89
+ action: "member.updated",
90
+ actor_ref: "1",
91
+ actor_type: "user",
92
+ app: "proconnect-identite",
93
+ cause_activity_id: null,
94
+ context: {
95
+ inputs: { verification_type: "organization_dirigeant" },
96
+ outcome: {
97
+ previous: {
98
+ is_external: true,
99
+ verification_type: "domain_not_verified_yet",
100
+ },
101
+ },
102
+ },
103
+ interaction_cid: null,
104
+ moderation_id: null,
105
+ organization_id: 1,
106
+ user_id: 1,
107
+ },
108
+ ]);
109
+ });
110
+
111
+ it("✅ renews verified_at when the verification type stays the same", async () => {
112
+ const { db, runtime } = await setup();
113
+
114
+ const outcome = await runtime.updateMembership({
115
+ organization_id: 1,
116
+ renew_verification: true,
117
+ user_id: 3,
118
+ verification_type: "organization_dirigeant",
119
+ });
120
+
121
+ assert.equal(outcome.kind, "member_updated");
122
+ const [, , membership] = await memberships(db);
123
+ assert.equal(membership?.verification_type, "organization_dirigeant");
124
+ assert.notEqual(membership?.verified_at, "4444-04-04 00:00:00+00");
125
+ const [activity] = await readActivityLogs(db);
126
+ assert.deepEqual(activity?.context, {
127
+ inputs: {
128
+ renew_verification: true,
129
+ verification_type: "organization_dirigeant",
130
+ },
131
+ outcome: {
132
+ previous: {
133
+ is_external: false,
134
+ verification_type: "organization_dirigeant",
135
+ },
136
+ },
137
+ });
138
+ });
139
+
140
+ it("✅ clears verified_at when the membership goes back to domain_not_verified_yet", async () => {
141
+ const { db, runtime } = await setup();
142
+
143
+ await runtime.updateMembership({
144
+ organization_id: 1,
145
+ user_id: 2,
146
+ verification_type: "domain_not_verified_yet",
147
+ });
148
+
149
+ const [, membership] = await memberships(db);
150
+ assert.equal(membership?.verification_type, "domain_not_verified_yet");
151
+ assert.equal(membership?.verified_at, null);
152
+ });
153
+
154
+ it("❎ returns membership_unchanged and journals nothing when nothing changes", async () => {
155
+ const { db, runtime } = await setup();
156
+ const before = await memberships(db);
157
+
158
+ const outcome = await runtime.updateMembership({
159
+ is_external: true,
160
+ organization_id: 1,
161
+ user_id: 1,
162
+ verification_type: "domain_not_verified_yet",
163
+ });
164
+
165
+ assert.deepEqual(outcome, {
166
+ kind: "membership_unchanged",
167
+ organization_id: 1,
168
+ user_id: 1,
169
+ });
170
+ assert.deepEqual(await memberships(db), before);
171
+ assert.deepEqual(await readActivityLogs(db), []);
172
+ });
173
+
174
+ it("❎ returns membership_not_found and journals nothing for a missing link", async () => {
175
+ const { db, runtime } = await setup();
176
+
177
+ const outcome = await runtime.updateMembership({
178
+ is_external: false,
179
+ organization_id: 1,
180
+ user_id: 999,
181
+ });
182
+
183
+ assert.deepEqual(outcome, {
184
+ kind: "membership_not_found",
185
+ organization_id: 1,
186
+ user_id: 999,
187
+ });
188
+ assert.deepEqual(await readActivityLogs(db), []);
189
+ });
190
+ });
191
+
192
+ function memberships(db: IdentiteDatabase) {
193
+ return db.query.users_organizations.findMany({
194
+ columns: {
195
+ is_external: true,
196
+ user_id: true,
197
+ verification_type: true,
198
+ verified_at: true,
199
+ },
200
+ orderBy: (table, { asc }) => [asc(table.user_id)],
201
+ });
202
+ }
@@ -0,0 +1,106 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users_organizations } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import {
7
+ SuperWeakLinkEnum,
8
+ type LinkType,
9
+ } from "@proconnect-gouv/proconnect.identite/types";
10
+ import { and, eq, sql } from "drizzle-orm";
11
+
12
+ //
13
+
14
+ type UpdateMembershipOutcome =
15
+ | {
16
+ kind: "member_updated";
17
+ activity_id: number;
18
+ previous: {
19
+ is_external: boolean;
20
+ verification_type: string;
21
+ };
22
+ }
23
+ | { kind: "membership_not_found"; organization_id: number; user_id: number }
24
+ | { kind: "membership_unchanged"; organization_id: number; user_id: number };
25
+
26
+ export function updateMembershipFactory({ actor, app, db }: RuntimeContext) {
27
+ return function updateMembership({
28
+ is_external,
29
+ organization_id,
30
+ renew_verification,
31
+ user_id,
32
+ verification_type,
33
+ }: {
34
+ is_external?: boolean;
35
+ organization_id: number;
36
+ user_id: number;
37
+ } & (
38
+ | { renew_verification: true; verification_type: LinkType }
39
+ | { renew_verification?: false; verification_type?: LinkType }
40
+ )): Promise<UpdateMembershipOutcome> {
41
+ return db.transaction(async (tx) => {
42
+ const link = and(
43
+ eq(users_organizations.organization_id, organization_id),
44
+ eq(users_organizations.user_id, user_id),
45
+ );
46
+ const [previous] = await tx
47
+ .select({
48
+ is_external: users_organizations.is_external,
49
+ verification_type: users_organizations.verification_type,
50
+ })
51
+ .from(users_organizations)
52
+ .where(link)
53
+ .for("update");
54
+
55
+ if (!previous) {
56
+ return { kind: "membership_not_found", organization_id, user_id };
57
+ }
58
+
59
+ if (
60
+ renew_verification !== true &&
61
+ (is_external ?? previous.is_external) === previous.is_external &&
62
+ (verification_type ?? previous.verification_type) ===
63
+ previous.verification_type
64
+ ) {
65
+ return { kind: "membership_unchanged", organization_id, user_id };
66
+ }
67
+
68
+ const fields = {
69
+ ...(is_external === undefined ? {} : { is_external }),
70
+ ...(verification_type === undefined ? {} : { verification_type }),
71
+ };
72
+
73
+ const inputs = {
74
+ ...fields,
75
+ ...(renew_verification === true ? { renew_verification: true } : {}),
76
+ };
77
+
78
+ await tx
79
+ .update(users_organizations)
80
+ .set({
81
+ ...fields,
82
+ ...(verification_type === undefined
83
+ ? {}
84
+ : {
85
+ verified_at: SuperWeakLinkEnum.safeParse(verification_type)
86
+ .success
87
+ ? null
88
+ : sql`now()`,
89
+ }),
90
+ updated_at: sql`now()`,
91
+ })
92
+ .where(link);
93
+
94
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
95
+ action: "member.updated",
96
+ actor,
97
+ app,
98
+ context: { inputs, outcome: { previous } },
99
+ organization_id,
100
+ user_id,
101
+ });
102
+
103
+ return { kind: "member_updated", activity_id, previous };
104
+ });
105
+ };
106
+ }
@@ -0,0 +1,15 @@
1
+ //
2
+
3
+ import * as z from "zod";
4
+
5
+ //
6
+
7
+ export const moderationActivities = {
8
+ "moderation.opened": z.object({
9
+ inputs: z.object({
10
+ sp_name: z.string().nullable(),
11
+ type: z.string(),
12
+ }),
13
+ outcome: z.object({}),
14
+ }),
15
+ };
@@ -0,0 +1,4 @@
1
+ //
2
+
3
+ export * from "./activities.js";
4
+ export * from "./open-moderation.js";
@@ -0,0 +1,99 @@
1
+ //
2
+
3
+ import { createIdentiteRuntime } from "#src/runtime";
4
+ import { createTestDatabase, readActivityLogs } from "#testing";
5
+ import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import assert from "node:assert/strict";
7
+ import { describe, it } from "node:test";
8
+
9
+ //
10
+
11
+ async function setup() {
12
+ const db = await createTestDatabase();
13
+
14
+ await db.insert(schema.users).values({
15
+ created_at: "4444-04-04",
16
+ email: "jean.valjean@republic.fr",
17
+ id: 1,
18
+ updated_at: "4444-04-04",
19
+ });
20
+ await db.insert(schema.organizations).values({
21
+ created_at: "4444-04-04",
22
+ id: 1,
23
+ siret: "11111111111111",
24
+ updated_at: "4444-04-04",
25
+ });
26
+
27
+ return {
28
+ db,
29
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
30
+ type: "user",
31
+ user_id: 1,
32
+ }),
33
+ };
34
+ }
35
+
36
+ describe("openModeration", () => {
37
+ it("✅ opens a pending moderation and journals it under the user", async () => {
38
+ const { db, runtime } = await setup();
39
+
40
+ const outcome = await runtime.openModeration({
41
+ organization_id: 1,
42
+ sp_name: "Lasagnes",
43
+ ticket_id: "session_42",
44
+ type: "organization_join_block",
45
+ user_id: 1,
46
+ });
47
+
48
+ assert.deepEqual(outcome, {
49
+ kind: "moderation_opened",
50
+ activity_id: 1,
51
+ moderation: { id: 1 },
52
+ });
53
+ assert.deepEqual(
54
+ await db.query.moderations.findMany({
55
+ columns: {
56
+ id: true,
57
+ moderated_at: true,
58
+ moderated_by: true,
59
+ organization_id: true,
60
+ sp_name: true,
61
+ status: true,
62
+ ticket_id: true,
63
+ type: true,
64
+ user_id: true,
65
+ },
66
+ }),
67
+ [
68
+ {
69
+ id: 1,
70
+ moderated_at: null,
71
+ moderated_by: null,
72
+ organization_id: 1,
73
+ sp_name: "Lasagnes",
74
+ status: "pending",
75
+ ticket_id: "session_42",
76
+ type: "organization_join_block",
77
+ user_id: 1,
78
+ },
79
+ ],
80
+ );
81
+ assert.deepEqual(await readActivityLogs(db), [
82
+ {
83
+ action: "moderation.opened",
84
+ actor_ref: "1",
85
+ actor_type: "user",
86
+ app: "proconnect-identite",
87
+ cause_activity_id: null,
88
+ context: {
89
+ inputs: { sp_name: "Lasagnes", type: "organization_join_block" },
90
+ outcome: {},
91
+ },
92
+ interaction_cid: null,
93
+ moderation_id: 1,
94
+ organization_id: 1,
95
+ user_id: 1,
96
+ },
97
+ ]);
98
+ });
99
+ });
@@ -0,0 +1,59 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { moderations } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import {
7
+ ModerationStatusSchema,
8
+ type ModerationType,
9
+ } from "@proconnect-gouv/proconnect.identite/types";
10
+
11
+ //
12
+
13
+ type OpenModerationOutcome = {
14
+ kind: "moderation_opened";
15
+ activity_id: number;
16
+ moderation: { id: number };
17
+ };
18
+
19
+ export function openModerationFactory({ actor, app, db }: RuntimeContext) {
20
+ return function openModeration({
21
+ organization_id,
22
+ sp_name = null,
23
+ ticket_id,
24
+ type,
25
+ user_id,
26
+ }: {
27
+ organization_id: number;
28
+ sp_name?: string | null;
29
+ ticket_id: string | null;
30
+ type: ModerationType;
31
+ user_id: number;
32
+ }): Promise<OpenModerationOutcome> {
33
+ return db.transaction(async (tx) => {
34
+ const [moderation] = await tx
35
+ .insert(moderations)
36
+ .values({
37
+ organization_id,
38
+ sp_name,
39
+ status: ModerationStatusSchema.enum.pending,
40
+ ticket_id,
41
+ type,
42
+ user_id,
43
+ })
44
+ .returning({ id: moderations.id });
45
+
46
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
47
+ action: "moderation.opened",
48
+ actor,
49
+ app,
50
+ context: { inputs: { sp_name, type }, outcome: {} },
51
+ moderation_id: moderation.id,
52
+ organization_id,
53
+ user_id,
54
+ });
55
+
56
+ return { kind: "moderation_opened", activity_id, moderation };
57
+ });
58
+ };
59
+ }