Skip to content

Commit

Permalink
fix: correction et ajout de certains types manquants (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot authored Sep 4, 2024
1 parent c3e7e3e commit 78cce2a
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 73 deletions.
7 changes: 7 additions & 0 deletions shared/types/src/elastic/modeles-de-courrier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DocumentElasticWithSource } from "./common";
import { MailTemplateDoc } from "../hasura";

export type MailElasticDocument = DocumentElasticWithSource<
MailTemplateDoc,
"modeles_de_courriers"
>;
18 changes: 18 additions & 0 deletions shared/types/src/elastic/theme.ts
Original file line number Diff line number Diff line change
@@ -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;
};
17 changes: 12 additions & 5 deletions shared/types/src/hasura/modeles-de-courrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
55 changes: 0 additions & 55 deletions targets/export-elasticsearch/src/ingester/buildThemes.js

This file was deleted.

2 changes: 1 addition & 1 deletion targets/export-elasticsearch/src/ingester/cdtnDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
74 changes: 74 additions & 0 deletions targets/export-elasticsearch/src/ingester/themes/buildThemes.ts
Original file line number Diff line number Diff line change
@@ -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),
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const graphQLThemesQuery = `
slug
source
title
document
description: document(path: "description")
url: document(path: "url")
}
position: data(path: "position")
}
Expand Down
9 changes: 6 additions & 3 deletions targets/export-elasticsearch/src/ingester/types/themes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SourceRoute } from "@socialgouv/cdtn-sources";

export interface Data {
themes: Theme[];
}
Expand All @@ -6,7 +8,7 @@ export interface Theme {
cdtnId: string;
id: string;
slug: string;
source: string;
source: "themes";
title: string;
document: ThemeDocument;
contentRelations: ThemeContentRelation[];
Expand All @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions targets/frontend/src/modules/models/mapModelToDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
): HasuraDocument<any> => {
document?: HasuraDocument<MailTemplateDoc>
): HasuraDocument<MailTemplateDoc> => {
return {
cdtn_id: document?.cdtn_id ?? generateCdtnId(data.title),
initial_id: data.id!,
Expand All @@ -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 ?? "",
Expand Down

0 comments on commit 78cce2a

Please sign in to comment.