From 78cce2a81dca58be18ae12cfea3c0d15d76709bf Mon Sep 17 00:00:00 2001 From: Martial Maillot Date: Wed, 4 Sep 2024 13:10:04 +0200 Subject: [PATCH] fix: correction et ajout de certains types manquants (#1464) --- .../types/src/elastic/modeles-de-courrier.ts | 7 ++ shared/types/src/elastic/theme.ts | 18 +++++ .../types/src/hasura/modeles-de-courrier.ts | 17 +++-- .../src/ingester/buildThemes.js | 55 -------------- .../src/ingester/cdtnDocuments.ts | 2 +- .../src/ingester/themes/buildThemes.ts | 74 +++++++++++++++++++ .../src/ingester/themes/fetchThemes.ts | 3 +- .../src/ingester/types/themes.ts | 9 ++- .../src/modules/models/mapModelToDocument.ts | 22 ++++-- 9 files changed, 134 insertions(+), 73 deletions(-) create mode 100644 shared/types/src/elastic/modeles-de-courrier.ts create mode 100644 shared/types/src/elastic/theme.ts delete mode 100644 targets/export-elasticsearch/src/ingester/buildThemes.js create mode 100644 targets/export-elasticsearch/src/ingester/themes/buildThemes.ts diff --git a/shared/types/src/elastic/modeles-de-courrier.ts b/shared/types/src/elastic/modeles-de-courrier.ts new file mode 100644 index 000000000..8a56893cf --- /dev/null +++ b/shared/types/src/elastic/modeles-de-courrier.ts @@ -0,0 +1,7 @@ +import { DocumentElasticWithSource } from "./common"; +import { MailTemplateDoc } from "../hasura"; + +export type MailElasticDocument = DocumentElasticWithSource< + MailTemplateDoc, + "modeles_de_courriers" +>; diff --git a/shared/types/src/elastic/theme.ts b/shared/types/src/elastic/theme.ts new file mode 100644 index 000000000..c74807689 --- /dev/null +++ b/shared/types/src/elastic/theme.ts @@ -0,0 +1,18 @@ +import { DocumentElasticWithSource } from "./common"; + +export type ThemeElasticDocument = DocumentElasticWithSource< + ThemeElastic, + "themes" +>; + +export type ThemeElastic = { + children: ThemeChildren[]; + description?: string; + icon?: string; + position: number; +}; + +export type ThemeChildren = { + label: string; + slug: string; +}; diff --git a/shared/types/src/hasura/modeles-de-courrier.ts b/shared/types/src/hasura/modeles-de-courrier.ts index 103d56610..ae6f78fde 100644 --- a/shared/types/src/hasura/modeles-de-courrier.ts +++ b/shared/types/src/hasura/modeles-de-courrier.ts @@ -6,15 +6,22 @@ export type MailTemplate = HasuraDocument< >; export type MailTemplateDoc = { + meta_title: string; date: string; + type: MailTemplateType; html: string; author: string; filename: string; filesize: number; + intro: string; description: string; - references?: { - url: string; - title: string; - type: string; - }[]; + references?: MailTemplateReference[]; }; + +export type MailTemplateReference = { + url: string; + title: string; + type: "external"; +}; + +export type MailTemplateType = "lettre" | "fichier" | "document"; diff --git a/targets/export-elasticsearch/src/ingester/buildThemes.js b/targets/export-elasticsearch/src/ingester/buildThemes.js deleted file mode 100644 index 0f88e4b8a..000000000 --- a/targets/export-elasticsearch/src/ingester/buildThemes.js +++ /dev/null @@ -1,55 +0,0 @@ -export function buildThemes(themes, getBreadcrumbs) { - return themes.map( - ({ - cdtnId, - id, - slug, - source, - title, - document: { icon, description }, - contentRelations, - parentRelations, - }) => { - const breadcrumbs = getBreadcrumbs(cdtnId); - return { - breadcrumbs: breadcrumbs.slice(0, -1), - cdtnId, - children: themes - .filter( - ({ parentRelations }) => parentRelations[0].parentThemeId === cdtnId - ) - .sort( - ( - { parentRelations: [{ position: positionA }] }, - { parentRelations: [{ position: positionB }] } - ) => positionA - positionB - ) - .map(({ slug, title }) => ({ - label: title, - slug, - })), - description, - icon, - id, - isPublished: true, - position: parentRelations[0].position, - refs: contentRelations - .sort( - ({ position: positionA }, { position: positionB }) => - positionA - positionB - ) - .map(({ content: { cdtnId, document, slug, source, title } }) => ({ - cdtnId, - description: document.description, - url: document.url, - slug, - source, - title, - })), - slug, - source, - title, - }; - } - ); -} diff --git a/targets/export-elasticsearch/src/ingester/cdtnDocuments.ts b/targets/export-elasticsearch/src/ingester/cdtnDocuments.ts index dac44ba72..45571085d 100644 --- a/targets/export-elasticsearch/src/ingester/cdtnDocuments.ts +++ b/targets/export-elasticsearch/src/ingester/cdtnDocuments.ts @@ -12,7 +12,7 @@ import { logger } from "@shared/utils"; import { SOURCES } from "@socialgouv/cdtn-sources"; import { buildGetBreadcrumbs } from "./breadcrumbs"; -import { buildThemes } from "./buildThemes"; +import { buildThemes } from "./themes/buildThemes"; import { getDocumentBySource, getDocumentBySourceWithRelation, diff --git a/targets/export-elasticsearch/src/ingester/themes/buildThemes.ts b/targets/export-elasticsearch/src/ingester/themes/buildThemes.ts new file mode 100644 index 000000000..31e2540c9 --- /dev/null +++ b/targets/export-elasticsearch/src/ingester/themes/buildThemes.ts @@ -0,0 +1,74 @@ +import { Theme, ThemeContentRelation } from "../types/themes"; +import { GetBreadcrumbsFn } from "../breadcrumbs"; +import { ThemeElasticDocument } from "@socialgouv/cdtn-types/build/elastic/theme"; +import { DocumentRef } from "@socialgouv/cdtn-types"; + +export function buildThemes( + themes: Theme[], + getBreadcrumbs: GetBreadcrumbsFn +): ThemeElasticDocument[] { + return themes.map( + ({ + cdtnId, + id, + slug, + source, + title, + document: { icon, description }, + contentRelations, + parentRelations, + }) => { + const breadcrumbs = getBreadcrumbs(cdtnId); + return { + breadcrumbs: breadcrumbs.slice(0, -1), + cdtnId, + text: title, + excludeFromSearch: false, + metaDescription: `Explorez les contenus autour du thème ${title}`, + children: themes + .filter( + ({ parentRelations }) => parentRelations[0].parentThemeId === cdtnId + ) + .sort( + ( + { parentRelations: [{ position: positionA }] }, + { parentRelations: [{ position: positionB }] } + ) => positionA - positionB + ) + .map(({ slug, title }) => ({ + label: title, + slug, + })), + description, + icon, + id, + isPublished: true, + position: parentRelations[0].position, + refs: getContentRelation(contentRelations, getBreadcrumbs), + slug, + source, + title, + }; + } + ); +} + +const getContentRelation = ( + contentRelations: ThemeContentRelation[], + getBreadcrumbs: GetBreadcrumbsFn +): DocumentRef[] => { + return contentRelations + .sort( + ({ position: positionA }, { position: positionB }) => + positionA - positionB + ) + .map(({ content: { cdtnId, description, url, slug, source, title } }) => ({ + cdtnId, + description, + url, + slug, + source, + title, + breadcrumbs: getBreadcrumbs(cdtnId), + })); +}; diff --git a/targets/export-elasticsearch/src/ingester/themes/fetchThemes.ts b/targets/export-elasticsearch/src/ingester/themes/fetchThemes.ts index dc33a765b..26c2daca5 100644 --- a/targets/export-elasticsearch/src/ingester/themes/fetchThemes.ts +++ b/targets/export-elasticsearch/src/ingester/themes/fetchThemes.ts @@ -18,7 +18,8 @@ const graphQLThemesQuery = ` slug source title - document + description: document(path: "description") + url: document(path: "url") } position: data(path: "position") } diff --git a/targets/export-elasticsearch/src/ingester/types/themes.ts b/targets/export-elasticsearch/src/ingester/types/themes.ts index bc1f07f65..94ae3ddea 100644 --- a/targets/export-elasticsearch/src/ingester/types/themes.ts +++ b/targets/export-elasticsearch/src/ingester/types/themes.ts @@ -1,3 +1,5 @@ +import { SourceRoute } from "@socialgouv/cdtn-sources"; + export interface Data { themes: Theme[]; } @@ -6,7 +8,7 @@ export interface Theme { cdtnId: string; id: string; slug: string; - source: string; + source: "themes"; title: string; document: ThemeDocument; contentRelations: ThemeContentRelation[]; @@ -27,9 +29,10 @@ export interface ThemeContentRelation { export interface ThemeContent { cdtnId: string; slug: string; - source: string; + source: SourceRoute; title: string; - document: unknown; + description: string; + url?: string; } export interface ThemeParentRelation { diff --git a/targets/frontend/src/modules/models/mapModelToDocument.ts b/targets/frontend/src/modules/models/mapModelToDocument.ts index aadaf73ad..68766056e 100644 --- a/targets/frontend/src/modules/models/mapModelToDocument.ts +++ b/targets/frontend/src/modules/models/mapModelToDocument.ts @@ -2,12 +2,16 @@ import { format, parseISO } from "date-fns"; import { generateCdtnId } from "@shared/utils"; import slugify from "@socialgouv/cdtn-slugify"; import { Model } from "../models"; -import { HasuraDocument } from "@socialgouv/cdtn-types"; +import { + HasuraDocument, + MailTemplateDoc, + MailTemplateReference, +} from "@socialgouv/cdtn-types"; export const mapModelToDocument = ( data: Model, - document?: HasuraDocument -): HasuraDocument => { + document?: HasuraDocument +): HasuraDocument => { return { cdtn_id: document?.cdtn_id ?? generateCdtnId(data.title), initial_id: data.id!, @@ -25,11 +29,13 @@ export const mapModelToDocument = ( date: format(parseISO(data.displayDate), "dd/MM/yyyy"), author: "Ministère du Travail", references: data.legiReferences - .map((item) => ({ - url: `https://www.legifrance.gouv.fr/codes/article_lc/${item.legiArticle.cid}`, - title: item.legiArticle.label, - type: "external", - })) + .map( + (item): MailTemplateReference => ({ + url: `https://www.legifrance.gouv.fr/codes/article_lc/${item.legiArticle.cid}`, + title: item.legiArticle.label, + type: "external", + }) + ) .concat( data.otherReferences.map((item) => ({ url: item.url ?? "",