@proconnect-gouv/proconnect.identite 10.0.0 → 10.0.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 (171) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/connectors/index.d.ts +154 -0
  3. package/dist/connectors/index.d.ts.map +1 -1
  4. package/dist/connectors/index.js +60 -0
  5. package/dist/errors/index.d.ts +3 -0
  6. package/dist/errors/index.d.ts.map +1 -1
  7. package/dist/errors/index.js +6 -0
  8. package/dist/repositories/authenticator/create-authenticator.d.ts +6 -0
  9. package/dist/repositories/authenticator/create-authenticator.d.ts.map +1 -0
  10. package/dist/repositories/authenticator/create-authenticator.js +37 -0
  11. package/dist/repositories/authenticator/delete-authenticator.d.ts +3 -0
  12. package/dist/repositories/authenticator/delete-authenticator.d.ts.map +1 -0
  13. package/dist/repositories/authenticator/delete-authenticator.js +13 -0
  14. package/dist/repositories/authenticator/find-authenticator.d.ts +3 -0
  15. package/dist/repositories/authenticator/find-authenticator.d.ts.map +1 -0
  16. package/dist/repositories/authenticator/find-authenticator.js +14 -0
  17. package/dist/repositories/authenticator/get-authenticators-by-user-id.d.ts +3 -0
  18. package/dist/repositories/authenticator/get-authenticators-by-user-id.d.ts.map +1 -0
  19. package/dist/repositories/authenticator/get-authenticators-by-user-id.js +13 -0
  20. package/dist/repositories/authenticator/index.d.ts +6 -0
  21. package/dist/repositories/authenticator/index.d.ts.map +1 -0
  22. package/dist/repositories/authenticator/index.js +6 -0
  23. package/dist/repositories/authenticator/update-authenticator.d.ts +4 -0
  24. package/dist/repositories/authenticator/update-authenticator.d.ts.map +1 -0
  25. package/dist/repositories/authenticator/update-authenticator.js +13 -0
  26. package/dist/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.d.ts +3 -0
  27. package/dist/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.d.ts.map +1 -0
  28. package/dist/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.js +15 -0
  29. package/dist/repositories/email-deliverability-whitelist/index.d.ts +2 -0
  30. package/dist/repositories/email-deliverability-whitelist/index.d.ts.map +1 -0
  31. package/dist/repositories/email-deliverability-whitelist/index.js +2 -0
  32. package/dist/repositories/moderation/create-moderation.d.ts +22 -0
  33. package/dist/repositories/moderation/create-moderation.d.ts.map +1 -0
  34. package/dist/repositories/moderation/create-moderation.js +21 -0
  35. package/dist/repositories/moderation/delete-moderation.d.ts +3 -0
  36. package/dist/repositories/moderation/delete-moderation.d.ts.map +1 -0
  37. package/dist/repositories/moderation/delete-moderation.js +12 -0
  38. package/dist/repositories/moderation/find-moderation-by-id.d.ts +16 -0
  39. package/dist/repositories/moderation/find-moderation-by-id.d.ts.map +1 -0
  40. package/dist/repositories/moderation/find-moderation-by-id.js +13 -0
  41. package/dist/repositories/moderation/find-pending-moderation.d.ts +20 -0
  42. package/dist/repositories/moderation/find-pending-moderation.d.ts.map +1 -0
  43. package/dist/repositories/moderation/find-pending-moderation.js +16 -0
  44. package/dist/repositories/moderation/find-rejected-moderation.d.ts +20 -0
  45. package/dist/repositories/moderation/find-rejected-moderation.d.ts.map +1 -0
  46. package/dist/repositories/moderation/find-rejected-moderation.js +17 -0
  47. package/dist/repositories/moderation/get-moderation-by-id.d.ts +3 -0
  48. package/dist/repositories/moderation/get-moderation-by-id.d.ts.map +1 -0
  49. package/dist/repositories/moderation/get-moderation-by-id.js +15 -0
  50. package/dist/repositories/moderation/index.d.ts +8 -0
  51. package/dist/repositories/moderation/index.d.ts.map +1 -0
  52. package/dist/repositories/moderation/index.js +8 -0
  53. package/dist/repositories/moderation/reopen-moderation.d.ts +20 -0
  54. package/dist/repositories/moderation/reopen-moderation.d.ts.map +1 -0
  55. package/dist/repositories/moderation/reopen-moderation.js +17 -0
  56. package/dist/repositories/oidc-client/add-connection.d.ts +3 -0
  57. package/dist/repositories/oidc-client/add-connection.d.ts.map +1 -0
  58. package/dist/repositories/oidc-client/add-connection.js +22 -0
  59. package/dist/repositories/oidc-client/find-by-client-id.d.ts +3 -0
  60. package/dist/repositories/oidc-client/find-by-client-id.d.ts.map +1 -0
  61. package/dist/repositories/oidc-client/find-by-client-id.js +14 -0
  62. package/dist/repositories/oidc-client/index.d.ts +3 -0
  63. package/dist/repositories/oidc-client/index.d.ts.map +1 -0
  64. package/dist/repositories/oidc-client/index.js +3 -0
  65. package/dist/repositories/organization/delete-user-organization.d.ts +6 -0
  66. package/dist/repositories/organization/delete-user-organization.d.ts.map +1 -0
  67. package/dist/repositories/organization/delete-user-organization.js +10 -0
  68. package/dist/repositories/organization/find-by-siret.d.ts +3 -0
  69. package/dist/repositories/organization/find-by-siret.d.ts.map +1 -0
  70. package/dist/repositories/organization/find-by-siret.js +13 -0
  71. package/dist/repositories/organization/find-by-verified-email-domain.d.ts +6 -0
  72. package/dist/repositories/organization/find-by-verified-email-domain.d.ts.map +1 -0
  73. package/dist/repositories/organization/find-by-verified-email-domain.js +21 -0
  74. package/dist/repositories/organization/find-pending-by-user-id.d.ts +6 -0
  75. package/dist/repositories/organization/find-pending-by-user-id.d.ts.map +1 -0
  76. package/dist/repositories/organization/find-pending-by-user-id.js +18 -0
  77. package/dist/repositories/organization/get-user-organization-link.d.ts +15 -0
  78. package/dist/repositories/organization/get-user-organization-link.d.ts.map +1 -0
  79. package/dist/repositories/organization/get-user-organization-link.js +23 -0
  80. package/dist/repositories/organization/index.d.ts +5 -0
  81. package/dist/repositories/organization/index.d.ts.map +1 -1
  82. package/dist/repositories/organization/index.js +5 -0
  83. package/dist/repositories/user/delete-franceconnect-userinfo.d.ts +4 -0
  84. package/dist/repositories/user/delete-franceconnect-userinfo.d.ts.map +1 -0
  85. package/dist/repositories/user/delete-franceconnect-userinfo.js +10 -0
  86. package/dist/repositories/user/delete.d.ts +3 -0
  87. package/dist/repositories/user/delete.d.ts.map +1 -0
  88. package/dist/repositories/user/delete.js +11 -0
  89. package/dist/repositories/user/find-by-magic-link-token.d.ts +3 -0
  90. package/dist/repositories/user/find-by-magic-link-token.d.ts.map +1 -0
  91. package/dist/repositories/user/find-by-magic-link-token.js +12 -0
  92. package/dist/repositories/user/find-by-reset-password-token.d.ts +3 -0
  93. package/dist/repositories/user/find-by-reset-password-token.d.ts.map +1 -0
  94. package/dist/repositories/user/find-by-reset-password-token.js +12 -0
  95. package/dist/repositories/user/index.d.ts +3 -0
  96. package/dist/repositories/user/index.d.ts.map +1 -1
  97. package/dist/repositories/user/index.js +3 -0
  98. package/dist/types/authenticator.d.ts +18 -0
  99. package/dist/types/authenticator.d.ts.map +1 -0
  100. package/dist/types/authenticator.js +2 -0
  101. package/dist/types/connection.d.ts +13 -0
  102. package/dist/types/connection.d.ts.map +1 -0
  103. package/dist/types/connection.js +2 -0
  104. package/dist/types/index.d.ts +3 -0
  105. package/dist/types/index.d.ts.map +1 -1
  106. package/dist/types/index.js +3 -0
  107. package/dist/types/oidc-client.d.ts +19 -0
  108. package/dist/types/oidc-client.d.ts.map +1 -0
  109. package/dist/types/oidc-client.js +2 -0
  110. package/package.json +3 -3
  111. package/src/connectors/index.ts +61 -0
  112. package/src/errors/index.ts +7 -0
  113. package/src/repositories/authenticator/create-authenticator.test.ts +44 -0
  114. package/src/repositories/authenticator/create-authenticator.ts +66 -0
  115. package/src/repositories/authenticator/delete-authenticator.test.ts +42 -0
  116. package/src/repositories/authenticator/delete-authenticator.ts +24 -0
  117. package/src/repositories/authenticator/find-authenticator.test.ts +42 -0
  118. package/src/repositories/authenticator/find-authenticator.ts +25 -0
  119. package/src/repositories/authenticator/get-authenticators-by-user-id.test.ts +45 -0
  120. package/src/repositories/authenticator/get-authenticators-by-user-id.ts +21 -0
  121. package/src/repositories/authenticator/index.ts +7 -0
  122. package/src/repositories/authenticator/update-authenticator.test.ts +41 -0
  123. package/src/repositories/authenticator/update-authenticator.ts +29 -0
  124. package/src/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.test.ts +45 -0
  125. package/src/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.ts +24 -0
  126. package/src/repositories/email-deliverability-whitelist/index.ts +3 -0
  127. package/src/repositories/moderation/create-moderation.test.ts +43 -0
  128. package/src/repositories/moderation/create-moderation.ts +45 -0
  129. package/src/repositories/moderation/delete-moderation.test.ts +49 -0
  130. package/src/repositories/moderation/delete-moderation.ts +19 -0
  131. package/src/repositories/moderation/find-moderation-by-id.test.ts +49 -0
  132. package/src/repositories/moderation/find-moderation-by-id.ts +20 -0
  133. package/src/repositories/moderation/find-pending-moderation.test.ts +57 -0
  134. package/src/repositories/moderation/find-pending-moderation.ts +35 -0
  135. package/src/repositories/moderation/find-rejected-moderation.test.ts +57 -0
  136. package/src/repositories/moderation/find-rejected-moderation.ts +37 -0
  137. package/src/repositories/moderation/get-moderation-by-id.test.ts +51 -0
  138. package/src/repositories/moderation/get-moderation-by-id.ts +19 -0
  139. package/src/repositories/moderation/index.ts +9 -0
  140. package/src/repositories/moderation/reopen-moderation.test.ts +48 -0
  141. package/src/repositories/moderation/reopen-moderation.ts +36 -0
  142. package/src/repositories/oidc-client/add-connection.test.ts +43 -0
  143. package/src/repositories/oidc-client/add-connection.ts +36 -0
  144. package/src/repositories/oidc-client/find-by-client-id.test.ts +35 -0
  145. package/src/repositories/oidc-client/find-by-client-id.ts +22 -0
  146. package/src/repositories/oidc-client/index.ts +4 -0
  147. package/src/repositories/organization/delete-user-organization.test.ts +55 -0
  148. package/src/repositories/organization/delete-user-organization.ts +25 -0
  149. package/src/repositories/organization/find-by-siret.test.ts +35 -0
  150. package/src/repositories/organization/find-by-siret.ts +21 -0
  151. package/src/repositories/organization/find-by-verified-email-domain.test.ts +46 -0
  152. package/src/repositories/organization/find-by-verified-email-domain.ts +33 -0
  153. package/src/repositories/organization/find-pending-by-user-id.test.ts +51 -0
  154. package/src/repositories/organization/find-pending-by-user-id.ts +27 -0
  155. package/src/repositories/organization/get-user-organization-link.test.ts +52 -0
  156. package/src/repositories/organization/get-user-organization-link.ts +34 -0
  157. package/src/repositories/organization/index.ts +5 -0
  158. package/src/repositories/user/delete-franceconnect-userinfo.test.ts +47 -0
  159. package/src/repositories/user/delete-franceconnect-userinfo.ts +21 -0
  160. package/src/repositories/user/delete.test.ts +35 -0
  161. package/src/repositories/user/delete.ts +19 -0
  162. package/src/repositories/user/find-by-magic-link-token.test.ts +35 -0
  163. package/src/repositories/user/find-by-magic-link-token.ts +20 -0
  164. package/src/repositories/user/find-by-reset-password-token.test.ts +37 -0
  165. package/src/repositories/user/find-by-reset-password-token.ts +20 -0
  166. package/src/repositories/user/index.ts +3 -0
  167. package/src/types/authenticator.ts +27 -0
  168. package/src/types/connection.ts +15 -0
  169. package/src/types/index.ts +3 -0
  170. package/src/types/oidc-client.ts +20 -0
  171. package/tsconfig.lib.tsbuildinfo +1 -1
@@ -0,0 +1,36 @@
1
+ //
2
+
3
+ import {
4
+ ModerationStatusSchema,
5
+ type DatabaseContext,
6
+ type Moderation,
7
+ } from "#src/types";
8
+ import { type QueryResult } from "pg";
9
+
10
+ //
11
+
12
+ export function reopenModerationFactory({ pg }: DatabaseContext) {
13
+ return async function reopenModeration({
14
+ id,
15
+ userEmail,
16
+ cause,
17
+ }: {
18
+ id: number;
19
+ userEmail: string;
20
+ cause: string;
21
+ }) {
22
+ const { rows }: QueryResult<Moderation> = await pg.query(
23
+ `
24
+ UPDATE moderations
25
+ SET moderated_at = NULL,
26
+ moderated_by = NULL,
27
+ status = $4,
28
+ comment = COALESCE(comment, '') || ' | Réouvert le ' || NOW()::date || ' par ' || $2 || ' - ' || $3
29
+ WHERE id = $1
30
+ RETURNING *;`,
31
+ [id, userEmail, cause, ModerationStatusSchema.enum.reopened],
32
+ );
33
+
34
+ return rows.shift();
35
+ };
36
+ }
@@ -0,0 +1,43 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { addConnectionFactory } from "./add-connection.js";
7
+
8
+ //
9
+
10
+ const addConnection = addConnectionFactory({ pg: pg as any });
11
+
12
+ suite("addConnectionFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should insert and return the connection", async () => {
17
+ await pg.sql`
18
+ INSERT INTO users
19
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
20
+ VALUES
21
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
22
+ ;
23
+ `;
24
+ await pg.sql`
25
+ INSERT INTO oidc_clients
26
+ (id, client_id, client_name, client_secret, created_at, updated_at)
27
+ VALUES
28
+ (1, '⚔️', 'Dark Angels', 'secret', '4444-04-04', '4444-04-04')
29
+ ;
30
+ `;
31
+
32
+ const connection = await addConnection({
33
+ user_id: 1,
34
+ oidc_client_id: 1,
35
+ organization_id: null,
36
+ sp_name: "proconnect",
37
+ user_ip_address: "127.0.0.1",
38
+ });
39
+
40
+ assert.equal(connection.user_id, 1);
41
+ assert.equal(connection.oidc_client_id, 1);
42
+ });
43
+ });
@@ -0,0 +1,36 @@
1
+ //
2
+
3
+ import type { BaseConnection, Connection, DatabaseContext } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function addConnectionFactory({ pg }: DatabaseContext) {
9
+ return async function addConnection({
10
+ user_id,
11
+ oidc_client_id,
12
+ organization_id,
13
+ sp_name,
14
+ user_ip_address,
15
+ }: BaseConnection) {
16
+ const { rows }: QueryResult<Connection> = await pg.query(
17
+ `
18
+ INSERT INTO users_oidc_clients
19
+ (user_id, oidc_client_id, organization_id, sp_name, user_ip_address, created_at, updated_at)
20
+ VALUES ($1, $2, $3, $4, $5, $6, $7)
21
+ RETURNING user_id, oidc_client_id, organization_id, sp_name, user_ip_address, created_at, updated_at, id;
22
+ `,
23
+ [
24
+ user_id,
25
+ oidc_client_id,
26
+ organization_id,
27
+ sp_name,
28
+ user_ip_address,
29
+ new Date(),
30
+ new Date(),
31
+ ],
32
+ );
33
+
34
+ return rows.shift()!;
35
+ };
36
+ }
@@ -0,0 +1,35 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { findByClientIdFactory } from "./find-by-client-id.js";
7
+
8
+ //
9
+
10
+ const findByClientId = findByClientIdFactory({ pg: pg as any });
11
+
12
+ suite("findByClientIdFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return undefined when the client does not exist", async () => {
17
+ const client = await findByClientId("absent");
18
+
19
+ assert.equal(client, undefined);
20
+ });
21
+
22
+ test("should return the client when it exists", async () => {
23
+ await pg.sql`
24
+ INSERT INTO oidc_clients
25
+ (client_id, client_name, client_secret, created_at, updated_at)
26
+ VALUES
27
+ ('⚔️', 'Dark Angels', 'secret', '4444-04-04', '4444-04-04')
28
+ ;
29
+ `;
30
+
31
+ const client = await findByClientId("⚔️");
32
+
33
+ assert.equal(client?.client_name, "Dark Angels");
34
+ });
35
+ });
@@ -0,0 +1,22 @@
1
+ //
2
+
3
+ import type { DatabaseContext, OidcClient } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function findByClientIdFactory({ pg }: DatabaseContext) {
9
+ return async function findByClientId(client_id: string) {
10
+ const { rows }: QueryResult<OidcClient> = await pg.query(
11
+ `
12
+ SELECT
13
+ *
14
+ FROM oidc_clients
15
+ WHERE client_id = $1
16
+ `,
17
+ [client_id],
18
+ );
19
+
20
+ return rows.shift();
21
+ };
22
+ }
@@ -0,0 +1,4 @@
1
+ //
2
+
3
+ export * from "./add-connection.js";
4
+ export * from "./find-by-client-id.js";
@@ -0,0 +1,55 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { deleteUserOrganizationFactory } from "./delete-user-organization.js";
7
+
8
+ //
9
+
10
+ const deleteUserOrganization = deleteUserOrganizationFactory({ pg: pg as any });
11
+
12
+ suite("deleteUserOrganizationFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return true when the link exists", async () => {
17
+ await pg.sql`
18
+ INSERT INTO organizations
19
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
20
+ VALUES
21
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
22
+ ;
23
+ `;
24
+ await pg.sql`
25
+ INSERT INTO users
26
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
27
+ VALUES
28
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
29
+ ;
30
+ `;
31
+ await pg.sql`
32
+ INSERT INTO users_organizations
33
+ (user_id, organization_id, created_at, updated_at, verification_type)
34
+ VALUES
35
+ (1, 1, '4444-04-04', '4444-04-04', 'domain_not_verified_yet')
36
+ ;
37
+ `;
38
+
39
+ const result = await deleteUserOrganization({
40
+ user_id: 1,
41
+ organization_id: 1,
42
+ });
43
+
44
+ assert.equal(result, true);
45
+ });
46
+
47
+ test("should return false when the link does not exist", async () => {
48
+ const result = await deleteUserOrganization({
49
+ user_id: 999,
50
+ organization_id: 999,
51
+ });
52
+
53
+ assert.equal(result, false);
54
+ });
55
+ });
@@ -0,0 +1,25 @@
1
+ //
2
+
3
+ import type { DatabaseContext } from "#src/types";
4
+ import type { QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function deleteUserOrganizationFactory({ pg }: DatabaseContext) {
9
+ return async function deleteUserOrganization({
10
+ user_id,
11
+ organization_id,
12
+ }: {
13
+ user_id: number;
14
+ organization_id: number;
15
+ }) {
16
+ const { rowCount, affectedRows } = (await pg.query(
17
+ `
18
+ DELETE FROM users_organizations
19
+ WHERE user_id = $1 AND organization_id = $2`,
20
+ [user_id, organization_id],
21
+ )) as QueryResult & { affectedRows?: number };
22
+
23
+ return (affectedRows ?? rowCount ?? 0) > 0;
24
+ };
25
+ }
@@ -0,0 +1,35 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { findBySiretFactory } from "./find-by-siret.js";
7
+
8
+ //
9
+
10
+ const findBySiret = findBySiretFactory({ pg: pg as any });
11
+
12
+ suite("findBySiretFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should find the organization by siret", async () => {
17
+ await pg.sql`
18
+ INSERT INTO organizations
19
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
20
+ VALUES
21
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
22
+ ;
23
+ `;
24
+
25
+ const organization = await findBySiret("⚰️");
26
+
27
+ assert.equal(organization?.id, 1);
28
+ });
29
+
30
+ test("should return undefined when siret does not exist", async () => {
31
+ const organization = await findBySiret("absent");
32
+
33
+ assert.equal(organization, undefined);
34
+ });
35
+ });
@@ -0,0 +1,21 @@
1
+ //
2
+
3
+ import type { DatabaseContext, Organization } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function findBySiretFactory({ pg }: DatabaseContext) {
9
+ return async function findBySiret(siret: string) {
10
+ const { rows }: QueryResult<Organization> = await pg.query(
11
+ `
12
+ SELECT *
13
+ FROM organizations
14
+ WHERE siret = $1
15
+ `,
16
+ [siret],
17
+ );
18
+
19
+ return rows.shift();
20
+ };
21
+ }
@@ -0,0 +1,46 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { findByVerifiedEmailDomainFactory } from "./find-by-verified-email-domain.js";
7
+
8
+ //
9
+
10
+ const findByVerifiedEmailDomain = findByVerifiedEmailDomainFactory({
11
+ pg: pg as any,
12
+ });
13
+
14
+ suite("findByVerifiedEmailDomainFactory", () => {
15
+ before(migrate);
16
+ beforeEach(emptyDatabase);
17
+
18
+ test("should return empty array when no organization matches the domain", async () => {
19
+ const organizations = await findByVerifiedEmailDomain("absent.world");
20
+
21
+ assert.deepEqual(organizations, []);
22
+ });
23
+
24
+ test("should return matching organization with member_count", async () => {
25
+ await pg.sql`
26
+ INSERT INTO organizations
27
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at, cached_est_active)
28
+ VALUES
29
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19', 'true')
30
+ ;
31
+ `;
32
+ await pg.sql`
33
+ INSERT INTO email_domains
34
+ (organization_id, domain, verification_type, created_at, updated_at)
35
+ VALUES
36
+ (1, 'darkangels.world', 'verified', '4444-04-04', '4444-04-04')
37
+ ;
38
+ `;
39
+
40
+ const organizations = await findByVerifiedEmailDomain("darkangels.world");
41
+
42
+ assert.equal(organizations.length, 1);
43
+ assert.equal(organizations[0]?.id, 1);
44
+ assert.equal(organizations[0]?.member_count, 0);
45
+ });
46
+ });
@@ -0,0 +1,33 @@
1
+ //
2
+
3
+ import type { DatabaseContext } from "#src/types";
4
+ import {
5
+ EmailDomainApprovedVerificationValues,
6
+ type Organization,
7
+ } from "#src/types";
8
+ import { type QueryResult } from "pg";
9
+
10
+ //
11
+
12
+ export function findByVerifiedEmailDomainFactory({ pg }: DatabaseContext) {
13
+ return async function findByVerifiedEmailDomain(email_domain: string) {
14
+ const { rows }: QueryResult<Organization & { member_count: number }> =
15
+ await pg.query(
16
+ `
17
+ SELECT o.*, count(u.id)::int as member_count
18
+ FROM organizations o
19
+ INNER JOIN email_domains ed ON ed.organization_id = o.id
20
+ LEFT JOIN users_organizations uo ON uo.organization_id = o.id
21
+ LEFT JOIN users u ON u.id = uo.user_id
22
+ AND substring(u.email FROM '@(.+)$') = $1
23
+ WHERE o.cached_est_active = 'true'
24
+ AND ed.domain = $1
25
+ AND ed.verification_type = ANY ($2)
26
+ GROUP BY o.id
27
+ ORDER BY member_count DESC NULLS LAST;`,
28
+ [email_domain, EmailDomainApprovedVerificationValues],
29
+ );
30
+
31
+ return rows;
32
+ };
33
+ }
@@ -0,0 +1,51 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { findPendingByUserIdFactory } from "./find-pending-by-user-id.js";
7
+
8
+ //
9
+
10
+ const findPendingByUserId = findPendingByUserIdFactory({ pg: pg as any });
11
+
12
+ suite("findPendingByUserIdFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return empty array when user has no pending moderations", async () => {
17
+ const organizations = await findPendingByUserId(42);
18
+
19
+ assert.deepEqual(organizations, []);
20
+ });
21
+
22
+ test("should return pending organizations for a user", async () => {
23
+ await pg.sql`
24
+ INSERT INTO organizations
25
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
26
+ VALUES
27
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
28
+ ;
29
+ `;
30
+ await pg.sql`
31
+ INSERT INTO users
32
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
33
+ VALUES
34
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
35
+ ;
36
+ `;
37
+ await pg.sql`
38
+ INSERT INTO moderations
39
+ (id, user_id, organization_id, type, created_at, moderated_at, comment)
40
+ VALUES
41
+ (1, 1, 1, 'organization_join_block', '4444-04-04', NULL, 'pending')
42
+ ;
43
+ `;
44
+
45
+ const organizations = await findPendingByUserId(1);
46
+
47
+ assert.equal(organizations.length, 1);
48
+ assert.equal(organizations[0]?.id, 1);
49
+ assert.equal(organizations[0]?.moderation_id, 1);
50
+ });
51
+ });
@@ -0,0 +1,27 @@
1
+ //
2
+
3
+ import type { DatabaseContext } from "#src/types";
4
+ import { ModerationTypeSchema, type Organization } from "#src/types";
5
+ import { type QueryResult } from "pg";
6
+
7
+ //
8
+
9
+ export function findPendingByUserIdFactory({ pg }: DatabaseContext) {
10
+ return async function findPendingByUserId(user_id: number) {
11
+ const { rows }: QueryResult<Organization & { moderation_id: number }> =
12
+ await pg.query(
13
+ `
14
+ SELECT o.*, m.id as moderation_id
15
+ FROM moderations m
16
+ INNER JOIN organizations o on o.id = m.organization_id
17
+ WHERE m.user_id = $1
18
+ AND m.type = $2
19
+ AND m.moderated_at IS NULL
20
+ ORDER BY m.created_at
21
+ `,
22
+ [user_id, ModerationTypeSchema.enum.organization_join_block],
23
+ );
24
+
25
+ return rows;
26
+ };
27
+ }
@@ -0,0 +1,52 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, suite, test } from "node:test";
6
+ import { getUserOrganizationLinkFactory } from "./get-user-organization-link.js";
7
+
8
+ //
9
+
10
+ const getUserOrganizationLink = getUserOrganizationLinkFactory({
11
+ pg: pg as any,
12
+ });
13
+
14
+ suite("getUserOrganizationLinkFactory", () => {
15
+ before(migrate);
16
+ beforeEach(emptyDatabase);
17
+
18
+ test("should return undefined when the link does not exist", async () => {
19
+ const link = await getUserOrganizationLink(1, 1);
20
+
21
+ assert.equal(link, undefined);
22
+ });
23
+
24
+ test("should return the link when it exists", async () => {
25
+ await pg.sql`
26
+ INSERT INTO organizations
27
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
28
+ VALUES
29
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
30
+ ;
31
+ `;
32
+ await pg.sql`
33
+ INSERT INTO users
34
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
35
+ VALUES
36
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
37
+ ;
38
+ `;
39
+ await pg.sql`
40
+ INSERT INTO users_organizations
41
+ (user_id, organization_id, created_at, updated_at, verification_type)
42
+ VALUES
43
+ (1, 1, '4444-04-04', '4444-04-04', 'domain_not_verified_yet')
44
+ ;
45
+ `;
46
+
47
+ const link = await getUserOrganizationLink(1, 1);
48
+
49
+ assert.equal(link?.user_id, 1);
50
+ assert.equal(link?.organization_id, 1);
51
+ });
52
+ });
@@ -0,0 +1,34 @@
1
+ //
2
+
3
+ import type { DatabaseContext, UserOrganizationLink } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function getUserOrganizationLinkFactory({ pg }: DatabaseContext) {
9
+ return async function getUserOrganizationLink(
10
+ organization_id: number,
11
+ user_id: number,
12
+ ) {
13
+ const { rows }: QueryResult<UserOrganizationLink> = await pg.query(
14
+ `
15
+ SELECT
16
+ user_id,
17
+ organization_id,
18
+ is_external,
19
+ created_at,
20
+ updated_at,
21
+ verification_type,
22
+ verified_at,
23
+ has_been_greeted,
24
+ needs_official_contact_email_verification,
25
+ official_contact_email_verification_token,
26
+ official_contact_email_verification_sent_at
27
+ FROM users_organizations
28
+ WHERE organization_id = $1 AND user_id = $2`,
29
+ [organization_id, user_id],
30
+ );
31
+
32
+ return rows.shift();
33
+ };
34
+ }
@@ -1,7 +1,12 @@
1
1
  //
2
2
 
3
+ export * from "./delete-user-organization.js";
3
4
  export * from "./find-by-id.js";
5
+ export * from "./find-by-siret.js";
4
6
  export * from "./find-by-user-id.js";
7
+ export * from "./find-by-verified-email-domain.js";
8
+ export * from "./find-pending-by-user-id.js";
9
+ export * from "./get-user-organization-link.js";
5
10
  export * from "./get-users-by-organization.js";
6
11
  export * from "./link-user-to-organization.js";
7
12
  export * from "./upsert.js";
@@ -0,0 +1,47 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import assert from "node:assert/strict";
5
+ import { before, beforeEach, describe, it } from "node:test";
6
+ import { deleteFranceConnectUserInfoFactory } from "./delete-franceconnect-userinfo.js";
7
+ import { getFranceConnectUserInfoFactory } from "./get-franceconnect-user-info.js";
8
+
9
+ //
10
+
11
+ const deleteFranceConnectUserInfo = deleteFranceConnectUserInfoFactory({
12
+ pg: pg as any,
13
+ });
14
+ const getFranceConnectUserInfo = getFranceConnectUserInfoFactory({
15
+ pg: pg as any,
16
+ });
17
+
18
+ describe("deleteFranceConnectUserInfo", () => {
19
+ before(migrate);
20
+ beforeEach(emptyDatabase);
21
+
22
+ it("should delete franceconnect user info", async () => {
23
+ await pg.sql`
24
+ INSERT INTO users
25
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
26
+ VALUES
27
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
28
+ ;
29
+ `;
30
+ await pg.sql`
31
+ INSERT INTO franceconnect_userinfo
32
+ (user_id, birthdate, birthplace, family_name, gender, given_name, preferred_username, sub, created_at, updated_at)
33
+ VALUES
34
+ (1, '8888-08-08', 'Caliban', 'El''Jonson', 'male', 'Li', 'Li', 'abcdefghijklmnopqrstuvwxyz', '4444-04-04', '4444-04-04')
35
+ ;
36
+ `;
37
+
38
+ await deleteFranceConnectUserInfo(1);
39
+
40
+ const user = await getFranceConnectUserInfo(1);
41
+ assert.equal(user, undefined);
42
+ });
43
+
44
+ it("should not throw when deleting a user_id with no row", async () => {
45
+ await assert.doesNotReject(() => deleteFranceConnectUserInfo(42));
46
+ });
47
+ });
@@ -0,0 +1,21 @@
1
+ //
2
+
3
+ import type { DatabaseContext } from "#src/types";
4
+
5
+ //
6
+
7
+ export function deleteFranceConnectUserInfoFactory({ pg }: DatabaseContext) {
8
+ return async function deleteFranceConnectUserInfo(user_id: number) {
9
+ return pg.query(
10
+ `
11
+ DELETE FROM franceconnect_userinfo
12
+ WHERE user_id = $1
13
+ `,
14
+ [user_id],
15
+ );
16
+ };
17
+ }
18
+
19
+ export type DeleteFranceConnectUserInfoHandler = ReturnType<
20
+ typeof deleteFranceConnectUserInfoFactory
21
+ >;