@proconnect-gouv/proconnect.identite 10.0.0 → 10.0.2

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 (174) hide show
  1. package/CHANGELOG.md +30 -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/services/organization/compute-service-public-info.d.ts.map +1 -1
  99. package/dist/services/organization/compute-service-public-info.js +7 -6
  100. package/dist/types/authenticator.d.ts +18 -0
  101. package/dist/types/authenticator.d.ts.map +1 -0
  102. package/dist/types/authenticator.js +2 -0
  103. package/dist/types/connection.d.ts +13 -0
  104. package/dist/types/connection.d.ts.map +1 -0
  105. package/dist/types/connection.js +2 -0
  106. package/dist/types/index.d.ts +3 -0
  107. package/dist/types/index.d.ts.map +1 -1
  108. package/dist/types/index.js +3 -0
  109. package/dist/types/oidc-client.d.ts +19 -0
  110. package/dist/types/oidc-client.d.ts.map +1 -0
  111. package/dist/types/oidc-client.js +2 -0
  112. package/package.json +3 -3
  113. package/src/connectors/index.ts +61 -0
  114. package/src/errors/index.ts +7 -0
  115. package/src/repositories/authenticator/create-authenticator.test.ts +44 -0
  116. package/src/repositories/authenticator/create-authenticator.ts +66 -0
  117. package/src/repositories/authenticator/delete-authenticator.test.ts +42 -0
  118. package/src/repositories/authenticator/delete-authenticator.ts +24 -0
  119. package/src/repositories/authenticator/find-authenticator.test.ts +42 -0
  120. package/src/repositories/authenticator/find-authenticator.ts +25 -0
  121. package/src/repositories/authenticator/get-authenticators-by-user-id.test.ts +45 -0
  122. package/src/repositories/authenticator/get-authenticators-by-user-id.ts +21 -0
  123. package/src/repositories/authenticator/index.ts +7 -0
  124. package/src/repositories/authenticator/update-authenticator.test.ts +41 -0
  125. package/src/repositories/authenticator/update-authenticator.ts +29 -0
  126. package/src/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.test.ts +45 -0
  127. package/src/repositories/email-deliverability-whitelist/find-email-in-deliverability-white-list.ts +24 -0
  128. package/src/repositories/email-deliverability-whitelist/index.ts +3 -0
  129. package/src/repositories/moderation/create-moderation.test.ts +43 -0
  130. package/src/repositories/moderation/create-moderation.ts +45 -0
  131. package/src/repositories/moderation/delete-moderation.test.ts +49 -0
  132. package/src/repositories/moderation/delete-moderation.ts +19 -0
  133. package/src/repositories/moderation/find-moderation-by-id.test.ts +49 -0
  134. package/src/repositories/moderation/find-moderation-by-id.ts +20 -0
  135. package/src/repositories/moderation/find-pending-moderation.test.ts +57 -0
  136. package/src/repositories/moderation/find-pending-moderation.ts +35 -0
  137. package/src/repositories/moderation/find-rejected-moderation.test.ts +57 -0
  138. package/src/repositories/moderation/find-rejected-moderation.ts +37 -0
  139. package/src/repositories/moderation/get-moderation-by-id.test.ts +51 -0
  140. package/src/repositories/moderation/get-moderation-by-id.ts +19 -0
  141. package/src/repositories/moderation/index.ts +9 -0
  142. package/src/repositories/moderation/reopen-moderation.test.ts +48 -0
  143. package/src/repositories/moderation/reopen-moderation.ts +36 -0
  144. package/src/repositories/oidc-client/add-connection.test.ts +43 -0
  145. package/src/repositories/oidc-client/add-connection.ts +36 -0
  146. package/src/repositories/oidc-client/find-by-client-id.test.ts +35 -0
  147. package/src/repositories/oidc-client/find-by-client-id.ts +22 -0
  148. package/src/repositories/oidc-client/index.ts +4 -0
  149. package/src/repositories/organization/delete-user-organization.test.ts +55 -0
  150. package/src/repositories/organization/delete-user-organization.ts +25 -0
  151. package/src/repositories/organization/find-by-siret.test.ts +35 -0
  152. package/src/repositories/organization/find-by-siret.ts +21 -0
  153. package/src/repositories/organization/find-by-verified-email-domain.test.ts +46 -0
  154. package/src/repositories/organization/find-by-verified-email-domain.ts +33 -0
  155. package/src/repositories/organization/find-pending-by-user-id.test.ts +51 -0
  156. package/src/repositories/organization/find-pending-by-user-id.ts +27 -0
  157. package/src/repositories/organization/get-user-organization-link.test.ts +52 -0
  158. package/src/repositories/organization/get-user-organization-link.ts +34 -0
  159. package/src/repositories/organization/index.ts +5 -0
  160. package/src/repositories/user/delete-franceconnect-userinfo.test.ts +47 -0
  161. package/src/repositories/user/delete-franceconnect-userinfo.ts +21 -0
  162. package/src/repositories/user/delete.test.ts +35 -0
  163. package/src/repositories/user/delete.ts +19 -0
  164. package/src/repositories/user/find-by-magic-link-token.test.ts +35 -0
  165. package/src/repositories/user/find-by-magic-link-token.ts +20 -0
  166. package/src/repositories/user/find-by-reset-password-token.test.ts +37 -0
  167. package/src/repositories/user/find-by-reset-password-token.ts +20 -0
  168. package/src/repositories/user/index.ts +3 -0
  169. package/src/services/organization/compute-service-public-info.ts +8 -7
  170. package/src/types/authenticator.ts +27 -0
  171. package/src/types/connection.ts +15 -0
  172. package/src/types/index.ts +3 -0
  173. package/src/types/oidc-client.ts +20 -0
  174. package/tsconfig.lib.tsbuildinfo +1 -1
@@ -0,0 +1,41 @@
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 { updateAuthenticatorFactory } from "./update-authenticator.js";
7
+
8
+ //
9
+
10
+ const updateAuthenticator = updateAuthenticatorFactory({ pg: pg as any });
11
+
12
+ suite("updateAuthenticatorFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should update and return the authenticator", 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 authenticators
26
+ (credential_id, credential_public_key, counter, credential_backed_up, user_id, usage_count, user_verified, last_used_at)
27
+ VALUES
28
+ ('CRED1', '\\x00', 0, false, 1, 0, true, '4444-04-04')
29
+ ;
30
+ `;
31
+
32
+ const result = await updateAuthenticator("CRED1", {
33
+ counter: 7,
34
+ last_used_at: new Date("4444-04-05"),
35
+ usage_count: 3,
36
+ });
37
+
38
+ assert.equal(result.counter, 7);
39
+ assert.equal(result.usage_count, 3);
40
+ });
41
+ });
@@ -0,0 +1,29 @@
1
+ //
2
+
3
+ import type {
4
+ Authenticator,
5
+ BaseAuthenticator,
6
+ DatabaseContext,
7
+ } from "#src/types";
8
+ import type { Base64URLString } from "@simplewebauthn/server";
9
+ import { type QueryResult } from "pg";
10
+
11
+ //
12
+
13
+ export function updateAuthenticatorFactory({ pg }: DatabaseContext) {
14
+ return async function updateAuthenticator(
15
+ credential_id: Base64URLString,
16
+ { counter, last_used_at, usage_count }: Partial<BaseAuthenticator>,
17
+ ) {
18
+ const { rows }: QueryResult<Authenticator> = await pg.query(
19
+ `
20
+ UPDATE authenticators
21
+ SET counter = $2, last_used_at = $3, usage_count = $4
22
+ WHERE credential_id = $1
23
+ RETURNING *`,
24
+ [credential_id, counter, last_used_at, usage_count],
25
+ );
26
+
27
+ return rows.shift()!;
28
+ };
29
+ }
@@ -0,0 +1,45 @@
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 { findEmailInDeliverabilityWhiteListFactory } from "./find-email-in-deliverability-white-list.js";
7
+
8
+ //
9
+
10
+ const findEmailInDeliverabilityWhiteList =
11
+ findEmailInDeliverabilityWhiteListFactory({ pg: pg as any });
12
+
13
+ suite("findEmailInDeliverabilityWhiteListFactory", () => {
14
+ before(migrate);
15
+ beforeEach(emptyDatabase);
16
+
17
+ test("should return undefined when the domain is not whitelisted", async () => {
18
+ const result = await findEmailInDeliverabilityWhiteList(
19
+ "lion@darkangels.world",
20
+ );
21
+
22
+ assert.equal(result, undefined);
23
+ });
24
+
25
+ test("should return the whitelist entry when the domain matches", async () => {
26
+ await pg.sql`
27
+ INSERT INTO email_deliverability_whitelist
28
+ (problematic_email, email_domain)
29
+ VALUES
30
+ ('lion@darkangels.world', 'darkangels.world')
31
+ ;
32
+ `;
33
+
34
+ const result = await findEmailInDeliverabilityWhiteList(
35
+ "lion@darkangels.world",
36
+ );
37
+
38
+ assert.deepEqual(result, {
39
+ problematic_email: "lion@darkangels.world",
40
+ email_domain: "darkangels.world",
41
+ verified_at: result?.verified_at,
42
+ verified_by: null,
43
+ });
44
+ });
45
+ });
@@ -0,0 +1,24 @@
1
+ //
2
+
3
+ import { type DatabaseContext } from "#src/types";
4
+ import { getEmailDomain } from "@proconnect-gouv/proconnect.core/services/email";
5
+ import { type QueryResult } from "pg";
6
+
7
+ //
8
+
9
+ export function findEmailInDeliverabilityWhiteListFactory({
10
+ pg,
11
+ }: DatabaseContext) {
12
+ return async function findEmailInDeliverabilityWhiteList(email: string) {
13
+ const domain = getEmailDomain(email);
14
+ const { rows }: QueryResult<{}> = await pg.query(
15
+ `
16
+ SELECT *
17
+ FROM email_deliverability_whitelist WHERE email_domain = $1
18
+ `,
19
+ [domain],
20
+ );
21
+
22
+ return rows.shift();
23
+ };
24
+ }
@@ -0,0 +1,3 @@
1
+ //
2
+
3
+ export * from "./find-email-in-deliverability-white-list.js";
@@ -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 { createModerationFactory } from "./create-moderation.js";
7
+
8
+ //
9
+
10
+ const createModeration = createModerationFactory({ pg: pg as any });
11
+
12
+ suite("createModerationFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should insert and return the moderation", 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
+
32
+ const moderation = await createModeration({
33
+ user_id: 1,
34
+ organization_id: 1,
35
+ type: "organization_join_block",
36
+ ticket_id: null,
37
+ });
38
+
39
+ assert.equal(moderation.user_id, 1);
40
+ assert.equal(moderation.organization_id, 1);
41
+ assert.equal(moderation.status, "pending");
42
+ });
43
+ });
@@ -0,0 +1,45 @@
1
+ //
2
+
3
+ import {
4
+ ModerationStatusSchema,
5
+ type DatabaseContext,
6
+ type Moderation,
7
+ type ModerationType,
8
+ } from "#src/types";
9
+ import { type QueryResult } from "pg";
10
+
11
+ //
12
+
13
+ export function createModerationFactory({ pg }: DatabaseContext) {
14
+ return async function createModeration({
15
+ user_id,
16
+ organization_id,
17
+ sp_name,
18
+ type,
19
+ ticket_id,
20
+ }: {
21
+ user_id: number;
22
+ organization_id: number;
23
+ sp_name?: string;
24
+ type: ModerationType;
25
+ ticket_id: string | null;
26
+ }) {
27
+ const { rows }: QueryResult<Moderation> = await pg.query(
28
+ `
29
+ INSERT INTO moderations (user_id, organization_id, status, type, ticket_id, sp_name, allow_editing)
30
+ VALUES ($1, $2, $3, $4, $5, $6, $7)
31
+ RETURNING *;`,
32
+ [
33
+ user_id,
34
+ organization_id,
35
+ ModerationStatusSchema.enum.pending,
36
+ type,
37
+ ticket_id,
38
+ sp_name ?? null,
39
+ false,
40
+ ],
41
+ );
42
+
43
+ return rows.shift()!;
44
+ };
45
+ }
@@ -0,0 +1,49 @@
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 { deleteModerationFactory } from "./delete-moderation.js";
7
+
8
+ //
9
+
10
+ const deleteModeration = deleteModerationFactory({ pg: pg as any });
11
+
12
+ suite("deleteModerationFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return true when the moderation 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 moderations
33
+ (id, user_id, organization_id, type, status, created_at)
34
+ VALUES
35
+ (1, 1, 1, 'organization_join_block', 'pending', '4444-04-04')
36
+ ;
37
+ `;
38
+
39
+ const result = await deleteModeration(1);
40
+
41
+ assert.equal(result, true);
42
+ });
43
+
44
+ test("should return false when the moderation does not exist", async () => {
45
+ const result = await deleteModeration(999);
46
+
47
+ assert.equal(result, false);
48
+ });
49
+ });
@@ -0,0 +1,19 @@
1
+ //
2
+
3
+ import { type DatabaseContext } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function deleteModerationFactory({ pg }: DatabaseContext) {
9
+ return async function deleteModeration(id: number) {
10
+ const { rowCount, affectedRows } = (await pg.query(
11
+ `
12
+ DELETE FROM moderations
13
+ WHERE id = $1;`,
14
+ [id],
15
+ )) as QueryResult & { affectedRows?: number };
16
+
17
+ return (affectedRows ?? rowCount ?? 0) > 0;
18
+ };
19
+ }
@@ -0,0 +1,49 @@
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 { findModerationByIdFactory } from "./find-moderation-by-id.js";
7
+
8
+ //
9
+
10
+ const findModerationById = findModerationByIdFactory({ pg: pg as any });
11
+
12
+ suite("findModerationByIdFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return undefined when not found", async () => {
17
+ const moderation = await findModerationById(1);
18
+
19
+ assert.equal(moderation, undefined);
20
+ });
21
+
22
+ test("should return the moderation when it exists", 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, status, created_at)
40
+ VALUES
41
+ (1, 1, 1, 'organization_join_block', 'pending', '4444-04-04')
42
+ ;
43
+ `;
44
+
45
+ const moderation = await findModerationById(1);
46
+
47
+ assert.equal(moderation?.id, 1);
48
+ });
49
+ });
@@ -0,0 +1,20 @@
1
+ //
2
+
3
+ import { type DatabaseContext, type Moderation } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function findModerationByIdFactory({ pg }: DatabaseContext) {
9
+ return async function findModerationById(id: number) {
10
+ const { rows }: QueryResult<Moderation> = await pg.query(
11
+ `
12
+ SELECT *
13
+ FROM moderations
14
+ WHERE id = $1;`,
15
+ [id],
16
+ );
17
+
18
+ return rows.shift();
19
+ };
20
+ }
@@ -0,0 +1,57 @@
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 { findPendingModerationFactory } from "./find-pending-moderation.js";
7
+
8
+ //
9
+
10
+ const findPendingModeration = findPendingModerationFactory({ pg: pg as any });
11
+
12
+ suite("findPendingModerationFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return undefined when no pending moderation exists", async () => {
17
+ const moderation = await findPendingModeration({
18
+ user_id: 1,
19
+ organization_id: 1,
20
+ type: "organization_join_block",
21
+ });
22
+
23
+ assert.equal(moderation, undefined);
24
+ });
25
+
26
+ test("should return the pending moderation", async () => {
27
+ await pg.sql`
28
+ INSERT INTO organizations
29
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
30
+ VALUES
31
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
32
+ ;
33
+ `;
34
+ await pg.sql`
35
+ INSERT INTO users
36
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
37
+ VALUES
38
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
39
+ ;
40
+ `;
41
+ await pg.sql`
42
+ INSERT INTO moderations
43
+ (id, user_id, organization_id, type, status, created_at)
44
+ VALUES
45
+ (1, 1, 1, 'organization_join_block', 'pending', '4444-04-04')
46
+ ;
47
+ `;
48
+
49
+ const moderation = await findPendingModeration({
50
+ user_id: 1,
51
+ organization_id: 1,
52
+ type: "organization_join_block",
53
+ });
54
+
55
+ assert.equal(moderation?.id, 1);
56
+ });
57
+ });
@@ -0,0 +1,35 @@
1
+ //
2
+
3
+ import {
4
+ type DatabaseContext,
5
+ type Moderation,
6
+ type ModerationType,
7
+ } from "#src/types";
8
+ import { type QueryResult } from "pg";
9
+
10
+ //
11
+
12
+ export function findPendingModerationFactory({ pg }: DatabaseContext) {
13
+ return async function findPendingModeration({
14
+ user_id,
15
+ organization_id,
16
+ type,
17
+ }: {
18
+ user_id: number;
19
+ organization_id: number;
20
+ type: ModerationType;
21
+ }) {
22
+ const { rows }: QueryResult<Moderation> = await pg.query(
23
+ `
24
+ SELECT *
25
+ FROM moderations
26
+ WHERE user_id = $1
27
+ AND organization_id = $2
28
+ AND type = $3
29
+ AND moderated_at IS NULL;`,
30
+ [user_id, organization_id, type],
31
+ );
32
+
33
+ return rows.shift();
34
+ };
35
+ }
@@ -0,0 +1,57 @@
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 { findRejectedModerationFactory } from "./find-rejected-moderation.js";
7
+
8
+ //
9
+
10
+ const findRejectedModeration = findRejectedModerationFactory({ pg: pg as any });
11
+
12
+ suite("findRejectedModerationFactory", () => {
13
+ before(migrate);
14
+ beforeEach(emptyDatabase);
15
+
16
+ test("should return undefined when no rejected moderation exists", async () => {
17
+ const moderation = await findRejectedModeration({
18
+ user_id: 1,
19
+ organization_id: 1,
20
+ type: "organization_join_block",
21
+ });
22
+
23
+ assert.equal(moderation, undefined);
24
+ });
25
+
26
+ test("should return the rejected moderation", async () => {
27
+ await pg.sql`
28
+ INSERT INTO organizations
29
+ (cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
30
+ VALUES
31
+ ('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
32
+ ;
33
+ `;
34
+ await pg.sql`
35
+ INSERT INTO users
36
+ (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
37
+ VALUES
38
+ (1, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque')
39
+ ;
40
+ `;
41
+ await pg.sql`
42
+ INSERT INTO moderations
43
+ (id, user_id, organization_id, type, status, moderated_at, created_at)
44
+ VALUES
45
+ (1, 1, 1, 'organization_join_block', 'rejected', '4444-04-04', '4444-04-04')
46
+ ;
47
+ `;
48
+
49
+ const moderation = await findRejectedModeration({
50
+ user_id: 1,
51
+ organization_id: 1,
52
+ type: "organization_join_block",
53
+ });
54
+
55
+ assert.equal(moderation?.id, 1);
56
+ });
57
+ });
@@ -0,0 +1,37 @@
1
+ //
2
+
3
+ import {
4
+ ModerationStatusSchema,
5
+ type DatabaseContext,
6
+ type Moderation,
7
+ type ModerationType,
8
+ } from "#src/types";
9
+ import { type QueryResult } from "pg";
10
+
11
+ //
12
+
13
+ export function findRejectedModerationFactory({ pg }: DatabaseContext) {
14
+ return async function findRejectedModeration({
15
+ user_id,
16
+ organization_id,
17
+ type,
18
+ }: {
19
+ user_id: number;
20
+ organization_id: number;
21
+ type: ModerationType;
22
+ }) {
23
+ const { rows }: QueryResult<Moderation> = await pg.query(
24
+ `
25
+ SELECT *
26
+ FROM moderations
27
+ WHERE user_id = $1
28
+ AND organization_id = $2
29
+ AND type = $3
30
+ AND moderated_at IS NOT NULL
31
+ AND status = $4;`,
32
+ [user_id, organization_id, type, ModerationStatusSchema.enum.rejected],
33
+ );
34
+
35
+ return rows.shift();
36
+ };
37
+ }
@@ -0,0 +1,51 @@
1
+ //
2
+
3
+ import { emptyDatabase, migrate, pg } from "#testing";
4
+ import { ModerationNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
5
+ import assert from "node:assert/strict";
6
+ import { before, beforeEach, suite, test } from "node:test";
7
+ import { getModerationByIdFactory } from "./get-moderation-by-id.js";
8
+
9
+ //
10
+
11
+ const getModerationById = getModerationByIdFactory({ pg: pg as any });
12
+
13
+ suite("getModerationByIdFactory", () => {
14
+ before(migrate);
15
+ beforeEach(emptyDatabase);
16
+
17
+ test("should throw ModerationNotFoundError when not found", async () => {
18
+ await assert.rejects(
19
+ () => getModerationById(1),
20
+ (err: unknown) => err instanceof ModerationNotFoundError,
21
+ );
22
+ });
23
+
24
+ test("should return the moderation 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 moderations
41
+ (id, user_id, organization_id, type, status, created_at)
42
+ VALUES
43
+ (1, 1, 1, 'organization_join_block', 'pending', '4444-04-04')
44
+ ;
45
+ `;
46
+
47
+ const moderation = await getModerationById(1);
48
+
49
+ assert.equal(moderation.id, 1);
50
+ });
51
+ });
@@ -0,0 +1,19 @@
1
+ //
2
+
3
+ import { type DatabaseContext, type Moderation } from "#src/types";
4
+ import { ModerationNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
5
+ import { findModerationByIdFactory } from "./find-moderation-by-id.js";
6
+
7
+ //
8
+
9
+ export function getModerationByIdFactory({ pg }: DatabaseContext) {
10
+ const findModerationById = findModerationByIdFactory({ pg });
11
+
12
+ return async function getModerationById(id: number): Promise<Moderation> {
13
+ const moderation = await findModerationById(id);
14
+ if (!moderation) {
15
+ throw new ModerationNotFoundError(`Moderation ${id} not found`);
16
+ }
17
+ return moderation;
18
+ };
19
+ }
@@ -0,0 +1,9 @@
1
+ //
2
+
3
+ export * from "./create-moderation.js";
4
+ export * from "./delete-moderation.js";
5
+ export * from "./find-moderation-by-id.js";
6
+ export * from "./find-pending-moderation.js";
7
+ export * from "./find-rejected-moderation.js";
8
+ export * from "./get-moderation-by-id.js";
9
+ export * from "./reopen-moderation.js";