@proconnect-gouv/proconnect.identite.runtime 0.3.1 → 0.5.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 (101) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/activity/activity-context-schemas.d.ts +104 -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 +152 -0
  45. package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
  46. package/dist/runtime/create-identite-runtime.js +22 -0
  47. package/dist/users/activities.d.ts +62 -0
  48. package/dist/users/activities.d.ts.map +1 -0
  49. package/dist/users/activities.js +54 -0
  50. package/dist/users/change-password.d.ts +16 -0
  51. package/dist/users/change-password.d.ts.map +1 -0
  52. package/dist/users/change-password.js +38 -0
  53. package/dist/users/delete-user.d.ts +14 -0
  54. package/dist/users/delete-user.d.ts.map +1 -0
  55. package/dist/users/delete-user.js +39 -0
  56. package/dist/users/index.d.ts +6 -0
  57. package/dist/users/index.d.ts.map +1 -0
  58. package/dist/users/index.js +6 -0
  59. package/dist/users/reset-mfa.d.ts +14 -0
  60. package/dist/users/reset-mfa.d.ts.map +1 -0
  61. package/dist/users/reset-mfa.js +53 -0
  62. package/dist/users/update-personal-informations.d.ts +21 -0
  63. package/dist/users/update-personal-informations.d.ts.map +1 -0
  64. package/dist/users/update-personal-informations.js +53 -0
  65. package/package.json +5 -4
  66. package/src/activity/activity-context-schemas.ts +6 -0
  67. package/src/activity/types.ts +2 -1
  68. package/src/domains/mark-domain-as-verified.test.ts +162 -243
  69. package/src/franceconnect/activities.ts +20 -0
  70. package/src/franceconnect/connect-franceconnect.test.ts +197 -0
  71. package/src/franceconnect/connect-franceconnect.ts +96 -0
  72. package/src/franceconnect/disconnect-franceconnect.test.ts +94 -0
  73. package/src/franceconnect/disconnect-franceconnect.ts +53 -0
  74. package/src/franceconnect/index.ts +5 -0
  75. package/src/journal/insert-activity.ts +6 -0
  76. package/src/memberships/activities.ts +27 -0
  77. package/src/memberships/add-membership.test.ts +129 -0
  78. package/src/memberships/add-membership.ts +80 -0
  79. package/src/memberships/index.ts +3 -0
  80. package/src/memberships/remove-membership.test.ts +122 -0
  81. package/src/memberships/remove-membership.ts +59 -0
  82. package/src/memberships/update-membership.test.ts +202 -0
  83. package/src/memberships/update-membership.ts +106 -0
  84. package/src/moderations/activities.ts +15 -0
  85. package/src/moderations/index.ts +4 -0
  86. package/src/moderations/open-moderation.test.ts +99 -0
  87. package/src/moderations/open-moderation.ts +59 -0
  88. package/src/runtime/create-identite-runtime.test.ts +20 -51
  89. package/src/runtime/create-identite-runtime.ts +34 -0
  90. package/src/users/activities.ts +60 -0
  91. package/src/users/change-password.test.ts +91 -0
  92. package/src/users/change-password.ts +64 -0
  93. package/src/users/delete-user.test.ts +142 -0
  94. package/src/users/delete-user.ts +60 -0
  95. package/src/users/index.ts +7 -0
  96. package/src/users/reset-mfa.test.ts +128 -0
  97. package/src/users/reset-mfa.ts +79 -0
  98. package/src/users/update-personal-informations.test.ts +113 -0
  99. package/src/users/update-personal-informations.ts +97 -0
  100. package/testing/index.ts +25 -0
  101. package/tsconfig.json +1 -1
@@ -3,13 +3,9 @@
3
3
  import type { ContextsOf } from "#src/activity";
4
4
  import type { IdentiteTransaction } from "#src/database";
5
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";
6
+ import { createTestDatabase, readActivityLogs } from "#testing";
11
7
  import assert from "node:assert/strict";
12
- import { before, describe, it, mock } from "node:test";
8
+ import { describe, it } from "node:test";
13
9
  import * as z from "zod";
14
10
 
15
11
  //
@@ -45,10 +41,7 @@ const darkangels_refused = {
45
41
  };
46
42
 
47
43
  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 });
44
+ const db = await createTestDatabase();
52
45
 
53
46
  return {
54
47
  db,
@@ -56,60 +49,36 @@ async function setup() {
56
49
  app: "hyyypertool",
57
50
  db,
58
51
  }).as({ type: "moderator", sub: "moderator+refuses-darkangels.world" }),
59
- pg,
60
52
  };
61
53
  }
62
54
 
63
55
  describe("record", () => {
64
- before(() => {
65
- mock.timers.enable({ apis: ["Date"], now: new Date("5555-05-05") });
66
- });
67
-
68
56
  it("✅ journals the caller's own action under its actor, in the caller's transaction", async () => {
69
- const { db, moderator, pg } = await setup();
57
+ const { db, moderator } = await setup();
70
58
 
71
59
  const outcome = await db.transaction((tx) =>
72
60
  moderator.record(tx, darkangels_refused),
73
61
  );
74
62
 
75
63
  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, []);
64
+ assert.deepEqual(await readActivityLogs(db), [
65
+ {
66
+ action: "domain.refused",
67
+ actor_ref: "moderator+refuses-darkangels.world",
68
+ actor_type: "moderator",
69
+ app: "hyyypertool",
70
+ cause_activity_id: null,
71
+ context: darkangels_refused.context,
72
+ interaction_cid: null,
73
+ moderation_id: null,
74
+ organization_id: 1,
75
+ user_id: null,
76
+ },
77
+ ]);
109
78
  });
110
79
 
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 });
80
+ it("🚫 does not compile another owner's action, a mismatched context, the bare database or an overlapping owner", async () => {
81
+ const { db } = await setup();
113
82
  const { record } = createIdentiteRuntime<HyyypertoolActivityContexts>({
114
83
  app: "hyyypertool",
115
84
  db,
@@ -10,7 +10,23 @@ import type {
10
10
  } from "#src/activity";
11
11
  import type { IdentiteTransaction } from "#src/database";
12
12
  import { markDomainAsVerifiedFactory } from "#src/domains";
13
+ import {
14
+ connectFranceConnectFactory,
15
+ disconnectFranceConnectFactory,
16
+ } from "#src/franceconnect";
13
17
  import { insertActivity } from "#src/journal";
18
+ import {
19
+ addMembershipFactory,
20
+ removeMembershipFactory,
21
+ updateMembershipFactory,
22
+ } from "#src/memberships";
23
+ import { openModerationFactory } from "#src/moderations";
24
+ import {
25
+ changePasswordFactory,
26
+ deleteUserFactory,
27
+ resetMfaFactory,
28
+ updatePersonalInformationsFactory,
29
+ } from "#src/users";
14
30
 
15
31
  //
16
32
 
@@ -22,11 +38,29 @@ export function createIdentiteRuntime<
22
38
  return {
23
39
  as(actor: Actor) {
24
40
  return {
41
+ addMembership: addMembershipFactory({ actor, app, db }),
42
+ changePassword: changePasswordFactory({ actor, app, db }),
43
+ connectFranceConnect: connectFranceConnectFactory({ actor, app, db }),
44
+ deleteUser: deleteUserFactory({ actor, app, db }),
45
+ disconnectFranceConnect: disconnectFranceConnectFactory({
46
+ actor,
47
+ app,
48
+ db,
49
+ }),
25
50
  markDomainAsVerified: markDomainAsVerifiedFactory({ actor, app, db }),
51
+ openModeration: openModerationFactory({ actor, app, db }),
52
+ removeMembership: removeMembershipFactory({ actor, app, db }),
26
53
  record: (
27
54
  tx: IdentiteTransaction,
28
55
  activity: ActivityOf<OwnContexts> & ActivityTargets,
29
56
  ) => insertActivity<OwnContexts>(tx, { ...activity, actor, app }),
57
+ resetMfa: resetMfaFactory({ actor, app, db }),
58
+ updateMembership: updateMembershipFactory({ actor, app, db }),
59
+ updatePersonalInformations: updatePersonalInformationsFactory({
60
+ actor,
61
+ app,
62
+ db,
63
+ }),
30
64
  };
31
65
  },
32
66
  };
@@ -0,0 +1,60 @@
1
+ //
2
+
3
+ import * as z from "zod";
4
+
5
+ //
6
+
7
+ const userInformation = z.object({
8
+ family_name: z.string().nullable(),
9
+ given_name: z.string().nullable(),
10
+ job: z.string().nullable(),
11
+ phone_number: z.string().nullable(),
12
+ });
13
+
14
+ export const userActivities = {
15
+ "user.deleted": z.object({
16
+ inputs: z.object({
17
+ user_id: z.number(),
18
+ }),
19
+ outcome: z.object({
20
+ email: z.string(),
21
+ family_name: z.string().nullable(),
22
+ given_name: z.string().nullable(),
23
+ memberships: z.array(
24
+ z.object({
25
+ is_external: z.boolean(),
26
+ organization_id: z.number(),
27
+ verification_type: z.string(),
28
+ }),
29
+ ),
30
+ }),
31
+ }),
32
+ "user.info_changed": z.object({
33
+ inputs: userInformation,
34
+ outcome: z.object({
35
+ previous: userInformation,
36
+ }),
37
+ }),
38
+ "user.mfa_reset": z.object({
39
+ inputs: z.object({
40
+ user_id: z.number(),
41
+ }),
42
+ outcome: z.object({
43
+ previous: z.object({
44
+ authenticators: z.number(),
45
+ email_verified: z.boolean(),
46
+ force_2fa: z.boolean(),
47
+ password_configured: z.boolean(),
48
+ totp_configured: z.boolean(),
49
+ }),
50
+ }),
51
+ }),
52
+ "user.password_changed": z.object({
53
+ inputs: z.object({
54
+ source: z.enum(["reset_password", "signup"]),
55
+ }),
56
+ outcome: z.object({
57
+ previous: z.object({ email_verified: z.boolean() }),
58
+ }),
59
+ }),
60
+ };
@@ -0,0 +1,91 @@
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
+ await db.insert(schema.users).values({
14
+ created_at: "4444-04-04",
15
+ email: "jean.valjean@republic.fr",
16
+ id: 1,
17
+ reset_password_token: "token",
18
+ updated_at: "4444-04-04",
19
+ });
20
+
21
+ return {
22
+ db,
23
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
24
+ type: "user",
25
+ user_id: 1,
26
+ }),
27
+ };
28
+ }
29
+
30
+ describe("changePassword", () => {
31
+ it("✅ resets the password, verifies the email and clears the token", async () => {
32
+ const { db, runtime } = await setup();
33
+
34
+ const outcome = await runtime.changePassword({
35
+ encrypted_password: "hashed",
36
+ source: "reset_password",
37
+ user_id: 1,
38
+ });
39
+
40
+ assert.deepEqual(outcome, {
41
+ kind: "password_changed",
42
+ activity_id: 1,
43
+ previous: { email_verified: false },
44
+ });
45
+
46
+ const [user] = await db.query.users.findMany();
47
+ assert.equal(user.encrypted_password, "hashed");
48
+ assert.equal(user.email_verified, true);
49
+ assert.equal(user.reset_password_token, null);
50
+
51
+ assert.deepEqual(await readActivityLogs(db), [
52
+ {
53
+ action: "user.password_changed",
54
+ actor_ref: "1",
55
+ actor_type: "user",
56
+ app: "proconnect-identite",
57
+ cause_activity_id: null,
58
+ context: {
59
+ inputs: { source: "reset_password" },
60
+ outcome: {
61
+ previous: { email_verified: false },
62
+ },
63
+ },
64
+ interaction_cid: null,
65
+ moderation_id: null,
66
+ organization_id: null,
67
+ user_id: 1,
68
+ },
69
+ ]);
70
+ });
71
+
72
+ it("✅ keeps the email unverified when setting a first password at signup", async () => {
73
+ const { db, runtime } = await setup();
74
+
75
+ const outcome = await runtime.changePassword({
76
+ encrypted_password: "hashed",
77
+ source: "signup",
78
+ user_id: 1,
79
+ });
80
+
81
+ assert.deepEqual(outcome, {
82
+ kind: "password_changed",
83
+ activity_id: 1,
84
+ previous: { email_verified: false },
85
+ });
86
+
87
+ const [user] = await db.query.users.findMany();
88
+ assert.equal(user.email_verified, false);
89
+ assert.equal(user.email_verified_at, null);
90
+ });
91
+ });
@@ -0,0 +1,64 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import { UserNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
7
+ import { eq, sql } from "drizzle-orm";
8
+
9
+ //
10
+
11
+ type PasswordSource = "reset_password" | "signup";
12
+
13
+ type ChangePasswordOutcome = {
14
+ kind: "password_changed";
15
+ activity_id: number;
16
+ previous: { email_verified: boolean };
17
+ };
18
+
19
+ export function changePasswordFactory({ actor, app, db }: RuntimeContext) {
20
+ return function changePassword({
21
+ encrypted_password,
22
+ source,
23
+ user_id,
24
+ }: {
25
+ encrypted_password: string;
26
+ source: PasswordSource;
27
+ user_id: number;
28
+ }): Promise<ChangePasswordOutcome> {
29
+ return db.transaction(async (tx) => {
30
+ const [previous] = await tx
31
+ .select({ email_verified: users.email_verified })
32
+ .from(users)
33
+ .where(eq(users.id, user_id))
34
+ .for("update");
35
+
36
+ if (!previous) {
37
+ throw new UserNotFoundError(`user ${user_id} not found`);
38
+ }
39
+
40
+ await tx
41
+ .update(users)
42
+ .set({
43
+ email_verified: source === "reset_password",
44
+ email_verified_at: source === "reset_password" ? sql`now()` : null,
45
+ encrypted_password,
46
+ reset_password_sent_at:
47
+ source === "reset_password" ? null : undefined,
48
+ reset_password_token: source === "reset_password" ? null : undefined,
49
+ updated_at: sql`now()`,
50
+ })
51
+ .where(eq(users.id, user_id));
52
+
53
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
54
+ action: "user.password_changed",
55
+ actor,
56
+ app,
57
+ context: { inputs: { source }, outcome: { previous } },
58
+ user_id,
59
+ });
60
+
61
+ return { kind: "password_changed", activity_id, previous };
62
+ });
63
+ };
64
+ }
@@ -0,0 +1,142 @@
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
+
15
+ await db.insert(schema.users).values([
16
+ {
17
+ created_at: "4444-04-04",
18
+ email: "jean.valjean@republic.fr",
19
+ family_name: "Valjean",
20
+ given_name: "Jean",
21
+ id: 1,
22
+ updated_at: "4444-04-04",
23
+ },
24
+ {
25
+ created_at: "4444-04-04",
26
+ email: "cosette@republic.fr",
27
+ id: 2,
28
+ updated_at: "4444-04-04",
29
+ },
30
+ ]);
31
+ await db.insert(schema.organizations).values([
32
+ {
33
+ created_at: "4444-04-04",
34
+ id: 1,
35
+ siret: "11111111111111",
36
+ updated_at: "4444-04-04",
37
+ },
38
+ {
39
+ created_at: "4444-04-04",
40
+ id: 2,
41
+ siret: "22222222222222",
42
+ updated_at: "4444-04-04",
43
+ },
44
+ ]);
45
+ await db.insert(schema.users_organizations).values([
46
+ {
47
+ created_at: "4444-04-04",
48
+ is_external: true,
49
+ organization_id: 2,
50
+ updated_at: "4444-04-04",
51
+ user_id: 1,
52
+ verification_type: "domain",
53
+ },
54
+ {
55
+ created_at: "4444-04-04",
56
+ is_external: false,
57
+ organization_id: 1,
58
+ updated_at: "4444-04-04",
59
+ user_id: 1,
60
+ verification_type: "official_contact_email",
61
+ },
62
+ {
63
+ created_at: "4444-04-04",
64
+ is_external: false,
65
+ organization_id: 1,
66
+ updated_at: "4444-04-04",
67
+ user_id: 2,
68
+ verification_type: "domain",
69
+ },
70
+ ]);
71
+
72
+ return {
73
+ db,
74
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
75
+ type: "user",
76
+ user_id: 1,
77
+ }),
78
+ };
79
+ }
80
+
81
+ describe("deleteUser", () => {
82
+ it("✅ deletes the account and journals who it was and which organizations it left", async () => {
83
+ const { db, runtime } = await setup();
84
+
85
+ const outcome = await runtime.deleteUser({ user_id: 1 });
86
+
87
+ const previous = {
88
+ email: "jean.valjean@republic.fr",
89
+ family_name: "Valjean",
90
+ given_name: "Jean",
91
+ memberships: [
92
+ {
93
+ is_external: false,
94
+ organization_id: 1,
95
+ verification_type: "official_contact_email",
96
+ },
97
+ { is_external: true, organization_id: 2, verification_type: "domain" },
98
+ ],
99
+ };
100
+ assert.equal(outcome.kind, "user_deleted");
101
+ if (outcome.kind !== "user_deleted") return;
102
+ assert.deepEqual(outcome.previous, previous);
103
+ assert.deepEqual(await remainingUsers(db), [
104
+ { id: 2, users_organizations: [{ organization_id: 1 }] },
105
+ ]);
106
+ assert.deepEqual(await readActivityLogs(db), [
107
+ {
108
+ action: "user.deleted",
109
+ actor_ref: "1",
110
+ actor_type: "user",
111
+ app: "proconnect-identite",
112
+ cause_activity_id: null,
113
+ context: { inputs: { user_id: 1 }, outcome: previous },
114
+ interaction_cid: null,
115
+ moderation_id: null,
116
+ organization_id: null,
117
+ user_id: 1,
118
+ },
119
+ ]);
120
+ });
121
+
122
+ it("❎ returns user_not_found and journals nothing for a missing account", async () => {
123
+ const { db, runtime } = await setup();
124
+ const before = await remainingUsers(db);
125
+
126
+ const outcome = await runtime.deleteUser({ user_id: 3 });
127
+
128
+ assert.deepEqual(outcome, { kind: "user_not_found", user_id: 3 });
129
+ assert.deepEqual(await remainingUsers(db), before);
130
+ assert.deepEqual(await readActivityLogs(db), []);
131
+ });
132
+ });
133
+
134
+ function remainingUsers(db: IdentiteDatabase) {
135
+ return db.query.users.findMany({
136
+ columns: { id: true },
137
+ orderBy: (table, { asc }) => asc(table.id),
138
+ with: {
139
+ users_organizations: { columns: { organization_id: true } },
140
+ },
141
+ });
142
+ }
@@ -0,0 +1,60 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import { eq } from "drizzle-orm";
7
+
8
+ //
9
+
10
+ type DeleteUserOutcome =
11
+ | {
12
+ kind: "user_deleted";
13
+ activity_id: number;
14
+ previous: ActivityContexts["user.deleted"]["outcome"];
15
+ }
16
+ | { kind: "user_not_found"; user_id: number };
17
+
18
+ export function deleteUserFactory({ actor, app, db }: RuntimeContext) {
19
+ return function deleteUser({
20
+ user_id,
21
+ }: {
22
+ user_id: number;
23
+ }): Promise<DeleteUserOutcome> {
24
+ return db.transaction(async (tx) => {
25
+ const memberships = await tx.query.users_organizations.findMany({
26
+ columns: {
27
+ is_external: true,
28
+ organization_id: true,
29
+ verification_type: true,
30
+ },
31
+ orderBy: (table, { asc }) => asc(table.organization_id),
32
+ where: (table, { eq }) => eq(table.user_id, user_id),
33
+ });
34
+
35
+ const [user] = await tx
36
+ .delete(users)
37
+ .where(eq(users.id, user_id))
38
+ .returning({
39
+ email: users.email,
40
+ family_name: users.family_name,
41
+ given_name: users.given_name,
42
+ });
43
+
44
+ if (!user) {
45
+ return { kind: "user_not_found", user_id };
46
+ }
47
+
48
+ const previous = { ...user, memberships };
49
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
50
+ action: "user.deleted",
51
+ actor,
52
+ app,
53
+ context: { inputs: { user_id }, outcome: previous },
54
+ user_id,
55
+ });
56
+
57
+ return { kind: "user_deleted", activity_id, previous };
58
+ });
59
+ };
60
+ }
@@ -0,0 +1,7 @@
1
+ //
2
+
3
+ export * from "./activities.js";
4
+ export * from "./change-password.js";
5
+ export * from "./delete-user.js";
6
+ export * from "./reset-mfa.js";
7
+ export * from "./update-personal-informations.js";