Skip to content

Commit

Permalink
feat(convention-collective): ajout d'une redirection vers nos pages C…
Browse files Browse the repository at this point in the history
…C à partir d'une url contenant seulement l'IDCC (#5277)
  • Loading branch information
carolineBda authored Jul 13, 2023
1 parent c8a5eaf commit 7532331
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } };
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ const apiIdcc = function createFetcher(query: string): Promise<Agreement[]> {

const searchAgreement = debounce(apiIdcc, 300);

export { searchAgreement };
export { searchAgreement, apiIdcc };

0 comments on commit 7532331

Please sign in to comment.