Skip to content

Commit aafacb4

Browse files
committed
♻️ refactor(moderations): consolidate get_moderation variants into repository
- Move get_moderation from context files to repository as specialized functions - GetModerationWithDetails: returns full moderation with organization and user details - GetModerationForEmail: returns minimal data for email context (ticket_id, user.email) - Update all imports and references to use new repository functions - Remove duplicate local functions from context files
1 parent 867d0a2 commit aafacb4

File tree

9 files changed

+117
-85
lines changed

9 files changed

+117
-85
lines changed

bin/www/.env.sentry-build-plugin

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SENTRY_AUTH_TOKEN=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
2+
SENTRY_DSN=https://46befb5e85c441ecba457eb4a9b01a61@errors.data.gouv.fr/26

bin/www/dist/manifest.webmanifest

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"icons": [
3+
{
4+
"src": "android-chrome-192x192.png",
5+
"sizes": "192x192",
6+
"type": "image/png"
7+
},
8+
{
9+
"src": "android-chrome-512x512.png",
10+
"sizes": "512x512",
11+
"type": "image/png"
12+
}
13+
]
14+
}
Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
22

3-
import { NotFoundError } from "@~/app.core/error";
43
import type { App_Context } from "@~/app.middleware/context";
54
import { urls } from "@~/app.urls";
65
import {
76
schema,
87
type IdentiteProconnect_PgDatabase,
98
} from "@~/identite-proconnect.database";
9+
import type { GetModerationWithDetailsDto } from "@~/moderations.repository";
1010
import type { GetFicheOrganizationByIdHandler } from "@~/organizations.lib/usecase";
1111
import { type get_domain_count_dto } from "@~/organizations.repository/get_domain_count";
1212
import { type get_organization_members_count_dto } from "@~/organizations.repository/get_organization_members_count";
@@ -18,13 +18,13 @@ import { useRequestContext } from "hono/jsx-renderer";
1818

1919
export interface ModerationContext extends Env {
2020
Variables: {
21-
moderation: get_moderation_dto;
21+
moderation: GetModerationWithDetailsDto;
2222
};
2323
}
2424
export interface ContextVariablesType extends Env {
2525
Variables: {
2626
domain: string;
27-
moderation: get_moderation_dto;
27+
moderation: GetModerationWithDetailsDto;
2828
organization_member: get_organization_member_dto;
2929
organization_fiche: Awaited<ReturnType<GetFicheOrganizationByIdHandler>>;
3030
query_organization_members_count: Promise<get_organization_members_count_dto>;
@@ -64,52 +64,3 @@ export async function get_organization_member(
6464
export type get_organization_member_dto = Awaited<
6565
ReturnType<typeof get_organization_member>
6666
>;
67-
68-
//
69-
70-
export async function get_moderation(
71-
{ pg }: { pg: IdentiteProconnect_PgDatabase },
72-
{ moderation_id }: { moderation_id: number },
73-
) {
74-
const moderation = await pg.query.moderations.findFirst({
75-
where: eq(schema.moderations.id, moderation_id),
76-
with: {
77-
organization: {
78-
columns: {
79-
cached_activite_principale: true,
80-
cached_adresse: true,
81-
cached_code_postal: true,
82-
cached_est_active: true,
83-
cached_etat_administratif: true,
84-
cached_libelle_activite_principale: true,
85-
cached_libelle_categorie_juridique: true,
86-
cached_libelle_tranche_effectif: true,
87-
cached_libelle: true,
88-
cached_nom_complet: true,
89-
cached_tranche_effectifs: true,
90-
created_at: true,
91-
id: true,
92-
siret: true,
93-
updated_at: true,
94-
},
95-
},
96-
user: {
97-
columns: {
98-
created_at: true,
99-
email: true,
100-
family_name: true,
101-
given_name: true,
102-
id: true,
103-
job: true,
104-
last_sign_in_at: true,
105-
phone_number: true,
106-
sign_in_count: true,
107-
},
108-
},
109-
},
110-
});
111-
112-
if (!moderation) throw new NotFoundError("Moderation not found.");
113-
return moderation;
114-
}
115-
export type get_moderation_dto = Awaited<ReturnType<typeof get_moderation>>;

sources/moderations/api/src/:id/email/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { to } from "await-to-js";
1111
import { Hono } from "hono";
1212
import { jsxRenderer } from "hono/jsx-renderer";
1313
import Page from "./page";
14-
import { get_moderation, type ContextType } from "./page/context";
14+
import { GetModerationForEmail } from "@~/moderations.repository";
15+
import { type ContextType } from "./page/context";
1516

1617
//
1718

@@ -27,9 +28,8 @@ export default new Hono<ContextType>().get(
2728
},
2829
async function set_moderation({ req, set, var: { identite_pg } }, next) {
2930
const { id: moderation_id } = req.valid("param");
30-
const moderation = await get_moderation(identite_pg, {
31-
moderation_id,
32-
});
31+
const get_moderation_for_email = GetModerationForEmail(identite_pg);
32+
const moderation = await get_moderation_for_email(moderation_id);
3333
set("moderation", moderation);
3434
return next();
3535
},
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
//
22

3-
import { NotFoundError } from "@~/app.core/error";
43
import { DescribedBy_Schema, Entity_Schema } from "@~/app.core/schema";
54
import type { App_Context } from "@~/app.middleware/context";
65
import type { Crisp_Context } from "@~/crisp.middleware";
7-
import {
8-
schema,
9-
type IdentiteProconnect_PgDatabase,
10-
} from "@~/identite-proconnect.database";
6+
import type { GetModerationForEmailDto } from "@~/moderations.repository";
117
import type { GetCripsFromSessionIdHandler } from "@~/moderations.lib/usecase/GetCripsFromSessionId";
128
import { type get_zammad_mail_dto } from "@~/zammad.lib/get_zammad_mail";
13-
import { eq } from "drizzle-orm";
149
import { type Env } from "hono";
1510
import { useRequestContext } from "hono/jsx-renderer";
1611
import type { z } from "zod";
@@ -22,7 +17,7 @@ export interface ContextVariablesType extends Env {
2217
MAX_ARTICLE_COUNT: number;
2318
//
2419
crisp?: Awaited<ReturnType<GetCripsFromSessionIdHandler>>;
25-
moderation: get_moderation_dto;
20+
moderation: GetModerationForEmailDto;
2621
zammad:
2722
| undefined
2823
| {
@@ -50,23 +45,3 @@ export const usePageRequestContext = useRequestContext<
5045
PageInputType
5146
>;
5247

53-
//
54-
55-
export async function get_moderation(
56-
pg: IdentiteProconnect_PgDatabase,
57-
{ moderation_id }: { moderation_id: number },
58-
) {
59-
const moderation = await pg.query.moderations.findFirst({
60-
columns: { ticket_id: true },
61-
where: eq(schema.moderations.id, moderation_id),
62-
with: { user: { columns: { email: true } } },
63-
});
64-
65-
if (!moderation) {
66-
throw new NotFoundError("Moderation not found");
67-
}
68-
69-
return moderation;
70-
}
71-
72-
export type get_moderation_dto = Awaited<ReturnType<typeof get_moderation>>;

sources/moderations/api/src/:id/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { Hono } from "hono";
1616
import { getContext } from "hono/context-storage";
1717
import { jsxRenderer } from "hono/jsx-renderer";
1818
import moderation_procedures_router from "./$procedures";
19+
import { GetModerationWithDetails } from "@~/moderations.repository";
1920
import {
20-
get_moderation,
2121
get_organization_member,
2222
type ContextType,
2323
type ContextVariablesType,
@@ -38,8 +38,9 @@ export default new Hono<ContextType>()
3838
const { identite_pg } = getContext<App_Context>().var;
3939
const { id: moderation_id } = req.valid("param");
4040

41+
const get_moderation_with_details = GetModerationWithDetails(identite_pg);
4142
const [moderation_error, moderation] = await to(
42-
get_moderation({ pg: identite_pg }, { moderation_id }),
43+
get_moderation_with_details(moderation_id),
4344
);
4445

4546
if (moderation_error instanceof NotFoundError) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
3+
import { NotFoundError } from "@~/app.core/error";
4+
import {
5+
schema,
6+
type IdentiteProconnect_PgDatabase,
7+
} from "@~/identite-proconnect.database";
8+
import { eq } from "drizzle-orm";
9+
10+
//
11+
12+
export function GetModerationForEmail(pg: IdentiteProconnect_PgDatabase) {
13+
return async function get_moderation_for_email(moderation_id: number) {
14+
const moderation = await pg.query.moderations.findFirst({
15+
columns: { ticket_id: true },
16+
where: eq(schema.moderations.id, moderation_id),
17+
with: { user: { columns: { email: true } } },
18+
});
19+
20+
if (!moderation) {
21+
throw new NotFoundError("Moderation not found");
22+
}
23+
24+
return moderation;
25+
};
26+
}
27+
28+
export type GetModerationForEmailHandler = ReturnType<typeof GetModerationForEmail>;
29+
export type GetModerationForEmailDto = Awaited<ReturnType<GetModerationForEmailHandler>>;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
3+
import { NotFoundError } from "@~/app.core/error";
4+
import {
5+
schema,
6+
type IdentiteProconnect_PgDatabase,
7+
} from "@~/identite-proconnect.database";
8+
import { eq } from "drizzle-orm";
9+
10+
//
11+
12+
export function GetModerationWithDetails(pg: IdentiteProconnect_PgDatabase) {
13+
return async function get_moderation_with_details(moderation_id: number) {
14+
const moderation = await pg.query.moderations.findFirst({
15+
where: eq(schema.moderations.id, moderation_id),
16+
with: {
17+
organization: {
18+
columns: {
19+
cached_activite_principale: true,
20+
cached_adresse: true,
21+
cached_code_postal: true,
22+
cached_est_active: true,
23+
cached_etat_administratif: true,
24+
cached_libelle_activite_principale: true,
25+
cached_libelle_categorie_juridique: true,
26+
cached_libelle_tranche_effectif: true,
27+
cached_libelle: true,
28+
cached_nom_complet: true,
29+
cached_tranche_effectifs: true,
30+
created_at: true,
31+
id: true,
32+
siret: true,
33+
updated_at: true,
34+
},
35+
},
36+
user: {
37+
columns: {
38+
created_at: true,
39+
email: true,
40+
family_name: true,
41+
given_name: true,
42+
id: true,
43+
job: true,
44+
last_sign_in_at: true,
45+
phone_number: true,
46+
sign_in_count: true,
47+
},
48+
},
49+
},
50+
});
51+
52+
if (!moderation) throw new NotFoundError("Moderation not found.");
53+
return moderation;
54+
};
55+
}
56+
57+
export type GetModerationWithDetailsHandler = ReturnType<typeof GetModerationWithDetails>;
58+
export type GetModerationWithDetailsDto = Awaited<ReturnType<GetModerationWithDetailsHandler>>;

sources/moderations/repository/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
export * from "./GetDuplicateModerations";
44
export * from "./GetModeration";
55
export * from "./GetModerationById";
6+
export * from "./GetModerationForEmail";
67
export * from "./GetModerationsByUserId";
78
export * from "./GetModerationsList";
9+
export * from "./GetModerationWithDetails";
810
export * from "./RemoveUserFromOrganization";
911
export * from "./UpdateModerationById";

0 commit comments

Comments
 (0)