@proconnect-gouv/proconnect.identite.runtime 0.5.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 +16 -0
- package/dist/activity/activity-context-schemas.d.ts +22 -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 +24 -20
- package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
- package/dist/runtime/create-identite-runtime.js +6 -2
- 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 +6 -2
- 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
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ActivityContexts, RuntimeContext } from "#src/activity";
|
|
4
|
+
import type { IdentiteTransaction } from "#src/database";
|
|
5
|
+
import { insertActivity } from "#src/journal";
|
|
6
|
+
import {
|
|
7
|
+
email_domains,
|
|
8
|
+
users_organizations,
|
|
9
|
+
} from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
10
|
+
import { getMembershipChangesByDomainVerification } from "@proconnect-gouv/proconnect.identite/services/organization";
|
|
11
|
+
import {
|
|
12
|
+
EmailDomainApprovedVerificationValues,
|
|
13
|
+
EmailDomainPendingVerificationValues,
|
|
14
|
+
EmailDomainRejectedVerificationValues,
|
|
15
|
+
LinkEnum,
|
|
16
|
+
type EmailDomainNoPendingVerificationType,
|
|
17
|
+
} from "@proconnect-gouv/proconnect.identite/types";
|
|
18
|
+
import { and, eq, inArray, sql, type SQL } from "drizzle-orm";
|
|
19
|
+
|
|
20
|
+
//
|
|
21
|
+
|
|
22
|
+
type ChangeEmailDomainVerificationOutcome =
|
|
23
|
+
| {
|
|
24
|
+
kind: "domain_verification_changed";
|
|
25
|
+
activity_id: number;
|
|
26
|
+
email_domain: {
|
|
27
|
+
id: number;
|
|
28
|
+
verification_type: EmailDomainNoPendingVerificationType;
|
|
29
|
+
};
|
|
30
|
+
promoted_members: ReadonlyArray<{
|
|
31
|
+
previous_verification_type: string;
|
|
32
|
+
user_id: number;
|
|
33
|
+
}>;
|
|
34
|
+
removed_members: ReadonlyArray<{
|
|
35
|
+
previous_verification_type: string;
|
|
36
|
+
user_id: number;
|
|
37
|
+
}>;
|
|
38
|
+
replaced_verification_types: ReadonlyArray<string>;
|
|
39
|
+
}
|
|
40
|
+
| { kind: "organization_not_found"; organization_id: number };
|
|
41
|
+
|
|
42
|
+
type Member = Awaited<ReturnType<typeof findMembers>>[number];
|
|
43
|
+
|
|
44
|
+
export function changeEmailDomainVerificationFactory({
|
|
45
|
+
actor,
|
|
46
|
+
app,
|
|
47
|
+
db,
|
|
48
|
+
}: RuntimeContext) {
|
|
49
|
+
return function changeEmailDomainVerification({
|
|
50
|
+
domain,
|
|
51
|
+
organization_id,
|
|
52
|
+
verification_type,
|
|
53
|
+
}: {
|
|
54
|
+
domain: string;
|
|
55
|
+
organization_id: number;
|
|
56
|
+
verification_type: EmailDomainNoPendingVerificationType;
|
|
57
|
+
}): Promise<ChangeEmailDomainVerificationOutcome> {
|
|
58
|
+
return db.transaction(async (tx) => {
|
|
59
|
+
const organization = await tx.query.organizations.findFirst({
|
|
60
|
+
columns: { id: true },
|
|
61
|
+
where: (table, { eq }) => eq(table.id, organization_id),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (!organization) {
|
|
65
|
+
return { kind: "organization_not_found", organization_id };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const changes = getMembershipChangesByDomainVerification({
|
|
69
|
+
domain,
|
|
70
|
+
members: await findMembers(tx, { organization_id }),
|
|
71
|
+
verification_type,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
switch (changes.kind) {
|
|
75
|
+
case "domain_approved": {
|
|
76
|
+
const { email_domain_id, replaced_verification_types } =
|
|
77
|
+
await replaceEmailDomain(tx, {
|
|
78
|
+
domain,
|
|
79
|
+
organization_id,
|
|
80
|
+
verification_type: changes.verification_type,
|
|
81
|
+
verified_at: sql`now()`,
|
|
82
|
+
});
|
|
83
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
84
|
+
action: "domain.verified",
|
|
85
|
+
actor,
|
|
86
|
+
app,
|
|
87
|
+
context: {
|
|
88
|
+
inputs: { domain, verification_type: changes.verification_type },
|
|
89
|
+
outcome: { email_domain_id, replaced_verification_types },
|
|
90
|
+
},
|
|
91
|
+
organization_id,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
await promoteMembers(tx, {
|
|
95
|
+
app,
|
|
96
|
+
cause_activity_id: activity_id,
|
|
97
|
+
members: changes.promoted_members,
|
|
98
|
+
organization_id,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
kind: "domain_verification_changed",
|
|
103
|
+
activity_id,
|
|
104
|
+
email_domain: { id: email_domain_id, verification_type },
|
|
105
|
+
promoted_members:
|
|
106
|
+
changes.promoted_members.map(toPreviousMembership),
|
|
107
|
+
removed_members: [],
|
|
108
|
+
replaced_verification_types,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
case "domain_refused": {
|
|
112
|
+
const { email_domain_id, replaced_verification_types } =
|
|
113
|
+
await replaceEmailDomain(tx, {
|
|
114
|
+
domain,
|
|
115
|
+
organization_id,
|
|
116
|
+
verification_type: changes.verification_type,
|
|
117
|
+
verified_at: null,
|
|
118
|
+
});
|
|
119
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
120
|
+
action: "domain.refused",
|
|
121
|
+
actor,
|
|
122
|
+
app,
|
|
123
|
+
context: {
|
|
124
|
+
inputs: { domain, verification_type: changes.verification_type },
|
|
125
|
+
outcome: { email_domain_id, replaced_verification_types },
|
|
126
|
+
},
|
|
127
|
+
organization_id,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
await removeMembers(tx, {
|
|
131
|
+
app,
|
|
132
|
+
cause_activity_id: activity_id,
|
|
133
|
+
members: changes.removed_members,
|
|
134
|
+
organization_id,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
kind: "domain_verification_changed",
|
|
139
|
+
activity_id,
|
|
140
|
+
email_domain: { id: email_domain_id, verification_type },
|
|
141
|
+
promoted_members: [],
|
|
142
|
+
removed_members: changes.removed_members.map(toPreviousMembership),
|
|
143
|
+
replaced_verification_types,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
default: {
|
|
147
|
+
const unhandled_changes: never = changes;
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Unhandled membership changes: ${JSON.stringify(unhandled_changes)}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
//
|
|
158
|
+
|
|
159
|
+
async function findMembers(
|
|
160
|
+
tx: IdentiteTransaction,
|
|
161
|
+
{ organization_id }: { organization_id: number },
|
|
162
|
+
) {
|
|
163
|
+
const memberships = await tx.query.users_organizations.findMany({
|
|
164
|
+
columns: { is_external: true, user_id: true, verification_type: true },
|
|
165
|
+
where: (table, { eq }) => eq(table.organization_id, organization_id),
|
|
166
|
+
with: { user: { columns: { email: true } } },
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return memberships.map(
|
|
170
|
+
({ is_external, user: { email }, user_id, verification_type }) => ({
|
|
171
|
+
email,
|
|
172
|
+
is_external,
|
|
173
|
+
user_id,
|
|
174
|
+
verification_type,
|
|
175
|
+
}),
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function replaceEmailDomain(
|
|
180
|
+
tx: IdentiteTransaction,
|
|
181
|
+
{
|
|
182
|
+
domain,
|
|
183
|
+
organization_id,
|
|
184
|
+
verification_type,
|
|
185
|
+
verified_at,
|
|
186
|
+
}: {
|
|
187
|
+
domain: string;
|
|
188
|
+
organization_id: number;
|
|
189
|
+
verification_type: EmailDomainNoPendingVerificationType;
|
|
190
|
+
verified_at: SQL | null;
|
|
191
|
+
},
|
|
192
|
+
) {
|
|
193
|
+
const replaced = await tx
|
|
194
|
+
.delete(email_domains)
|
|
195
|
+
.where(
|
|
196
|
+
and(
|
|
197
|
+
eq(email_domains.organization_id, organization_id),
|
|
198
|
+
eq(email_domains.domain, domain),
|
|
199
|
+
inArray(email_domains.verification_type, [
|
|
200
|
+
...EmailDomainApprovedVerificationValues,
|
|
201
|
+
...EmailDomainPendingVerificationValues,
|
|
202
|
+
...EmailDomainRejectedVerificationValues,
|
|
203
|
+
]),
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
.returning({ verification_type: email_domains.verification_type });
|
|
207
|
+
|
|
208
|
+
const [email_domain] = await tx
|
|
209
|
+
.insert(email_domains)
|
|
210
|
+
.values({ domain, organization_id, verification_type, verified_at })
|
|
211
|
+
.returning({ id: email_domains.id });
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
email_domain_id: email_domain.id,
|
|
215
|
+
replaced_verification_types: replaced.map(
|
|
216
|
+
({ verification_type }) => verification_type,
|
|
217
|
+
),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function promoteMembers(
|
|
222
|
+
tx: IdentiteTransaction,
|
|
223
|
+
{
|
|
224
|
+
app,
|
|
225
|
+
cause_activity_id,
|
|
226
|
+
members,
|
|
227
|
+
organization_id,
|
|
228
|
+
}: Pick<RuntimeContext, "app"> & {
|
|
229
|
+
cause_activity_id: number;
|
|
230
|
+
members: ReadonlyArray<Member>;
|
|
231
|
+
organization_id: number;
|
|
232
|
+
},
|
|
233
|
+
) {
|
|
234
|
+
if (members.length === 0) return;
|
|
235
|
+
|
|
236
|
+
await tx
|
|
237
|
+
.update(users_organizations)
|
|
238
|
+
.set({ updated_at: sql`now()`, verification_type: LinkEnum.enum.domain })
|
|
239
|
+
.where(
|
|
240
|
+
and(
|
|
241
|
+
eq(users_organizations.organization_id, organization_id),
|
|
242
|
+
inArray(
|
|
243
|
+
users_organizations.user_id,
|
|
244
|
+
members.map(({ user_id }) => user_id),
|
|
245
|
+
),
|
|
246
|
+
),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
for (const { user_id, verification_type } of members) {
|
|
250
|
+
await insertActivity<ActivityContexts>(tx, {
|
|
251
|
+
action: "member.promoted",
|
|
252
|
+
actor: { type: "app", job: "domain.promote_members" },
|
|
253
|
+
app,
|
|
254
|
+
cause_activity_id,
|
|
255
|
+
context: {
|
|
256
|
+
inputs: { previous_verification_type: verification_type },
|
|
257
|
+
outcome: { verification_type: LinkEnum.enum.domain },
|
|
258
|
+
},
|
|
259
|
+
organization_id,
|
|
260
|
+
user_id,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function removeMembers(
|
|
266
|
+
tx: IdentiteTransaction,
|
|
267
|
+
{
|
|
268
|
+
app,
|
|
269
|
+
cause_activity_id,
|
|
270
|
+
members,
|
|
271
|
+
organization_id,
|
|
272
|
+
}: Pick<RuntimeContext, "app"> & {
|
|
273
|
+
cause_activity_id: number;
|
|
274
|
+
members: ReadonlyArray<Member>;
|
|
275
|
+
organization_id: number;
|
|
276
|
+
},
|
|
277
|
+
) {
|
|
278
|
+
if (members.length === 0) return;
|
|
279
|
+
|
|
280
|
+
await tx.delete(users_organizations).where(
|
|
281
|
+
and(
|
|
282
|
+
eq(users_organizations.organization_id, organization_id),
|
|
283
|
+
inArray(
|
|
284
|
+
users_organizations.user_id,
|
|
285
|
+
members.map(({ user_id }) => user_id),
|
|
286
|
+
),
|
|
287
|
+
),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
for (const { is_external, user_id, verification_type } of members) {
|
|
291
|
+
await insertActivity<ActivityContexts>(tx, {
|
|
292
|
+
action: "member.removed",
|
|
293
|
+
actor: { type: "app", job: "domain.remove_members" },
|
|
294
|
+
app,
|
|
295
|
+
cause_activity_id,
|
|
296
|
+
context: { inputs: {}, outcome: { is_external, verification_type } },
|
|
297
|
+
organization_id,
|
|
298
|
+
user_id,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function toPreviousMembership({ user_id, verification_type }: Member) {
|
|
304
|
+
return { previous_verification_type: verification_type, user_id };
|
|
305
|
+
}
|
package/src/domains/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,31 +11,23 @@ import * as z from "zod";
|
|
|
11
11
|
//
|
|
12
12
|
|
|
13
13
|
const hyyypertool_activities = {
|
|
14
|
-
"
|
|
14
|
+
"moderation.commented": z.object({
|
|
15
15
|
inputs: z.object({
|
|
16
|
-
|
|
17
|
-
verification_type: z.literal("refused"),
|
|
16
|
+
comment: z.string(),
|
|
18
17
|
}),
|
|
19
18
|
outcome: z.object({
|
|
20
|
-
|
|
21
|
-
replaced_verification_types: z.array(z.string()),
|
|
19
|
+
moderation_id: z.number(),
|
|
22
20
|
}),
|
|
23
21
|
}),
|
|
24
22
|
};
|
|
25
23
|
|
|
26
24
|
type HyyypertoolActivityContexts = ContextsOf<typeof hyyypertool_activities>;
|
|
27
25
|
|
|
28
|
-
const
|
|
29
|
-
action: "
|
|
26
|
+
const darkangels_commented = {
|
|
27
|
+
action: "moderation.commented" as const,
|
|
30
28
|
context: {
|
|
31
|
-
inputs: {
|
|
32
|
-
|
|
33
|
-
verification_type: "refused" as const,
|
|
34
|
-
},
|
|
35
|
-
outcome: {
|
|
36
|
-
email_domain_id: 7,
|
|
37
|
-
replaced_verification_types: ["not_verified_yet"],
|
|
38
|
-
},
|
|
29
|
+
inputs: { comment: "darkangels.world looks legit" },
|
|
30
|
+
outcome: { moderation_id: 7 },
|
|
39
31
|
},
|
|
40
32
|
organization_id: 1,
|
|
41
33
|
};
|
|
@@ -48,7 +40,7 @@ async function setup() {
|
|
|
48
40
|
moderator: createIdentiteRuntime<HyyypertoolActivityContexts>({
|
|
49
41
|
app: "hyyypertool",
|
|
50
42
|
db,
|
|
51
|
-
}).as({ type: "moderator", sub: "moderator+
|
|
43
|
+
}).as({ type: "moderator", sub: "moderator+comments-darkangels.world" }),
|
|
52
44
|
};
|
|
53
45
|
}
|
|
54
46
|
|
|
@@ -57,18 +49,18 @@ describe("record", () => {
|
|
|
57
49
|
const { db, moderator } = await setup();
|
|
58
50
|
|
|
59
51
|
const outcome = await db.transaction((tx) =>
|
|
60
|
-
moderator.record(tx,
|
|
52
|
+
moderator.record(tx, darkangels_commented),
|
|
61
53
|
);
|
|
62
54
|
|
|
63
55
|
assert.deepEqual(outcome, { activity_id: 1 });
|
|
64
56
|
assert.deepEqual(await readActivityLogs(db), [
|
|
65
57
|
{
|
|
66
|
-
action: "
|
|
67
|
-
actor_ref: "moderator+
|
|
58
|
+
action: "moderation.commented",
|
|
59
|
+
actor_ref: "moderator+comments-darkangels.world",
|
|
68
60
|
actor_type: "moderator",
|
|
69
61
|
app: "hyyypertool",
|
|
70
62
|
cause_activity_id: null,
|
|
71
|
-
context:
|
|
63
|
+
context: darkangels_commented.context,
|
|
72
64
|
interaction_cid: null,
|
|
73
65
|
moderation_id: null,
|
|
74
66
|
organization_id: 1,
|
|
@@ -86,24 +78,24 @@ describe("record", () => {
|
|
|
86
78
|
|
|
87
79
|
void ((tx: IdentiteTransaction) =>
|
|
88
80
|
record(tx, {
|
|
89
|
-
// @ts-expect-error domain.verified belongs to the runtime, only
|
|
81
|
+
// @ts-expect-error domain.verified belongs to the runtime, only changeEmailDomainVerification writes it
|
|
90
82
|
action: "domain.verified",
|
|
91
|
-
context:
|
|
83
|
+
context: darkangels_commented.context,
|
|
92
84
|
}));
|
|
93
85
|
|
|
94
86
|
void ((tx: IdentiteTransaction) =>
|
|
95
87
|
record(tx, {
|
|
96
|
-
action: "
|
|
97
|
-
// @ts-expect-error the context does not match
|
|
98
|
-
context: { inputs: {
|
|
88
|
+
action: "moderation.commented",
|
|
89
|
+
// @ts-expect-error the context does not match moderation.commented
|
|
90
|
+
context: { inputs: { comment: "darkangels.world" }, outcome: {} },
|
|
99
91
|
}));
|
|
100
92
|
|
|
101
93
|
void (() =>
|
|
102
94
|
// @ts-expect-error the bare database is not a transaction
|
|
103
|
-
record(db,
|
|
95
|
+
record(db, darkangels_commented));
|
|
104
96
|
|
|
105
97
|
type OverlappingContexts = {
|
|
106
|
-
"domain.verified": HyyypertoolActivityContexts["
|
|
98
|
+
"domain.verified": HyyypertoolActivityContexts["moderation.commented"];
|
|
107
99
|
};
|
|
108
100
|
// @ts-expect-error domain.verified is already defined by the runtime
|
|
109
101
|
void createIdentiteRuntime<OverlappingContexts>({ app: "hyyypertool", db });
|
|
@@ -112,6 +104,6 @@ describe("record", () => {
|
|
|
112
104
|
createIdentiteRuntime({ app: "proconnect-identite", db })
|
|
113
105
|
.as({ type: "app", job: "join.official_contact" })
|
|
114
106
|
// @ts-expect-error proconnect-identite has no app-only action to record
|
|
115
|
-
.record(tx,
|
|
107
|
+
.record(tx, darkangels_commented));
|
|
116
108
|
});
|
|
117
109
|
});
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
RuntimeContext,
|
|
10
10
|
} from "#src/activity";
|
|
11
11
|
import type { IdentiteTransaction } from "#src/database";
|
|
12
|
-
import {
|
|
12
|
+
import { changeEmailDomainVerificationFactory } from "#src/domains";
|
|
13
13
|
import {
|
|
14
14
|
connectFranceConnectFactory,
|
|
15
15
|
disconnectFranceConnectFactory,
|
|
@@ -39,6 +39,11 @@ export function createIdentiteRuntime<
|
|
|
39
39
|
as(actor: Actor) {
|
|
40
40
|
return {
|
|
41
41
|
addMembership: addMembershipFactory({ actor, app, db }),
|
|
42
|
+
changeEmailDomainVerification: changeEmailDomainVerificationFactory({
|
|
43
|
+
actor,
|
|
44
|
+
app,
|
|
45
|
+
db,
|
|
46
|
+
}),
|
|
42
47
|
changePassword: changePasswordFactory({ actor, app, db }),
|
|
43
48
|
connectFranceConnect: connectFranceConnectFactory({ actor, app, db }),
|
|
44
49
|
deleteUser: deleteUserFactory({ actor, app, db }),
|
|
@@ -47,7 +52,6 @@ export function createIdentiteRuntime<
|
|
|
47
52
|
app,
|
|
48
53
|
db,
|
|
49
54
|
}),
|
|
50
|
-
markDomainAsVerified: markDomainAsVerifiedFactory({ actor, app, db }),
|
|
51
55
|
openModeration: openModerationFactory({ actor, app, db }),
|
|
52
56
|
removeMembership: removeMembershipFactory({ actor, app, db }),
|
|
53
57
|
record: (
|
|
@@ -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
|
-
}
|