@proconnect-gouv/proconnect.identite.runtime 0.0.0-stage → 0.1.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 +13 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +88 -0
- package/package.json +44 -4
- package/src/database.ts +8 -0
- package/src/index.ts +14 -0
- package/src/mark-domain-as-verified.test.ts +226 -0
- package/src/mark-domain-as-verified.ts +131 -0
- package/tsconfig.json +11 -0
- package/tsconfig.lib.json +9 -0
- package/README.md +0 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @proconnect-gouv/proconnect.identite.runtime
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#2365](https://github.com/proconnect-gouv/proconnect-identite/pull/2365) [`e47d7dd`](https://github.com/proconnect-gouv/proconnect-identite/commit/e47d7dd69c76c7b8c8dc8094c8ec4ac574278e75) Thanks [@douglasduteil](https://github.com/douglasduteil)! - Première version : `createIdentiteRuntime({ db })` reçoit une instance Drizzle construite par l'appelant avec le schéma de `@proconnect-gouv/proconnect.identite.database` et renvoie la commande `markDomainAsVerified`. Elle vérifie un domaine avec un type approuvé dans une seule transaction (remplace les états du domaine pour l'organisation et promeut les membres concernés) et renvoie une issue `domain_verified` (ligne `email_domain` créée, `promoted_members` avec leur rattachement précédent, `replaced_verification_types` supprimés) ou `organization_not_found` (avec `organization_id`). Le type `IdentiteDatabase` est exporté.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`e47d7dd`](https://github.com/proconnect-gouv/proconnect-identite/commit/e47d7dd69c76c7b8c8dc8094c8ec4ac574278e75), [`e47d7dd`](https://github.com/proconnect-gouv/proconnect-identite/commit/e47d7dd69c76c7b8c8dc8094c8ec4ac574278e75), [`da31f6d`](https://github.com/proconnect-gouv/proconnect-identite/commit/da31f6d036c7569c0184962bb9bc6d14d12f04a1)]:
|
|
12
|
+
- @proconnect-gouv/proconnect.identite.database@1.10.1
|
|
13
|
+
- @proconnect-gouv/proconnect.identite@12.0.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as _proconnect_gouv_proconnect_identite_types from '@proconnect-gouv/proconnect.identite/types';
|
|
2
|
+
import * as schema from '@proconnect-gouv/proconnect.identite.database/drizzle';
|
|
3
|
+
import { PgDatabase, PgQueryResultHKT } from 'drizzle-orm/pg-core';
|
|
4
|
+
|
|
5
|
+
type IdentiteDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;
|
|
6
|
+
|
|
7
|
+
declare function createIdentiteRuntime({ db }: {
|
|
8
|
+
db: IdentiteDatabase;
|
|
9
|
+
}): {
|
|
10
|
+
markDomainAsVerified: ({ domain, domain_verification_type, organization_id, }: {
|
|
11
|
+
domain: string;
|
|
12
|
+
domain_verification_type: _proconnect_gouv_proconnect_identite_types.EmailDomainApprovedVerificationType;
|
|
13
|
+
organization_id: number;
|
|
14
|
+
}) => Promise<{
|
|
15
|
+
kind: "domain_verified";
|
|
16
|
+
email_domain: {
|
|
17
|
+
id: number;
|
|
18
|
+
verification_type: _proconnect_gouv_proconnect_identite_types.EmailDomainApprovedVerificationType;
|
|
19
|
+
};
|
|
20
|
+
promoted_members: ReadonlyArray<{
|
|
21
|
+
previous_verification_type: string;
|
|
22
|
+
user_id: number;
|
|
23
|
+
}>;
|
|
24
|
+
replaced_verification_types: ReadonlyArray<string>;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "organization_not_found";
|
|
27
|
+
organization_id: number;
|
|
28
|
+
}>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { createIdentiteRuntime };
|
|
32
|
+
export type { IdentiteDatabase };
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/database.ts","../src/index.ts"],"mappings":";;;;AAOM,KAAM,gBAAgB,GAAG,UAAU,CAAC,gBAAgB,SAAS,MAAM;;ACEzE,iBAAgB,qBAAqB;QAAe,gBAAgB","names":[]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { email_domains, users_organizations } from '@proconnect-gouv/proconnect.identite.database/drizzle';
|
|
2
|
+
import { getMemberIdsPromotedByDomain } from '@proconnect-gouv/proconnect.identite/services/organization';
|
|
3
|
+
import { EmailDomainApprovedVerificationValues, EmailDomainPendingVerificationValues, EmailDomainRejectedVerificationValues, LinkEnum } from '@proconnect-gouv/proconnect.identite/types';
|
|
4
|
+
import { and, eq, inArray, sql } from 'drizzle-orm';
|
|
5
|
+
|
|
6
|
+
function markDomainAsVerifiedFactory({ db }) {
|
|
7
|
+
return function markDomainAsVerified({
|
|
8
|
+
domain,
|
|
9
|
+
domain_verification_type,
|
|
10
|
+
organization_id
|
|
11
|
+
}) {
|
|
12
|
+
return db.transaction(async (tx) => {
|
|
13
|
+
const organization = await tx.query.organizations.findFirst({
|
|
14
|
+
columns: { id: true },
|
|
15
|
+
where: (table, { eq: eq2 }) => eq2(table.id, organization_id)
|
|
16
|
+
});
|
|
17
|
+
if (!organization) {
|
|
18
|
+
return { kind: "organization_not_found", organization_id };
|
|
19
|
+
}
|
|
20
|
+
const memberships = await tx.query.users_organizations.findMany({
|
|
21
|
+
columns: { user_id: true, verification_type: true },
|
|
22
|
+
where: (table, { eq: eq2 }) => eq2(table.organization_id, organization_id),
|
|
23
|
+
with: { user: { columns: { email: true } } }
|
|
24
|
+
});
|
|
25
|
+
const members = memberships.map(
|
|
26
|
+
({ user: { email }, user_id, verification_type }) => ({
|
|
27
|
+
email,
|
|
28
|
+
id: user_id,
|
|
29
|
+
verification_type
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
const promoted_member_ids = getMemberIdsPromotedByDomain({
|
|
33
|
+
domain,
|
|
34
|
+
members
|
|
35
|
+
});
|
|
36
|
+
const replaced = await tx.delete(email_domains).where(
|
|
37
|
+
and(
|
|
38
|
+
eq(email_domains.organization_id, organization_id),
|
|
39
|
+
eq(email_domains.domain, domain),
|
|
40
|
+
inArray(email_domains.verification_type, [
|
|
41
|
+
...EmailDomainApprovedVerificationValues,
|
|
42
|
+
...EmailDomainPendingVerificationValues,
|
|
43
|
+
...EmailDomainRejectedVerificationValues
|
|
44
|
+
])
|
|
45
|
+
)
|
|
46
|
+
).returning({ verification_type: email_domains.verification_type });
|
|
47
|
+
const [email_domain] = await tx.insert(email_domains).values({
|
|
48
|
+
domain,
|
|
49
|
+
organization_id,
|
|
50
|
+
verification_type: domain_verification_type,
|
|
51
|
+
verified_at: sql`now()`
|
|
52
|
+
}).returning({ id: email_domains.id });
|
|
53
|
+
if (promoted_member_ids.length > 0) {
|
|
54
|
+
await tx.update(users_organizations).set({
|
|
55
|
+
updated_at: sql`now()`,
|
|
56
|
+
verification_type: LinkEnum.enum.domain
|
|
57
|
+
}).where(
|
|
58
|
+
and(
|
|
59
|
+
eq(users_organizations.organization_id, organization_id),
|
|
60
|
+
inArray(users_organizations.user_id, promoted_member_ids)
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
kind: "domain_verified",
|
|
66
|
+
email_domain: {
|
|
67
|
+
id: email_domain.id,
|
|
68
|
+
verification_type: domain_verification_type
|
|
69
|
+
},
|
|
70
|
+
promoted_members: memberships.filter(({ user_id }) => promoted_member_ids.includes(user_id)).map(({ user_id, verification_type }) => ({
|
|
71
|
+
previous_verification_type: verification_type,
|
|
72
|
+
user_id
|
|
73
|
+
})),
|
|
74
|
+
replaced_verification_types: replaced.map(
|
|
75
|
+
({ verification_type }) => verification_type
|
|
76
|
+
)
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function createIdentiteRuntime({ db }) {
|
|
83
|
+
return {
|
|
84
|
+
markDomainAsVerified: markDomainAsVerifiedFactory({ db })
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { createIdentiteRuntime };
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proconnect-gouv/proconnect.identite.runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"homepage": "https://github.com/proconnect-gouv/proconnect-identite/tree/main/packages/runtime#readme",
|
|
5
|
+
"bugs": "https://github.com/proconnect-gouv/proconnect-identite/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/proconnect-gouv/proconnect-identite.git",
|
|
9
|
+
"directory": "packages/runtime"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "pkgroll --tsconfig=tsconfig.lib.json",
|
|
22
|
+
"check": "npm run build -- --noEmit",
|
|
23
|
+
"dev": "npm run build -- --watch --preserveWatchOutput",
|
|
24
|
+
"dev:test": "tsx --watch --test src/**/*.test.ts",
|
|
25
|
+
"test": "tsx --test --test-reporter=spec src/**/*.test.ts"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@proconnect-gouv/proconnect.identite": "^12.0.0",
|
|
29
|
+
"@proconnect-gouv/proconnect.identite.database": "^1.10.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@electric-sql/pglite": "^0.3.12",
|
|
33
|
+
"@proconnect-gouv/proconnect.devtools.typescript": "1.0.0",
|
|
34
|
+
"@types/node": "^22.18.6",
|
|
35
|
+
"drizzle-orm": "^0.45.4",
|
|
36
|
+
"pkgroll": "^2.22.0",
|
|
37
|
+
"tsx": "^4.20.3"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"drizzle-orm": "^0.45.4"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/database.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { IdentiteDatabase } from "./database.js";
|
|
4
|
+
import { markDomainAsVerifiedFactory } from "./mark-domain-as-verified.js";
|
|
5
|
+
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
export type { IdentiteDatabase } from "./database.js";
|
|
9
|
+
|
|
10
|
+
export function createIdentiteRuntime({ db }: { db: IdentiteDatabase }) {
|
|
11
|
+
return {
|
|
12
|
+
markDomainAsVerified: markDomainAsVerifiedFactory({ db }),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
4
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
5
|
+
import { migrate } from "@proconnect-gouv/proconnect.identite.database/pglite/migrator";
|
|
6
|
+
import { drizzle } from "drizzle-orm/pglite";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
import { createIdentiteRuntime } from "./index.js";
|
|
10
|
+
|
|
11
|
+
//
|
|
12
|
+
|
|
13
|
+
async function setup() {
|
|
14
|
+
const pg = new PGlite();
|
|
15
|
+
await migrate(pg);
|
|
16
|
+
|
|
17
|
+
await pg.exec(`
|
|
18
|
+
SET search_path TO public;
|
|
19
|
+
|
|
20
|
+
INSERT INTO organizations
|
|
21
|
+
(id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
|
|
22
|
+
VALUES
|
|
23
|
+
(1, 'Dark Angels', 'Organization verifying darkangels.world', '🦁', '4444-04-04', '4444-04-04'),
|
|
24
|
+
(2, 'Space Wolves', 'Other organization using darkangels.world', '🐺', '4444-04-04', '4444-04-04');
|
|
25
|
+
|
|
26
|
+
INSERT INTO users
|
|
27
|
+
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
|
|
28
|
+
VALUES
|
|
29
|
+
(1, 'lion.eljonson+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', '0', 'Primarque'),
|
|
30
|
+
(2, 'azrael+promoted@darkangels.world', '4444-04-04', '4444-04-04', 'Azrael', 'Supreme', '0', 'Grand Master'),
|
|
31
|
+
(3, 'luther+kept-dirigeant@darkangels.world', '4444-04-04', '4444-04-04', 'Luther', 'Fallen', '0', 'Second'),
|
|
32
|
+
(4, 'leman.russ+kept-other-domain@spacewolves.world', '4444-04-04', '4444-04-04', 'Leman', 'Russ', '0', 'Primarque');
|
|
33
|
+
|
|
34
|
+
INSERT INTO users_organizations
|
|
35
|
+
(user_id, organization_id, created_at, updated_at, is_external, verification_type)
|
|
36
|
+
VALUES
|
|
37
|
+
(1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_entreprise_unipersonnelle'),
|
|
38
|
+
(2, 1, '4444-04-04', '4444-04-04', false, 'domain_not_verified_yet'),
|
|
39
|
+
(3, 1, '4444-04-04', '4444-04-04', false, 'organization_dirigeant'),
|
|
40
|
+
(4, 1, '4444-04-04', '4444-04-04', false, 'no_validation_means_available'),
|
|
41
|
+
(1, 2, '4444-04-04', '4444-04-04', false, 'no_validation_means_available');
|
|
42
|
+
|
|
43
|
+
INSERT INTO email_domains
|
|
44
|
+
(id, organization_id, domain, verification_type, created_at, updated_at)
|
|
45
|
+
VALUES
|
|
46
|
+
(1, 1, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04'),
|
|
47
|
+
(2, 1, 'darkangels.world', 'refused', '4444-04-04', '4444-04-04'),
|
|
48
|
+
(3, 1, 'calibanites.world', 'verified', '4444-04-04', '4444-04-04'),
|
|
49
|
+
(4, 2, 'darkangels.world', 'not_verified_yet', '4444-04-04', '4444-04-04');
|
|
50
|
+
`);
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
pg,
|
|
54
|
+
runtime: createIdentiteRuntime({ db: drizzle(pg, { schema }) }),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe("markDomainAsVerified", () => {
|
|
59
|
+
it("❎ returns organization_not_found for an unknown organization", async () => {
|
|
60
|
+
const { pg, runtime } = await setup();
|
|
61
|
+
const before = await snapshot(pg);
|
|
62
|
+
|
|
63
|
+
const outcome = await runtime.markDomainAsVerified({
|
|
64
|
+
domain: "notfound.world",
|
|
65
|
+
domain_verification_type: "official_contact",
|
|
66
|
+
organization_id: 404,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
assert.deepEqual(outcome, {
|
|
70
|
+
kind: "organization_not_found",
|
|
71
|
+
organization_id: 404,
|
|
72
|
+
});
|
|
73
|
+
assert.deepEqual(await snapshot(pg), before);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("✅ replaces the domain states and promotes the domain members", async () => {
|
|
77
|
+
const { pg, runtime } = await setup();
|
|
78
|
+
|
|
79
|
+
const outcome = await runtime.markDomainAsVerified({
|
|
80
|
+
domain: "darkangels.world",
|
|
81
|
+
domain_verification_type: "official_contact",
|
|
82
|
+
organization_id: 1,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
rows: [official_contact_domain],
|
|
87
|
+
} = await pg.query<{ id: number }>(
|
|
88
|
+
"SELECT id FROM email_domains WHERE verification_type = 'official_contact'",
|
|
89
|
+
);
|
|
90
|
+
assert.deepEqual(outcome, {
|
|
91
|
+
kind: "domain_verified",
|
|
92
|
+
email_domain: {
|
|
93
|
+
id: official_contact_domain?.id,
|
|
94
|
+
verification_type: "official_contact",
|
|
95
|
+
},
|
|
96
|
+
promoted_members: [
|
|
97
|
+
{
|
|
98
|
+
previous_verification_type:
|
|
99
|
+
"no_verification_means_for_entreprise_unipersonnelle",
|
|
100
|
+
user_id: 1,
|
|
101
|
+
},
|
|
102
|
+
{ previous_verification_type: "domain_not_verified_yet", user_id: 2 },
|
|
103
|
+
],
|
|
104
|
+
replaced_verification_types: ["not_verified_yet", "refused"],
|
|
105
|
+
});
|
|
106
|
+
assert.deepEqual(
|
|
107
|
+
(
|
|
108
|
+
await pg.query(`
|
|
109
|
+
SELECT
|
|
110
|
+
organization_id,
|
|
111
|
+
domain,
|
|
112
|
+
verification_type,
|
|
113
|
+
can_be_suggested,
|
|
114
|
+
verified_at IS NOT NULL AS is_verified,
|
|
115
|
+
created_at = '4444-04-04' AS is_seeded
|
|
116
|
+
FROM email_domains
|
|
117
|
+
ORDER BY organization_id, domain, verification_type
|
|
118
|
+
`)
|
|
119
|
+
).rows,
|
|
120
|
+
[
|
|
121
|
+
{
|
|
122
|
+
can_be_suggested: true,
|
|
123
|
+
domain: "calibanites.world",
|
|
124
|
+
is_seeded: true,
|
|
125
|
+
is_verified: false,
|
|
126
|
+
organization_id: 1,
|
|
127
|
+
verification_type: "verified",
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
can_be_suggested: true,
|
|
131
|
+
domain: "darkangels.world",
|
|
132
|
+
is_seeded: false,
|
|
133
|
+
is_verified: true,
|
|
134
|
+
organization_id: 1,
|
|
135
|
+
verification_type: "official_contact",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
can_be_suggested: true,
|
|
139
|
+
domain: "darkangels.world",
|
|
140
|
+
is_seeded: true,
|
|
141
|
+
is_verified: false,
|
|
142
|
+
organization_id: 2,
|
|
143
|
+
verification_type: "not_verified_yet",
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
);
|
|
147
|
+
assert.deepEqual(
|
|
148
|
+
(
|
|
149
|
+
await pg.query(`
|
|
150
|
+
SELECT
|
|
151
|
+
organization_id,
|
|
152
|
+
user_id,
|
|
153
|
+
verification_type,
|
|
154
|
+
updated_at <> '4444-04-04' AS is_updated
|
|
155
|
+
FROM users_organizations
|
|
156
|
+
ORDER BY organization_id, user_id
|
|
157
|
+
`)
|
|
158
|
+
).rows,
|
|
159
|
+
[
|
|
160
|
+
{
|
|
161
|
+
is_updated: true,
|
|
162
|
+
organization_id: 1,
|
|
163
|
+
user_id: 1,
|
|
164
|
+
verification_type: "domain",
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
is_updated: true,
|
|
168
|
+
organization_id: 1,
|
|
169
|
+
user_id: 2,
|
|
170
|
+
verification_type: "domain",
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
is_updated: false,
|
|
174
|
+
organization_id: 1,
|
|
175
|
+
user_id: 3,
|
|
176
|
+
verification_type: "organization_dirigeant",
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
is_updated: false,
|
|
180
|
+
organization_id: 1,
|
|
181
|
+
user_id: 4,
|
|
182
|
+
verification_type: "no_validation_means_available",
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
is_updated: false,
|
|
186
|
+
organization_id: 2,
|
|
187
|
+
user_id: 1,
|
|
188
|
+
verification_type: "no_validation_means_available",
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("❎ rolls back the domain state when a member update fails", async () => {
|
|
195
|
+
const { pg, runtime } = await setup();
|
|
196
|
+
await pg.exec(`
|
|
197
|
+
ALTER TABLE users_organizations
|
|
198
|
+
ADD CONSTRAINT users_organizations_never_domain
|
|
199
|
+
CHECK (verification_type <> 'domain');
|
|
200
|
+
`);
|
|
201
|
+
const before = await snapshot(pg);
|
|
202
|
+
|
|
203
|
+
await assert.rejects(
|
|
204
|
+
runtime.markDomainAsVerified({
|
|
205
|
+
domain: "darkangels.world",
|
|
206
|
+
domain_verification_type: "official_contact",
|
|
207
|
+
organization_id: 1,
|
|
208
|
+
}),
|
|
209
|
+
(error: Error) =>
|
|
210
|
+
error.cause instanceof Error &&
|
|
211
|
+
error.cause.message.includes("users_organizations_never_domain"),
|
|
212
|
+
);
|
|
213
|
+
assert.deepEqual(await snapshot(pg), before);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
async function snapshot(pg: PGlite) {
|
|
218
|
+
const [{ rows: email_domains }, { rows: users_organizations }] =
|
|
219
|
+
await Promise.all([
|
|
220
|
+
pg.query("SELECT * FROM email_domains ORDER BY id"),
|
|
221
|
+
pg.query(
|
|
222
|
+
"SELECT * FROM users_organizations ORDER BY organization_id, user_id",
|
|
223
|
+
),
|
|
224
|
+
]);
|
|
225
|
+
return { email_domains, users_organizations };
|
|
226
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
email_domains,
|
|
5
|
+
users_organizations,
|
|
6
|
+
} from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
7
|
+
import { getMemberIdsPromotedByDomain } from "@proconnect-gouv/proconnect.identite/services/organization";
|
|
8
|
+
import {
|
|
9
|
+
EmailDomainApprovedVerificationValues,
|
|
10
|
+
EmailDomainPendingVerificationValues,
|
|
11
|
+
EmailDomainRejectedVerificationValues,
|
|
12
|
+
LinkEnum,
|
|
13
|
+
type EmailDomainApprovedVerificationType,
|
|
14
|
+
} from "@proconnect-gouv/proconnect.identite/types";
|
|
15
|
+
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
16
|
+
import type { IdentiteDatabase } from "./database.js";
|
|
17
|
+
|
|
18
|
+
//
|
|
19
|
+
|
|
20
|
+
type MarkDomainAsVerifiedOutcome =
|
|
21
|
+
| {
|
|
22
|
+
kind: "domain_verified";
|
|
23
|
+
email_domain: {
|
|
24
|
+
id: number;
|
|
25
|
+
verification_type: EmailDomainApprovedVerificationType;
|
|
26
|
+
};
|
|
27
|
+
promoted_members: ReadonlyArray<{
|
|
28
|
+
previous_verification_type: string;
|
|
29
|
+
user_id: number;
|
|
30
|
+
}>;
|
|
31
|
+
replaced_verification_types: ReadonlyArray<string>;
|
|
32
|
+
}
|
|
33
|
+
| { kind: "organization_not_found"; organization_id: number };
|
|
34
|
+
|
|
35
|
+
export function markDomainAsVerifiedFactory({ db }: { db: IdentiteDatabase }) {
|
|
36
|
+
return function markDomainAsVerified({
|
|
37
|
+
domain,
|
|
38
|
+
domain_verification_type,
|
|
39
|
+
organization_id,
|
|
40
|
+
}: {
|
|
41
|
+
domain: string;
|
|
42
|
+
domain_verification_type: EmailDomainApprovedVerificationType;
|
|
43
|
+
organization_id: number;
|
|
44
|
+
}): Promise<MarkDomainAsVerifiedOutcome> {
|
|
45
|
+
return db.transaction(async (tx) => {
|
|
46
|
+
const organization = await tx.query.organizations.findFirst({
|
|
47
|
+
columns: { id: true },
|
|
48
|
+
where: (table, { eq }) => eq(table.id, organization_id),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (!organization) {
|
|
52
|
+
return { kind: "organization_not_found", organization_id };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const memberships = await tx.query.users_organizations.findMany({
|
|
56
|
+
columns: { user_id: true, verification_type: true },
|
|
57
|
+
where: (table, { eq }) => eq(table.organization_id, organization_id),
|
|
58
|
+
with: { user: { columns: { email: true } } },
|
|
59
|
+
});
|
|
60
|
+
const members = memberships.map(
|
|
61
|
+
({ user: { email }, user_id, verification_type }) => ({
|
|
62
|
+
email,
|
|
63
|
+
id: user_id,
|
|
64
|
+
verification_type,
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const promoted_member_ids = getMemberIdsPromotedByDomain({
|
|
69
|
+
domain,
|
|
70
|
+
members,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const replaced = await tx
|
|
74
|
+
.delete(email_domains)
|
|
75
|
+
.where(
|
|
76
|
+
and(
|
|
77
|
+
eq(email_domains.organization_id, organization_id),
|
|
78
|
+
eq(email_domains.domain, domain),
|
|
79
|
+
inArray(email_domains.verification_type, [
|
|
80
|
+
...EmailDomainApprovedVerificationValues,
|
|
81
|
+
...EmailDomainPendingVerificationValues,
|
|
82
|
+
...EmailDomainRejectedVerificationValues,
|
|
83
|
+
]),
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
.returning({ verification_type: email_domains.verification_type });
|
|
87
|
+
|
|
88
|
+
const [email_domain] = await tx
|
|
89
|
+
.insert(email_domains)
|
|
90
|
+
.values({
|
|
91
|
+
domain,
|
|
92
|
+
organization_id,
|
|
93
|
+
verification_type: domain_verification_type,
|
|
94
|
+
verified_at: sql`now()`,
|
|
95
|
+
})
|
|
96
|
+
.returning({ id: email_domains.id });
|
|
97
|
+
|
|
98
|
+
if (promoted_member_ids.length > 0) {
|
|
99
|
+
await tx
|
|
100
|
+
.update(users_organizations)
|
|
101
|
+
.set({
|
|
102
|
+
updated_at: sql`now()`,
|
|
103
|
+
verification_type: LinkEnum.enum.domain,
|
|
104
|
+
})
|
|
105
|
+
.where(
|
|
106
|
+
and(
|
|
107
|
+
eq(users_organizations.organization_id, organization_id),
|
|
108
|
+
inArray(users_organizations.user_id, promoted_member_ids),
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
kind: "domain_verified",
|
|
115
|
+
email_domain: {
|
|
116
|
+
id: email_domain.id,
|
|
117
|
+
verification_type: domain_verification_type,
|
|
118
|
+
},
|
|
119
|
+
promoted_members: memberships
|
|
120
|
+
.filter(({ user_id }) => promoted_member_ids.includes(user_id))
|
|
121
|
+
.map(({ user_id, verification_type }) => ({
|
|
122
|
+
previous_verification_type: verification_type,
|
|
123
|
+
user_id,
|
|
124
|
+
})),
|
|
125
|
+
replaced_verification_types: replaced.map(
|
|
126
|
+
({ verification_type }) => verification_type,
|
|
127
|
+
),
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED