Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: séparation des anciennes API et des nouvelles #6183

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ exports[`<ModelesDeCourriers /> should render 1`] = `
</p>
</header>
<div
class="sc-5a2c6b8f-0 yOEut sc-496f89b6-2 RBhuQ"
class="sc-5a2c6b8f-0 yOEut sc-f36c6fc-2 cQpmLT"
>
<select
class="sc-5a2c6b8f-2 leEqvE"
Expand Down Expand Up @@ -268,15 +268,15 @@ exports[`<ModelesDeCourriers /> should render 1`] = `
</div>
</div>
<ul
class="sc-ab356c14-0 sc-496f89b6-0 bpNHPl eaxYjO"
class="sc-ab356c14-0 sc-f36c6fc-0 bpNHPl dRUUmx"
>
<h2
class="sc-496f89b6-3 ognPt sc-b879fc2d-0 hyiHle"
class="sc-f36c6fc-3 gWQLPi sc-b879fc2d-0 hyiHle"
>
Embauche et contrat de travail
</h2>
<li
class="sc-496f89b6-1 iHMoBq"
class="sc-f36c6fc-1 kJkExl"
>
<div
class="sc-d47d8018-0 cDsGtD"
Expand Down Expand Up @@ -333,12 +333,12 @@ exports[`<ModelesDeCourriers /> should render 1`] = `
</div>
</li>
<h2
class="sc-496f89b6-3 ognPt sc-b879fc2d-0 hyiHle"
class="sc-f36c6fc-3 gWQLPi sc-b879fc2d-0 hyiHle"
>
Départ de l'entreprise
</h2>
<li
class="sc-496f89b6-1 iHMoBq"
class="sc-f36c6fc-1 kJkExl"
>
<div
class="sc-d47d8018-0 cDsGtD"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import React from "react";
import { replaceArticlesRefs } from "../../../src/lib/replaceArticlesRefs";
import { DsfrLayout } from "../../../src/modules/layout";
import { notFound } from "next/navigation";
import ArticleCodeDuTravail from "../../../src/modules/code-du-travail/articleCodeDuTravail";
import {
ArticleCodeDuTravail,
fetchLegalArticle,
} from "../../../src/modules/code-du-travail";
import { generateDefaultMetadata } from "../../../src/modules/common/metas";
import { getLegalArticleBySlug } from "../../../src/api/modules/legal-articles";
import { getRelatedItems } from "../../../src/api/modules/related-items/service";
import { fetchRelatedItems } from "../../../src/modules/documents";

export async function generateMetadata({ params }) {
const { title, description } = await getArticle(params.slug);
Expand All @@ -23,7 +25,7 @@ export async function generateMetadata({ params }) {
async function Fiche({ params }) {
const { _id, title, description, dateDebut, html, url, notaHtml } =
await getArticle(params.slug);
const relatedItems = await getRelatedItems({ _id }, params.slug);
const relatedItems = await fetchRelatedItems({ _id }, params.slug);

const fixedHtml = replaceArticlesRefs("https://legifrance.gouv.fr", html);
return (
Expand All @@ -45,7 +47,7 @@ async function Fiche({ params }) {
}

const getArticle = async (slug: string) => {
const article = await getLegalArticleBySlug(slug);
const article = await fetchLegalArticle(slug);

if (!article) {
return notFound();
Expand Down
5 changes: 2 additions & 3 deletions packages/code-du-travail-frontend/app/plan-du-site/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Metadata } from "next";
import { DsfrLayout } from "../../src/modules/layout";
import { getSitemapData } from "../../src/api";
import { SiteMap } from "../../src/modules/plan-du-site";
import { fetchSitemapData, SiteMap } from "../../src/modules/plan-du-site";

export const metadata: Metadata = {
title: "Plan du site",
description: "Plan du site du Code du travail numérique",
};

const getSiteMap = async () => {
return getSitemapData();
return fetchSitemapData();
};

async function Index() {
Expand Down
4 changes: 2 additions & 2 deletions packages/code-du-travail-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"pretest:api": "NEXT_PUBLIC_ES_INDEX_PREFIX=cdtn_test NEXT_PUBLIC_APP_ENV=test NODE_ENV=test node -r @swc-node/register scripts/generate-data.ts",
"test": "yarn pretest:api && NEXT_PUBLIC_ES_INDEX_PREFIX=cdtn_test NEXT_PUBLIC_APP_ENV=test NODE_ENV=test jest --silent",
"test:watch": "npm run test -- --watch",
"test:frontend": "NEXT_PUBLIC_APP_ENV=test NODE_ENV=test jest --silent --testPathIgnorePatterns=src/api",
"test:frontend": "NEXT_PUBLIC_APP_ENV=test NODE_ENV=test jest --silent --testPathIgnorePatterns='.*\\.es\\.test\\.ts$'",
"test:frontend:update": "npm run test:frontend -- --updateSnapshot",
"test:api": "npm run test -- --testPathPattern=src/api",
"test:api": "npm run test -- --testPathPattern='.*\\.es\\.test\\.ts$'",
"test:api:update": "npm run test:api -- --updateSnapshot",
"test:update": "npm run test:frontend:update",
"test:e2e": "cypress run",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ export default Page;

export async function getStaticProps() {
try {
let data = await getAllAgreements([
"title",
"shortTitle",
"description",
"url",
"slug",
"source",
"num",
]);
let data = await getAllAgreements();
return { props: { ccs: data }, revalidate: REVALIDATE_TIME };
} catch (error) {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const IntegrationPage = (props): JSX.Element => {
const keys = Object.keys(integrationData);

const getModelesList = async () => {
const modeles = await getAllModeles(["title", "cdtnId"]);
const modeles = await getAllModeles();
return modeles
.map((item) => {
return {
label: item.title,
value: item.cdtnId,
label: item?.title ?? "",
value: item?.cdtnId ?? "",
};
})
?.sort((a, b) => a.label.localeCompare(b.label));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ function Modeles(props) {

export async function getStaticProps() {
try {
const data = await getAllModeles([
"title",
"slug",
"description",
"source",
"breadcrumbs",
"cdtnId",
]);
const data = await getAllModeles();
return { props: { data }, revalidate: REVALIDATE_TIME };
} catch (error) {
console.error(error);
Expand Down
12 changes: 3 additions & 9 deletions packages/code-du-travail-frontend/pages/themes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Metas from "../../src/common/Metas";
import { REVALIDATE_TIME } from "../../src/config";
import { Layout } from "../../src/layout/Layout";
import { LinkedTile } from "../../src/common/tiles/LinkedTile";
import { getRootThemes } from "../../src/api";
import { getAllThemes } from "../../src/api";

const SubThemes = ({ children = [] }) => {
return (
Expand Down Expand Up @@ -73,14 +73,8 @@ const ThemesPage = ({ children = [] }) => (

export async function getStaticProps() {
try {
const data = await getRootThemes([
"icon",
"children",
"title",
"slug",
"position",
]);
return { props: { children: data }, revalidate: REVALIDATE_TIME };
const data = await getAllThemes();
return { props: { children: data.children }, revalidate: REVALIDATE_TIME };
} catch (error) {
console.error(error);
return {
Expand Down
229 changes: 229 additions & 0 deletions packages/code-du-travail-frontend/scripts/data/documents_data.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports[`IDCC return idcc results for boulangerie 1`] = `
"answers.answer.keyword",
],
"_index": "cdtn_test-v2_documents",
"_score": 14.4136915,
"_score": 14.4407215,
"_source": {
"cdtnId": "647c224c9b",
"contributions": true,
Expand All @@ -53,7 +53,7 @@ exports[`IDCC return idcc results for boulangerie 1`] = `
{
"_id": "18",
"_index": "cdtn_test-v2_documents",
"_score": 13.31033,
"_score": 13.33736,
"_source": {
"cdtnId": "16752da066",
"contributions": false,
Expand All @@ -67,7 +67,7 @@ exports[`IDCC return idcc results for boulangerie 1`] = `
},
},
],
"max_score": 14.4136915,
"max_score": 14.4407215,
"total": {
"relation": "eq",
"value": 2,
Expand All @@ -84,7 +84,7 @@ exports[`IDCC return idcc results for pati 1`] = `
"answers.answer.keyword",
],
"_index": "cdtn_test-v2_documents",
"_score": 14.146065,
"_score": 14.173094,
"_source": {
"cdtnId": "647c224c9b",
"contributions": true,
Expand All @@ -100,7 +100,7 @@ exports[`IDCC return idcc results for pati 1`] = `
{
"_id": "18",
"_index": "cdtn_test-v2_documents",
"_score": 13.042704,
"_score": 13.069733,
"_source": {
"cdtnId": "16752da066",
"contributions": false,
Expand All @@ -114,7 +114,7 @@ exports[`IDCC return idcc results for pati 1`] = `
},
},
],
"max_score": 14.146065,
"max_score": 14.173094,
"total": {
"relation": "eq",
"value": 2,
Expand All @@ -131,7 +131,7 @@ exports[`IDCC return idcc results in correct order for banque 1`] = `
"answers.answer.keyword",
],
"_index": "cdtn_test-v2_documents",
"_score": 15.278859,
"_score": 15.306967,
"_source": {
"cdtnId": "a25dfc974f",
"contributions": true,
Expand All @@ -147,7 +147,7 @@ exports[`IDCC return idcc results in correct order for banque 1`] = `
{
"_id": "20",
"_index": "cdtn_test-v2_documents",
"_score": 9.034006,
"_score": 9.072985,
"_source": {
"cdtnId": "ceb38cca3c",
"contributions": false,
Expand All @@ -161,7 +161,7 @@ exports[`IDCC return idcc results in correct order for banque 1`] = `
},
},
],
"max_score": 15.278859,
"max_score": 15.306967,
"total": {
"relation": "eq",
"value": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {

describe("Agreements", () => {
it("getAllAgreements", async () => {
const result = await getAllAgreements(
["title", "shortTitle", "description", "url", "slug", "source", "num"],
"shortTitle"
);
const result = await getAllAgreements();
expect(result).toMatchSnapshot();
});
it("getBySlugsAgreements", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { SOURCES } from "@socialgouv/cdtn-utils";

export const getAllAgreementsQuery = () => {
export const getAllAgreementsWithContributions = () => {
return {
_source: [
"title",
"shortTitle",
"description",
"url",
"slug",
"source",
"num",
],
query: {
bool: {
filter: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,24 @@ import {
getAgreementBySlug,
getAgreementsByIds,
getAgreementsBySlugs,
getAllAgreementsQuery,
getAllAgreementsWithContributions,
} from "./queries";
import { ElasticSearchItem } from "../../types";
import { AgreementDoc, ElasticAgreement } from "@socialgouv/cdtn-types";
import {orderByAlpha} from "../../utils/sort";
import { nonNullable } from "@socialgouv/modeles-social";

export const getAllAgreements = async <K extends keyof ElasticAgreement>(
fields: K[],
sortBy?: K
): Promise<Pick<ElasticAgreement, K>[]> => {
const body = getAllAgreementsQuery();
export const getAllAgreements = async (): Promise<ElasticAgreement[]> => {
const body = getAllAgreementsWithContributions();

const response = await elasticsearchClient.search<Pick<ElasticAgreement, K>>({
...body,
_source: fields,
const response = await elasticsearchClient.search<ElasticAgreement>({
body,
index: elasticDocumentsIndex,
});

const data = response.hits.hits
return response.hits.hits
.map(({ _source }) => _source)
.filter((item) => item !== undefined);
if (sortBy) {
data.sort((a, b) => orderByAlpha(a, b, sortBy));
}
return data;
.filter(nonNullable)
.sort(orderByAlpha);
};

export const getBySlugsAgreements = async (
Expand Down Expand Up @@ -69,3 +62,9 @@ export const getBySlugAgreements = async (slug: string) => {

return { ...response.hits.hits[0]._source };
};

const orderByAlpha = (a, b) => {
return a.shortTitle.localeCompare(b.shortTitle, "fr", {
ignorePunctuation: true,
});
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Contributions getAllContributions 1`] = `
[
{
"agreements": [],
"generic": {
"idcc": "0000",
"slug": "les-conges-pour-evenements-familiaux",
"title": "Les congés pour événements familiaux",
},
},
{
"agreements": [
{
"idcc": "0044",
"slug": "44-quand-le-salarie-a-t-il-droit-a-une-prime-danciennete-quel-est-son-montant",
"title": "Quand le salarié a-t-il droit à une prime d’ancienneté ? Quel est son montant ?",
},
],
"generic": {
"idcc": "0000",
"slug": "quand-le-salarie-a-t-il-droit-a-une-prime-danciennete-quel-est-son-montant",
"title": "Quand le salarié a-t-il droit à une prime d’ancienneté ? Quel est son montant ?",
},
},
]
`;

exports[`Contributions getByIdsContributions 1`] = `
[
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/** @jest-environment node */

import {
getAllContributionsGroupByQuestion,
getByIdsContributions,
getGenericContributionsGroupByThemes,
} from "../service";
import { getAllAgreements } from "../../agreements";

describe("Contributions", () => {
it("getGenericContributions", async () => {
Expand All @@ -15,4 +17,10 @@ describe("Contributions", () => {
const result = await getByIdsContributions(["eba7a4592f"]);
expect(result).toMatchSnapshot();
});

it("getAllContributions", async () => {
const agreements = await getAllAgreements();
const result = await getAllContributionsGroupByQuestion(agreements);
expect(result).toMatchSnapshot();
});
});
Loading
Loading