@proconnect-gouv/proconnect.identite.runtime 0.5.0 → 0.6.1

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 (49) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/activity/activity-context-schemas.d.ts +22 -0
  3. package/dist/activity/activity-context-schemas.d.ts.map +1 -1
  4. package/dist/certification/activities.d.ts +64 -0
  5. package/dist/certification/activities.d.ts.map +1 -0
  6. package/dist/certification/activities.js +78 -0
  7. package/dist/certification/index.d.ts +2 -0
  8. package/dist/certification/index.d.ts.map +1 -0
  9. package/dist/certification/index.js +2 -0
  10. package/dist/domains/activities.d.ts +22 -0
  11. package/dist/domains/activities.d.ts.map +1 -1
  12. package/dist/domains/activities.js +20 -1
  13. package/dist/domains/change-email-domain-verification.d.ts +29 -0
  14. package/dist/domains/change-email-domain-verification.d.ts.map +1 -0
  15. package/dist/domains/change-email-domain-verification.js +167 -0
  16. package/dist/domains/index.d.ts +1 -1
  17. package/dist/domains/index.d.ts.map +1 -1
  18. package/dist/domains/index.js +1 -1
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1 -0
  22. package/dist/runtime/create-identite-runtime.d.ts +24 -20
  23. package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
  24. package/dist/runtime/create-identite-runtime.js +6 -2
  25. package/package.json +3 -3
  26. package/src/certification/activities.test.ts +128 -0
  27. package/src/certification/activities.ts +103 -0
  28. package/src/certification/index.ts +3 -0
  29. package/src/domains/activities.ts +23 -1
  30. package/src/domains/{mark-domain-as-verified.test.ts → change-email-domain-verification.test.ts} +100 -10
  31. package/src/domains/change-email-domain-verification.ts +305 -0
  32. package/src/domains/index.ts +1 -1
  33. package/src/franceconnect/connect-franceconnect.test.ts +4 -4
  34. package/src/franceconnect/disconnect-franceconnect.test.ts +4 -4
  35. package/src/index.ts +1 -0
  36. package/src/memberships/add-membership.test.ts +4 -4
  37. package/src/memberships/remove-membership.test.ts +4 -4
  38. package/src/memberships/update-membership.test.ts +6 -6
  39. package/src/moderations/open-moderation.test.ts +2 -2
  40. package/src/runtime/create-identite-runtime.test.ts +12 -59
  41. package/src/runtime/create-identite-runtime.ts +6 -2
  42. package/src/users/change-password.test.ts +2 -2
  43. package/src/users/delete-user.test.ts +4 -4
  44. package/src/users/reset-mfa.test.ts +5 -5
  45. package/src/users/update-personal-informations.test.ts +2 -2
  46. package/dist/domains/mark-domain-as-verified.d.ts +0 -25
  47. package/dist/domains/mark-domain-as-verified.d.ts.map +0 -1
  48. package/dist/domains/mark-domain-as-verified.js +0 -103
  49. package/src/domains/mark-domain-as-verified.ts +0 -174
@@ -0,0 +1,128 @@
1
+ //
2
+
3
+ import assert from "node:assert/strict";
4
+ import { describe, it } from "node:test";
5
+ import {
6
+ certificationDirigeantActivities,
7
+ getCertificationDirigeantActivity,
8
+ } from "./activities.js";
9
+
10
+ //
11
+
12
+ type Result = Parameters<typeof getCertificationDirigeantActivity>[0];
13
+
14
+ const ulysse = {
15
+ birthcountry: "99100",
16
+ birthdate: new Date("1992-09-07"),
17
+ birthplace: "13055",
18
+ family_name: "Tosi",
19
+ gender: "male",
20
+ given_name: "Ulysse",
21
+ } as const;
22
+
23
+ function result_of({
24
+ cause,
25
+ dirigeant = ulysse,
26
+ source = "registre-national-entreprises.inpi.fr/api",
27
+ }: {
28
+ cause: Result["cause"];
29
+ dirigeant?: Record<string, unknown>;
30
+ source?: string | null;
31
+ }) {
32
+ return {
33
+ cause,
34
+ details: { dirigeant, identity: ulysse, matches: new Set(), source },
35
+ ok: cause === "exact_match",
36
+ } as unknown as Result;
37
+ }
38
+
39
+ describe("getCertificationDirigeantActivity", () => {
40
+ it("records the registry of a certified dirigeant", () => {
41
+ assert.deepEqual(
42
+ getCertificationDirigeantActivity(result_of({ cause: "exact_match" })),
43
+ {
44
+ action: "certification_dirigeant.certified",
45
+ context: {
46
+ inputs: {},
47
+ outcome: { source: "registre-national-entreprises.inpi.fr/api" },
48
+ },
49
+ },
50
+ );
51
+ });
52
+
53
+ it("records only the differing criteria of a close match, as received and as compared", () => {
54
+ assert.deepEqual(
55
+ getCertificationDirigeantActivity(
56
+ result_of({
57
+ cause: "close_match",
58
+ dirigeant: { ...ulysse, given_name: "Ulisse" },
59
+ }),
60
+ ),
61
+ {
62
+ action: "certification_dirigeant.close_matched",
63
+ context: {
64
+ inputs: {},
65
+ outcome: {
66
+ diff: {
67
+ first_name: {
68
+ franceconnect: { compared: "ULYSSE", raw: "Ulysse" },
69
+ source: { compared: "ULISSE", raw: "Ulisse" },
70
+ },
71
+ },
72
+ source: "registre-national-entreprises.inpi.fr/api",
73
+ },
74
+ },
75
+ },
76
+ );
77
+ });
78
+
79
+ it("records why no dirigeant matched", () => {
80
+ assert.deepEqual(
81
+ getCertificationDirigeantActivity(result_of({ cause: "no_candidates" })),
82
+ {
83
+ action: "certification_dirigeant.not_matched",
84
+ context: {
85
+ inputs: {},
86
+ outcome: {
87
+ reason: "no_candidates",
88
+ source: "registre-national-entreprises.inpi.fr/api",
89
+ },
90
+ },
91
+ },
92
+ );
93
+ });
94
+
95
+ it("records an uncovered organization without any registry", () => {
96
+ assert.deepEqual(
97
+ getCertificationDirigeantActivity(
98
+ result_of({ cause: "organisation_not_covered", source: null }),
99
+ ),
100
+ {
101
+ action: "certification_dirigeant.not_covered",
102
+ context: { inputs: {}, outcome: {} },
103
+ },
104
+ );
105
+ });
106
+
107
+ it("builds contexts their published schema accepts", () => {
108
+ for (const cause of [
109
+ "below_threshold",
110
+ "close_match",
111
+ "exact_match",
112
+ "no_candidates",
113
+ "organisation_not_covered",
114
+ ] as const) {
115
+ const { action, context } = getCertificationDirigeantActivity(
116
+ result_of({
117
+ cause,
118
+ dirigeant: { ...ulysse, birthdate: null, family_name: "Tozi" },
119
+ }),
120
+ );
121
+
122
+ assert.deepEqual(
123
+ certificationDirigeantActivities[action].parse(context),
124
+ context,
125
+ );
126
+ }
127
+ });
128
+ });
@@ -0,0 +1,103 @@
1
+ //
2
+
3
+ import type { ActivityOf, ContextsOf } from "#src/activity";
4
+ import {
5
+ CertificationDirigeantDataSource,
6
+ compareCriteria,
7
+ MatchCriteria,
8
+ type processCertificationDirigeantFactory,
9
+ } from "@proconnect-gouv/proconnect.identite/managers/certification";
10
+ import * as z from "zod";
11
+
12
+ //
13
+
14
+ const ComparedValue = z.object({
15
+ compared: z.string().nullable(),
16
+ raw: z.string().nullable(),
17
+ });
18
+
19
+ export const certificationDirigeantActivities = {
20
+ "certification_dirigeant.certified": z.object({
21
+ inputs: z.object({}),
22
+ outcome: z.object({
23
+ source: CertificationDirigeantDataSource,
24
+ }),
25
+ }),
26
+ "certification_dirigeant.close_matched": z.object({
27
+ inputs: z.object({}),
28
+ outcome: z.object({
29
+ diff: z.partialRecord(
30
+ MatchCriteria,
31
+ z.object({ franceconnect: ComparedValue, source: ComparedValue }),
32
+ ),
33
+ source: CertificationDirigeantDataSource,
34
+ }),
35
+ }),
36
+ "certification_dirigeant.not_covered": z.object({
37
+ inputs: z.object({}),
38
+ outcome: z.object({}),
39
+ }),
40
+ "certification_dirigeant.not_matched": z.object({
41
+ inputs: z.object({}),
42
+ outcome: z.object({
43
+ reason: z.enum(["below_threshold", "no_candidates"]),
44
+ source: CertificationDirigeantDataSource,
45
+ }),
46
+ }),
47
+ };
48
+
49
+ //
50
+
51
+ type CertificationDirigeantResult = Awaited<
52
+ ReturnType<ReturnType<typeof processCertificationDirigeantFactory>>
53
+ >;
54
+
55
+ export function getCertificationDirigeantActivity(
56
+ result: CertificationDirigeantResult,
57
+ ): ActivityOf<ContextsOf<typeof certificationDirigeantActivities>> {
58
+ switch (result.cause) {
59
+ case "organisation_not_covered":
60
+ return {
61
+ action: "certification_dirigeant.not_covered",
62
+ context: { inputs: {}, outcome: {} },
63
+ };
64
+ case "exact_match":
65
+ return {
66
+ action: "certification_dirigeant.certified",
67
+ context: { inputs: {}, outcome: { source: result.details.source } },
68
+ };
69
+ case "close_match":
70
+ return {
71
+ action: "certification_dirigeant.close_matched",
72
+ context: {
73
+ inputs: {},
74
+ outcome: {
75
+ diff: Object.fromEntries(
76
+ compareCriteria(result.details.identity, result.details.dirigeant)
77
+ .filter(({ matched }) => !matched)
78
+ .map(({ criterion, franceconnect, source }) => [
79
+ criterion,
80
+ { franceconnect, source },
81
+ ]),
82
+ ),
83
+ source: result.details.source,
84
+ },
85
+ },
86
+ };
87
+ case "below_threshold":
88
+ case "no_candidates":
89
+ return {
90
+ action: "certification_dirigeant.not_matched",
91
+ context: {
92
+ inputs: {},
93
+ outcome: { reason: result.cause, source: result.details.source },
94
+ },
95
+ };
96
+ default:
97
+ return assert_never(result);
98
+ }
99
+ }
100
+
101
+ function assert_never(value: never): never {
102
+ throw new Error(`Unhandled certification result: ${JSON.stringify(value)}`);
103
+ }
@@ -0,0 +1,3 @@
1
+ //
2
+
3
+ export * from "./activities.js";
@@ -1,11 +1,33 @@
1
1
  //
2
2
 
3
- import { EmailDomainApprovedVerificationEnum } from "@proconnect-gouv/proconnect.identite/types";
3
+ import {
4
+ EmailDomainApprovedVerificationEnum,
5
+ EmailDomainRejectedVerificationEnum,
6
+ } from "@proconnect-gouv/proconnect.identite/types";
4
7
  import * as z from "zod";
5
8
 
6
9
  //
7
10
 
8
11
  export const domainActivities = {
12
+ "domain.refused": z.object({
13
+ inputs: z.object({
14
+ domain: z.string(),
15
+ verification_type: EmailDomainRejectedVerificationEnum,
16
+ }),
17
+ outcome: z.object({
18
+ email_domain_id: z.number(),
19
+ replaced_verification_types: z.array(z.string()),
20
+ }),
21
+ }),
22
+ "domain.removed": z.object({
23
+ inputs: z.object({
24
+ domain: z.string(),
25
+ }),
26
+ outcome: z.object({
27
+ email_domain_id: z.number(),
28
+ previous_verification_type: z.string(),
29
+ }),
30
+ }),
9
31
  "domain.verified": z.object({
10
32
  inputs: z.object({
11
33
  domain: z.string(),
@@ -23,10 +23,10 @@ async function setup() {
23
23
  "luther+kept-dirigeant@darkangels.world",
24
24
  "leman.russ+kept-other-domain@spacewolves.world",
25
25
  ].map((email, index) => ({
26
- created_at: "4444-04-04",
26
+ created_at: new Date("4444-04-04"),
27
27
  email,
28
28
  id: index + 1,
29
- updated_at: "4444-04-04",
29
+ updated_at: new Date("4444-04-04"),
30
30
  })),
31
31
  );
32
32
  await db.insert(schema.users_organizations).values(
@@ -102,7 +102,7 @@ async function setup() {
102
102
  };
103
103
  }
104
104
 
105
- describe("markDomainAsVerified", () => {
105
+ describe("changeEmailDomainVerification", () => {
106
106
  before(() => {
107
107
  mock.timers.enable({ apis: ["Date"], now: new Date("5555-05-05") });
108
108
  });
@@ -111,10 +111,10 @@ describe("markDomainAsVerified", () => {
111
111
  const { db, runtime } = await setup();
112
112
  const before = await snapshot(db);
113
113
 
114
- const outcome = await runtime.markDomainAsVerified({
114
+ const outcome = await runtime.changeEmailDomainVerification({
115
115
  domain: "notfound.world",
116
- domain_verification_type: "verified",
117
116
  organization_id: 404,
117
+ verification_type: "verified",
118
118
  });
119
119
 
120
120
  assert.deepEqual(outcome, {
@@ -128,10 +128,10 @@ describe("markDomainAsVerified", () => {
128
128
  it("✅ replaces the domain states, promotes the domain members and journals the verification under the moderator and each promotion under the app", async () => {
129
129
  const { db, runtime } = await setup();
130
130
 
131
- const outcome = await runtime.markDomainAsVerified({
131
+ const outcome = await runtime.changeEmailDomainVerification({
132
132
  domain: "darkangels.world",
133
- domain_verification_type: "verified",
134
133
  organization_id: 1,
134
+ verification_type: "verified",
135
135
  });
136
136
 
137
137
  const verified_domain = await db.query.email_domains.findFirst({
@@ -140,7 +140,7 @@ describe("markDomainAsVerified", () => {
140
140
  and(eq(table.domain, "darkangels.world"), eq(table.organization_id, 1)),
141
141
  });
142
142
  assert.deepEqual(outcome, {
143
- kind: "domain_verified",
143
+ kind: "domain_verification_changed",
144
144
  activity_id: 1,
145
145
  email_domain: {
146
146
  id: verified_domain?.id,
@@ -154,6 +154,7 @@ describe("markDomainAsVerified", () => {
154
154
  },
155
155
  { previous_verification_type: "domain_not_verified_yet", user_id: 2 },
156
156
  ],
157
+ removed_members: [],
157
158
  replaced_verification_types: ["not_verified_yet", "refused"],
158
159
  });
159
160
  assert.deepEqual(
@@ -303,6 +304,95 @@ describe("markDomainAsVerified", () => {
303
304
  ]);
304
305
  });
305
306
 
307
+ it("✅ refuses the domain, removes every member of the domain and journals the refusal under the moderator and each removal under the app", async () => {
308
+ const { db, runtime } = await setup();
309
+
310
+ const outcome = await runtime.changeEmailDomainVerification({
311
+ domain: "darkangels.world",
312
+ organization_id: 1,
313
+ verification_type: "refused",
314
+ });
315
+
316
+ const refused_domain = await db.query.email_domains.findFirst({
317
+ columns: { id: true, verified_at: true },
318
+ where: (table, { and, eq }) =>
319
+ and(eq(table.domain, "darkangels.world"), eq(table.organization_id, 1)),
320
+ });
321
+ assert.deepEqual(outcome, {
322
+ kind: "domain_verification_changed",
323
+ activity_id: 1,
324
+ email_domain: { id: refused_domain?.id, verification_type: "refused" },
325
+ promoted_members: [],
326
+ removed_members: [
327
+ {
328
+ previous_verification_type:
329
+ "no_verification_means_for_entreprise_unipersonnelle",
330
+ user_id: 1,
331
+ },
332
+ { previous_verification_type: "domain_not_verified_yet", user_id: 2 },
333
+ { previous_verification_type: "organization_dirigeant", user_id: 3 },
334
+ ],
335
+ replaced_verification_types: ["not_verified_yet", "refused"],
336
+ });
337
+ assert.equal(refused_domain?.verified_at, null);
338
+ assert.deepEqual(
339
+ await db.query.users_organizations.findMany({
340
+ columns: { organization_id: true, user_id: true },
341
+ orderBy: (table, { asc }) => [
342
+ asc(table.organization_id),
343
+ asc(table.user_id),
344
+ ],
345
+ }),
346
+ [
347
+ { organization_id: 1, user_id: 4 },
348
+ { organization_id: 2, user_id: 1 },
349
+ ],
350
+ );
351
+ assert.deepEqual(
352
+ (await readActivityLogs(db)).map(
353
+ ({ action, actor_ref, cause_activity_id, context, user_id }) => ({
354
+ action,
355
+ actor_ref,
356
+ cause_activity_id,
357
+ context,
358
+ user_id,
359
+ }),
360
+ ),
361
+ [
362
+ {
363
+ action: "domain.refused",
364
+ actor_ref: "moderator+verifies-darkangels.world",
365
+ cause_activity_id: null,
366
+ context: {
367
+ inputs: {
368
+ domain: "darkangels.world",
369
+ verification_type: "refused",
370
+ },
371
+ outcome: {
372
+ email_domain_id: refused_domain?.id,
373
+ replaced_verification_types: ["not_verified_yet", "refused"],
374
+ },
375
+ },
376
+ user_id: null,
377
+ },
378
+ ...[
379
+ "no_verification_means_for_entreprise_unipersonnelle",
380
+ "domain_not_verified_yet",
381
+ "organization_dirigeant",
382
+ ].map((verification_type, index) => ({
383
+ action: "member.removed",
384
+ actor_ref: "domain.remove_members",
385
+ cause_activity_id: 1,
386
+ context: {
387
+ inputs: {},
388
+ outcome: { is_external: false, verification_type },
389
+ },
390
+ user_id: index + 1,
391
+ })),
392
+ ],
393
+ );
394
+ });
395
+
306
396
  it("❎ rolls back the domain state and its journal row when a member update fails", async () => {
307
397
  const { db, runtime } = await setup();
308
398
  await db.execute(sql`
@@ -313,10 +403,10 @@ describe("markDomainAsVerified", () => {
313
403
  const before = await snapshot(db);
314
404
 
315
405
  await assert.rejects(
316
- runtime.markDomainAsVerified({
406
+ runtime.changeEmailDomainVerification({
317
407
  domain: "darkangels.world",
318
- domain_verification_type: "verified",
319
408
  organization_id: 1,
409
+ verification_type: "verified",
320
410
  }),
321
411
  (error: Error) =>
322
412
  error.cause instanceof Error &&