diff --git a/packages/code-du-travail-frontend/cypress/integration/conventions-collectives.spec.ts b/packages/code-du-travail-frontend/cypress/integration/conventions-collectives.spec.ts index 5dfb8f19c8..763dd14949 100644 --- a/packages/code-du-travail-frontend/cypress/integration/conventions-collectives.spec.ts +++ b/packages/code-du-travail-frontend/cypress/integration/conventions-collectives.spec.ts @@ -16,4 +16,20 @@ describe("Conventions collectives", () => { "/convention-collective/2941-aide-accompagnement-soins-et-services-a-domicile-bad" ); }); + it("je suis redirigé vers la cc si je mets seulement l'idcc dans l'url", () => { + cy.visit("/convention-collective/0029"); + cy.url().should( + "include", + "/convention-collective/29-hospitalisation-privee-etablissements-prives-dhospitalisation-de-soins-d" + ); + }); + it("je vois une 404 si l'iddc n'existe pas", () => { + cy.request({ + failOnStatusCode: false, + method: "GET", + url: "/convention-collective/1234", + }).then((response) => { + expect(response.status).to.equal(404); + }); + }); }); diff --git a/packages/code-du-travail-frontend/pages/convention-collective/[slug].tsx b/packages/code-du-travail-frontend/pages/convention-collective/[slug].tsx index 13c691c513..3580057458 100644 --- a/packages/code-du-travail-frontend/pages/convention-collective/[slug].tsx +++ b/packages/code-du-travail-frontend/pages/convention-collective/[slug].tsx @@ -11,6 +11,7 @@ import Convention from "../../src/conventions/Convention"; import { Layout } from "../../src/layout/Layout"; import { handleError } from "../../src/lib/fetch-error"; import { SITE_URL } from "../../src/config"; +import { apiIdcc } from "../../src/conventions/Search/api/agreement.service"; interface Props { convention; @@ -80,12 +81,20 @@ function ConventionCollective(props: Props): JSX.Element { ); } +const IDCC_ONLY = /^\d{2,4}$/; export const getServerSideProps = async ({ query }) => { - const responseContainer = await fetch(`${SITE_URL}/api/agreements/${query.slug}`); - if (!responseContainer.ok) { - return handleError(responseContainer); + if (IDCC_ONLY.test(query.slug)) { + const conventions = await apiIdcc(query.slug.padStart(4, "0")); + if (!conventions.length) { + return { notFound: true }; + } + return { redirect: { destination: conventions[0].slug, permanent: true } }; } - const convention = await responseContainer.json(); + const res = await fetch(`${SITE_URL}/api/agreements/${query.slug}`); + if (!res.ok) { + return handleError(res); + } + const convention = await res.json(); return { props: { convention } }; }; diff --git a/packages/code-du-travail-frontend/src/conventions/Search/api/agreement.service.ts b/packages/code-du-travail-frontend/src/conventions/Search/api/agreement.service.ts index 146ee64f7f..cf85bb6175 100644 --- a/packages/code-du-travail-frontend/src/conventions/Search/api/agreement.service.ts +++ b/packages/code-du-travail-frontend/src/conventions/Search/api/agreement.service.ts @@ -32,4 +32,4 @@ const apiIdcc = function createFetcher(query: string): Promise { const searchAgreement = debounce(apiIdcc, 300); -export { searchAgreement }; +export { searchAgreement, apiIdcc };