@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.
Files changed (39) hide show
  1. package/CHANGELOG.md +16 -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} +98 -8
  31. package/src/domains/change-email-domain-verification.ts +305 -0
  32. package/src/domains/index.ts +1 -1
  33. package/src/index.ts +1 -0
  34. package/src/runtime/create-identite-runtime.test.ts +20 -28
  35. package/src/runtime/create-identite-runtime.ts +6 -2
  36. package/dist/domains/mark-domain-as-verified.d.ts +0 -25
  37. package/dist/domains/mark-domain-as-verified.d.ts.map +0 -1
  38. package/dist/domains/mark-domain-as-verified.js +0 -103
  39. package/src/domains/mark-domain-as-verified.ts +0 -174
@@ -1,174 +0,0 @@
1
- //
2
-
3
- import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
- import { insertActivity } from "#src/journal";
5
- import {
6
- email_domains,
7
- users_organizations,
8
- } from "@proconnect-gouv/proconnect.identite.database/drizzle";
9
- import { getMemberIdsPromotedByDomain } from "@proconnect-gouv/proconnect.identite/services/organization";
10
- import {
11
- EmailDomainApprovedVerificationValues,
12
- EmailDomainPendingVerificationValues,
13
- EmailDomainRejectedVerificationValues,
14
- LinkEnum,
15
- type EmailDomainApprovedVerificationType,
16
- } from "@proconnect-gouv/proconnect.identite/types";
17
- import { and, eq, inArray, sql } from "drizzle-orm";
18
-
19
- //
20
-
21
- type MarkDomainAsVerifiedOutcome =
22
- | {
23
- kind: "domain_verified";
24
- activity_id: number;
25
- email_domain: {
26
- id: number;
27
- verification_type: EmailDomainApprovedVerificationType;
28
- };
29
- promoted_members: ReadonlyArray<{
30
- previous_verification_type: string;
31
- user_id: number;
32
- }>;
33
- replaced_verification_types: ReadonlyArray<string>;
34
- }
35
- | { kind: "organization_not_found"; organization_id: number };
36
-
37
- export function markDomainAsVerifiedFactory({
38
- actor,
39
- app,
40
- db,
41
- }: RuntimeContext) {
42
- return function markDomainAsVerified({
43
- domain,
44
- domain_verification_type,
45
- organization_id,
46
- }: {
47
- domain: string;
48
- domain_verification_type: EmailDomainApprovedVerificationType;
49
- organization_id: number;
50
- }): Promise<MarkDomainAsVerifiedOutcome> {
51
- return db.transaction(async (tx) => {
52
- const organization = await tx.query.organizations.findFirst({
53
- columns: { id: true },
54
- where: (table, { eq }) => eq(table.id, organization_id),
55
- });
56
-
57
- if (!organization) {
58
- return { kind: "organization_not_found", organization_id };
59
- }
60
-
61
- const memberships = await tx.query.users_organizations.findMany({
62
- columns: { user_id: true, verification_type: true },
63
- where: (table, { eq }) => eq(table.organization_id, organization_id),
64
- with: { user: { columns: { email: true } } },
65
- });
66
- const members = memberships.map(
67
- ({ user: { email }, user_id, verification_type }) => ({
68
- email,
69
- id: user_id,
70
- verification_type,
71
- }),
72
- );
73
-
74
- const promoted_member_ids = getMemberIdsPromotedByDomain({
75
- domain,
76
- members,
77
- });
78
-
79
- const promoted_members = memberships
80
- .filter(({ user_id }) => promoted_member_ids.includes(user_id))
81
- .map(({ user_id, verification_type }) => ({
82
- previous_verification_type: verification_type,
83
- user_id,
84
- }));
85
-
86
- const replaced = await tx
87
- .delete(email_domains)
88
- .where(
89
- and(
90
- eq(email_domains.organization_id, organization_id),
91
- eq(email_domains.domain, domain),
92
- inArray(email_domains.verification_type, [
93
- ...EmailDomainApprovedVerificationValues,
94
- ...EmailDomainPendingVerificationValues,
95
- ...EmailDomainRejectedVerificationValues,
96
- ]),
97
- ),
98
- )
99
- .returning({ verification_type: email_domains.verification_type });
100
-
101
- const [email_domain] = await tx
102
- .insert(email_domains)
103
- .values({
104
- domain,
105
- organization_id,
106
- verification_type: domain_verification_type,
107
- verified_at: sql`now()`,
108
- })
109
- .returning({ id: email_domains.id });
110
-
111
- const replaced_verification_types = replaced.map(
112
- ({ verification_type }) => verification_type,
113
- );
114
-
115
- const { activity_id } = await insertActivity<ActivityContexts>(tx, {
116
- action: "domain.verified",
117
- actor,
118
- app,
119
- context: {
120
- inputs: { domain, verification_type: domain_verification_type },
121
- outcome: {
122
- email_domain_id: email_domain.id,
123
- replaced_verification_types,
124
- },
125
- },
126
- organization_id,
127
- });
128
-
129
- if (promoted_member_ids.length > 0) {
130
- await tx
131
- .update(users_organizations)
132
- .set({
133
- updated_at: sql`now()`,
134
- verification_type: LinkEnum.enum.domain,
135
- })
136
- .where(
137
- and(
138
- eq(users_organizations.organization_id, organization_id),
139
- inArray(users_organizations.user_id, promoted_member_ids),
140
- ),
141
- );
142
-
143
- for (const {
144
- previous_verification_type,
145
- user_id,
146
- } of promoted_members) {
147
- await insertActivity<ActivityContexts>(tx, {
148
- action: "member.promoted",
149
- actor: { type: "app", job: "domain.promote_members" },
150
- app,
151
- cause_activity_id: activity_id,
152
- context: {
153
- inputs: { previous_verification_type },
154
- outcome: { verification_type: LinkEnum.enum.domain },
155
- },
156
- organization_id,
157
- user_id,
158
- });
159
- }
160
- }
161
-
162
- return {
163
- kind: "domain_verified",
164
- activity_id,
165
- email_domain: {
166
- id: email_domain.id,
167
- verification_type: domain_verification_type,
168
- },
169
- promoted_members,
170
- replaced_verification_types,
171
- };
172
- });
173
- };
174
- }