@proconnect-gouv/proconnect.identite.runtime 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/activity/activity-context-schemas.d.ts +51 -0
- package/dist/activity/activity-context-schemas.d.ts.map +1 -1
- package/dist/certification/activities.d.ts +64 -0
- package/dist/certification/activities.d.ts.map +1 -0
- package/dist/certification/activities.js +78 -0
- package/dist/certification/index.d.ts +2 -0
- package/dist/certification/index.d.ts.map +1 -0
- package/dist/certification/index.js +2 -0
- package/dist/domains/activities.d.ts +22 -0
- package/dist/domains/activities.d.ts.map +1 -1
- package/dist/domains/activities.js +20 -1
- package/dist/domains/change-email-domain-verification.d.ts +29 -0
- package/dist/domains/change-email-domain-verification.d.ts.map +1 -0
- package/dist/domains/change-email-domain-verification.js +167 -0
- package/dist/domains/index.d.ts +1 -1
- package/dist/domains/index.d.ts.map +1 -1
- package/dist/domains/index.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/runtime/create-identite-runtime.d.ts +60 -20
- package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
- package/dist/runtime/create-identite-runtime.js +13 -3
- package/dist/users/activities.d.ts +29 -0
- package/dist/users/activities.d.ts.map +1 -1
- package/dist/users/activities.js +20 -0
- package/dist/users/change-password.d.ts +16 -0
- package/dist/users/change-password.d.ts.map +1 -0
- package/dist/users/change-password.js +38 -0
- package/dist/users/index.d.ts +2 -0
- package/dist/users/index.d.ts.map +1 -1
- package/dist/users/index.js +2 -0
- package/dist/users/update-personal-informations.d.ts +21 -0
- package/dist/users/update-personal-informations.d.ts.map +1 -0
- package/dist/users/update-personal-informations.js +53 -0
- package/package.json +3 -3
- package/src/certification/activities.test.ts +128 -0
- package/src/certification/activities.ts +103 -0
- package/src/certification/index.ts +3 -0
- package/src/domains/activities.ts +23 -1
- package/src/domains/{mark-domain-as-verified.test.ts → change-email-domain-verification.test.ts} +98 -8
- package/src/domains/change-email-domain-verification.ts +305 -0
- package/src/domains/index.ts +1 -1
- package/src/index.ts +1 -0
- package/src/runtime/create-identite-runtime.test.ts +20 -28
- package/src/runtime/create-identite-runtime.ts +18 -3
- package/src/users/activities.ts +21 -0
- package/src/users/change-password.test.ts +91 -0
- package/src/users/change-password.ts +64 -0
- package/src/users/index.ts +2 -0
- package/src/users/update-personal-informations.test.ts +113 -0
- package/src/users/update-personal-informations.ts +97 -0
- package/dist/domains/mark-domain-as-verified.d.ts +0 -25
- package/dist/domains/mark-domain-as-verified.d.ts.map +0 -1
- package/dist/domains/mark-domain-as-verified.js +0 -103
- package/src/domains/mark-domain-as-verified.ts +0 -174
package/src/users/activities.ts
CHANGED
|
@@ -4,6 +4,13 @@ import * as z from "zod";
|
|
|
4
4
|
|
|
5
5
|
//
|
|
6
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
|
+
|
|
7
14
|
export const userActivities = {
|
|
8
15
|
"user.deleted": z.object({
|
|
9
16
|
inputs: z.object({
|
|
@@ -22,6 +29,12 @@ export const userActivities = {
|
|
|
22
29
|
),
|
|
23
30
|
}),
|
|
24
31
|
}),
|
|
32
|
+
"user.info_changed": z.object({
|
|
33
|
+
inputs: userInformation,
|
|
34
|
+
outcome: z.object({
|
|
35
|
+
previous: userInformation,
|
|
36
|
+
}),
|
|
37
|
+
}),
|
|
25
38
|
"user.mfa_reset": z.object({
|
|
26
39
|
inputs: z.object({
|
|
27
40
|
user_id: z.number(),
|
|
@@ -36,4 +49,12 @@ export const userActivities = {
|
|
|
36
49
|
}),
|
|
37
50
|
}),
|
|
38
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
|
+
}),
|
|
39
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
|
+
}
|
package/src/users/index.ts
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
updated_at: "4444-04-04",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
db,
|
|
22
|
+
runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
|
|
23
|
+
type: "user",
|
|
24
|
+
user_id: 1,
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("updatePersonalInformations", () => {
|
|
30
|
+
it("✅ changes the profile and journals the previous values", async () => {
|
|
31
|
+
const { db, runtime } = await setup();
|
|
32
|
+
|
|
33
|
+
const outcome = await runtime.updatePersonalInformations({
|
|
34
|
+
information: {
|
|
35
|
+
family_name: "Valjean",
|
|
36
|
+
given_name: "Jean",
|
|
37
|
+
job: "Maire",
|
|
38
|
+
phone_number: null,
|
|
39
|
+
},
|
|
40
|
+
user_id: 1,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
assert.deepEqual(outcome, {
|
|
44
|
+
kind: "info_changed",
|
|
45
|
+
activity_id: 1,
|
|
46
|
+
next: {
|
|
47
|
+
family_name: "Valjean",
|
|
48
|
+
given_name: "Jean",
|
|
49
|
+
job: "Maire",
|
|
50
|
+
phone_number: null,
|
|
51
|
+
},
|
|
52
|
+
previous: {
|
|
53
|
+
family_name: null,
|
|
54
|
+
given_name: null,
|
|
55
|
+
job: null,
|
|
56
|
+
phone_number: null,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
assert.deepEqual(await readActivityLogs(db), [
|
|
60
|
+
{
|
|
61
|
+
action: "user.info_changed",
|
|
62
|
+
actor_ref: "1",
|
|
63
|
+
actor_type: "user",
|
|
64
|
+
app: "proconnect-identite",
|
|
65
|
+
cause_activity_id: null,
|
|
66
|
+
context: {
|
|
67
|
+
inputs: {
|
|
68
|
+
family_name: "Valjean",
|
|
69
|
+
given_name: "Jean",
|
|
70
|
+
job: "Maire",
|
|
71
|
+
phone_number: null,
|
|
72
|
+
},
|
|
73
|
+
outcome: {
|
|
74
|
+
previous: {
|
|
75
|
+
family_name: null,
|
|
76
|
+
given_name: null,
|
|
77
|
+
job: null,
|
|
78
|
+
phone_number: null,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
interaction_cid: null,
|
|
83
|
+
moderation_id: null,
|
|
84
|
+
organization_id: null,
|
|
85
|
+
user_id: 1,
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("❎ writes nothing when the profile does not change", async () => {
|
|
91
|
+
const { db, runtime } = await setup();
|
|
92
|
+
|
|
93
|
+
const outcome = await runtime.updatePersonalInformations({
|
|
94
|
+
information: { given_name: null },
|
|
95
|
+
user_id: 1,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
assert.deepEqual(outcome, { kind: "info_unchanged", user_id: 1 });
|
|
99
|
+
assert.deepEqual(await readActivityLogs(db), []);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("❎ returns user_not_found and journals nothing", async () => {
|
|
103
|
+
const { db, runtime } = await setup();
|
|
104
|
+
|
|
105
|
+
const outcome = await runtime.updatePersonalInformations({
|
|
106
|
+
information: { given_name: "Jean" },
|
|
107
|
+
user_id: 42,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
assert.deepEqual(outcome, { kind: "user_not_found", user_id: 42 });
|
|
111
|
+
assert.deepEqual(await readActivityLogs(db), []);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
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 type { User } from "@proconnect-gouv/proconnect.identite/types";
|
|
7
|
+
import { eq, sql } from "drizzle-orm";
|
|
8
|
+
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
type PersonalInformation = Pick<
|
|
12
|
+
User,
|
|
13
|
+
"family_name" | "given_name" | "job" | "phone_number"
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
type UpdatePersonalInformationsOutcome =
|
|
17
|
+
| {
|
|
18
|
+
kind: "info_changed";
|
|
19
|
+
activity_id: number;
|
|
20
|
+
next: PersonalInformation;
|
|
21
|
+
previous: PersonalInformation;
|
|
22
|
+
}
|
|
23
|
+
| { kind: "info_unchanged"; user_id: number }
|
|
24
|
+
| { kind: "user_not_found"; user_id: number };
|
|
25
|
+
|
|
26
|
+
export function updatePersonalInformationsFactory({
|
|
27
|
+
actor,
|
|
28
|
+
app,
|
|
29
|
+
db,
|
|
30
|
+
}: RuntimeContext) {
|
|
31
|
+
return function updatePersonalInformations({
|
|
32
|
+
information,
|
|
33
|
+
user_id,
|
|
34
|
+
}: {
|
|
35
|
+
information: Partial<
|
|
36
|
+
Pick<User, "family_name" | "given_name" | "job" | "phone_number">
|
|
37
|
+
>;
|
|
38
|
+
user_id: number;
|
|
39
|
+
}): Promise<UpdatePersonalInformationsOutcome> {
|
|
40
|
+
return db.transaction(async (tx) => {
|
|
41
|
+
const [previous] = await tx
|
|
42
|
+
.select({
|
|
43
|
+
family_name: users.family_name,
|
|
44
|
+
given_name: users.given_name,
|
|
45
|
+
job: users.job,
|
|
46
|
+
phone_number: users.phone_number,
|
|
47
|
+
})
|
|
48
|
+
.from(users)
|
|
49
|
+
.where(eq(users.id, user_id))
|
|
50
|
+
.for("update");
|
|
51
|
+
|
|
52
|
+
if (!previous) {
|
|
53
|
+
return { kind: "user_not_found", user_id };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const next = {
|
|
57
|
+
family_name:
|
|
58
|
+
information.family_name !== undefined
|
|
59
|
+
? information.family_name
|
|
60
|
+
: previous.family_name,
|
|
61
|
+
given_name:
|
|
62
|
+
information.given_name !== undefined
|
|
63
|
+
? information.given_name
|
|
64
|
+
: previous.given_name,
|
|
65
|
+
job: information.job !== undefined ? information.job : previous.job,
|
|
66
|
+
phone_number:
|
|
67
|
+
information.phone_number !== undefined
|
|
68
|
+
? information.phone_number
|
|
69
|
+
: previous.phone_number,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
next.family_name === previous.family_name &&
|
|
74
|
+
next.given_name === previous.given_name &&
|
|
75
|
+
next.job === previous.job &&
|
|
76
|
+
next.phone_number === previous.phone_number
|
|
77
|
+
) {
|
|
78
|
+
return { kind: "info_unchanged", user_id };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await tx
|
|
82
|
+
.update(users)
|
|
83
|
+
.set({ ...next, updated_at: sql`now()` })
|
|
84
|
+
.where(eq(users.id, user_id));
|
|
85
|
+
|
|
86
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
87
|
+
action: "user.info_changed",
|
|
88
|
+
actor,
|
|
89
|
+
app,
|
|
90
|
+
context: { inputs: next, outcome: { previous } },
|
|
91
|
+
user_id,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return { kind: "info_changed", activity_id, next, previous };
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { RuntimeContext } from "#src/activity";
|
|
2
|
-
import { type EmailDomainApprovedVerificationType } from "@proconnect-gouv/proconnect.identite/types";
|
|
3
|
-
type MarkDomainAsVerifiedOutcome = {
|
|
4
|
-
kind: "domain_verified";
|
|
5
|
-
activity_id: number;
|
|
6
|
-
email_domain: {
|
|
7
|
-
id: number;
|
|
8
|
-
verification_type: EmailDomainApprovedVerificationType;
|
|
9
|
-
};
|
|
10
|
-
promoted_members: ReadonlyArray<{
|
|
11
|
-
previous_verification_type: string;
|
|
12
|
-
user_id: number;
|
|
13
|
-
}>;
|
|
14
|
-
replaced_verification_types: ReadonlyArray<string>;
|
|
15
|
-
} | {
|
|
16
|
-
kind: "organization_not_found";
|
|
17
|
-
organization_id: number;
|
|
18
|
-
};
|
|
19
|
-
export declare function markDomainAsVerifiedFactory({ actor, app, db, }: RuntimeContext): ({ domain, domain_verification_type, organization_id, }: {
|
|
20
|
-
domain: string;
|
|
21
|
-
domain_verification_type: EmailDomainApprovedVerificationType;
|
|
22
|
-
organization_id: number;
|
|
23
|
-
}) => Promise<MarkDomainAsVerifiedOutcome>;
|
|
24
|
-
export {};
|
|
25
|
-
//# sourceMappingURL=mark-domain-as-verified.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mark-domain-as-verified.d.ts","sourceRoot":"","sources":["../../src/domains/mark-domain-as-verified.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,eAAe,CAAC;AAOtE,OAAO,EAKL,KAAK,mCAAmC,EACzC,MAAM,4CAA4C,CAAC;AAKpD,KAAK,2BAA2B,GAC5B;IACE,IAAI,EAAE,iBAAiB,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE;QACZ,EAAE,EAAE,MAAM,CAAC;QACX,iBAAiB,EAAE,mCAAmC,CAAC;KACxD,CAAC;IACF,gBAAgB,EAAE,aAAa,CAAC;QAC9B,0BAA0B,EAAE,MAAM,CAAC;QACnC,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC,CAAC;IACH,2BAA2B,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CACpD,GACD;IAAE,IAAI,EAAE,wBAAwB,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhE,wBAAgB,2BAA2B,CAAC,EAC1C,KAAK,EACL,GAAG,EACH,EAAE,GACH,EAAE,cAAc,IACsB,wDAIlC;IACD,MAAM,EAAE,MAAM,CAAC;IACf,wBAAwB,EAAE,mCAAmC,CAAC;IAC9D,eAAe,EAAE,MAAM,CAAC;CACzB,KAAG,OAAO,CAAC,2BAA2B,CAAC,CA4HzC"}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
import { insertActivity } from "#src/journal";
|
|
3
|
-
import { email_domains, users_organizations, } from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
4
|
-
import { getMemberIdsPromotedByDomain } from "@proconnect-gouv/proconnect.identite/services/organization";
|
|
5
|
-
import { EmailDomainApprovedVerificationValues, EmailDomainPendingVerificationValues, EmailDomainRejectedVerificationValues, LinkEnum, } from "@proconnect-gouv/proconnect.identite/types";
|
|
6
|
-
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
7
|
-
export function markDomainAsVerifiedFactory({ actor, app, db, }) {
|
|
8
|
-
return function markDomainAsVerified({ domain, domain_verification_type, organization_id, }) {
|
|
9
|
-
return db.transaction(async (tx) => {
|
|
10
|
-
const organization = await tx.query.organizations.findFirst({
|
|
11
|
-
columns: { id: true },
|
|
12
|
-
where: (table, { eq }) => eq(table.id, organization_id),
|
|
13
|
-
});
|
|
14
|
-
if (!organization) {
|
|
15
|
-
return { kind: "organization_not_found", organization_id };
|
|
16
|
-
}
|
|
17
|
-
const memberships = await tx.query.users_organizations.findMany({
|
|
18
|
-
columns: { user_id: true, verification_type: true },
|
|
19
|
-
where: (table, { eq }) => eq(table.organization_id, organization_id),
|
|
20
|
-
with: { user: { columns: { email: true } } },
|
|
21
|
-
});
|
|
22
|
-
const members = memberships.map(({ user: { email }, user_id, verification_type }) => ({
|
|
23
|
-
email,
|
|
24
|
-
id: user_id,
|
|
25
|
-
verification_type,
|
|
26
|
-
}));
|
|
27
|
-
const promoted_member_ids = getMemberIdsPromotedByDomain({
|
|
28
|
-
domain,
|
|
29
|
-
members,
|
|
30
|
-
});
|
|
31
|
-
const promoted_members = memberships
|
|
32
|
-
.filter(({ user_id }) => promoted_member_ids.includes(user_id))
|
|
33
|
-
.map(({ user_id, verification_type }) => ({
|
|
34
|
-
previous_verification_type: verification_type,
|
|
35
|
-
user_id,
|
|
36
|
-
}));
|
|
37
|
-
const replaced = await tx
|
|
38
|
-
.delete(email_domains)
|
|
39
|
-
.where(and(eq(email_domains.organization_id, organization_id), eq(email_domains.domain, domain), inArray(email_domains.verification_type, [
|
|
40
|
-
...EmailDomainApprovedVerificationValues,
|
|
41
|
-
...EmailDomainPendingVerificationValues,
|
|
42
|
-
...EmailDomainRejectedVerificationValues,
|
|
43
|
-
])))
|
|
44
|
-
.returning({ verification_type: email_domains.verification_type });
|
|
45
|
-
const [email_domain] = await tx
|
|
46
|
-
.insert(email_domains)
|
|
47
|
-
.values({
|
|
48
|
-
domain,
|
|
49
|
-
organization_id,
|
|
50
|
-
verification_type: domain_verification_type,
|
|
51
|
-
verified_at: sql `now()`,
|
|
52
|
-
})
|
|
53
|
-
.returning({ id: email_domains.id });
|
|
54
|
-
const replaced_verification_types = replaced.map(({ verification_type }) => verification_type);
|
|
55
|
-
const { activity_id } = await insertActivity(tx, {
|
|
56
|
-
action: "domain.verified",
|
|
57
|
-
actor,
|
|
58
|
-
app,
|
|
59
|
-
context: {
|
|
60
|
-
inputs: { domain, verification_type: domain_verification_type },
|
|
61
|
-
outcome: {
|
|
62
|
-
email_domain_id: email_domain.id,
|
|
63
|
-
replaced_verification_types,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
organization_id,
|
|
67
|
-
});
|
|
68
|
-
if (promoted_member_ids.length > 0) {
|
|
69
|
-
await tx
|
|
70
|
-
.update(users_organizations)
|
|
71
|
-
.set({
|
|
72
|
-
updated_at: sql `now()`,
|
|
73
|
-
verification_type: LinkEnum.enum.domain,
|
|
74
|
-
})
|
|
75
|
-
.where(and(eq(users_organizations.organization_id, organization_id), inArray(users_organizations.user_id, promoted_member_ids)));
|
|
76
|
-
for (const { previous_verification_type, user_id, } of promoted_members) {
|
|
77
|
-
await insertActivity(tx, {
|
|
78
|
-
action: "member.promoted",
|
|
79
|
-
actor: { type: "app", job: "domain.promote_members" },
|
|
80
|
-
app,
|
|
81
|
-
cause_activity_id: activity_id,
|
|
82
|
-
context: {
|
|
83
|
-
inputs: { previous_verification_type },
|
|
84
|
-
outcome: { verification_type: LinkEnum.enum.domain },
|
|
85
|
-
},
|
|
86
|
-
organization_id,
|
|
87
|
-
user_id,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return {
|
|
92
|
-
kind: "domain_verified",
|
|
93
|
-
activity_id,
|
|
94
|
-
email_domain: {
|
|
95
|
-
id: email_domain.id,
|
|
96
|
-
verification_type: domain_verification_type,
|
|
97
|
-
},
|
|
98
|
-
promoted_members,
|
|
99
|
-
replaced_verification_types,
|
|
100
|
-
};
|
|
101
|
-
});
|
|
102
|
-
};
|
|
103
|
-
}
|