Skip to content

Commit

Permalink
feat: implement new route for aquagir rex export
Browse files Browse the repository at this point in the history
  • Loading branch information
rtaieb committed Jan 20, 2025
1 parent fff4223 commit 325ce12
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/app/api/get-rex-aquagir/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NextResponse } from "next/server";
import { getAquagirRetoursExperiences } from "@/src/lib/strapi/queries/retoursExperienceQueries";
import { RetourExperience } from "@/src/lib/strapi/types/api/retour-experience";
import join from "lodash/join";
import { getFullUrl, PFMV_ROUTES } from "@/src/helpers/routes";
import { GeoJsonAdresse } from "@/src/components/sourcing/types";
import { getStrapiImageUrl } from "@/src/lib/strapi/strapiClient";
import { captureError } from "@/src/lib/sentry/sentryCustomMessage";

type AquagirRetourExperience = {
id: number;
titre: string;
description: string;
contenu: string;
url: string;
codeInsee: string;
datePublication?: Date;
image: string;
};

const rexToAquagirRex = (rex: RetourExperience): AquagirRetourExperience => ({
id: rex.id,
titre: rex.attributes.titre,
description: rex.attributes.description,
contenu: join(
[
rex.attributes.titre,
rex.attributes.citations.map((citation) => `${citation.auteur} ${citation.texte}`).join(" "),
rex.attributes.description,
rex.attributes.solution_retour_experiences?.data
.map((sol) => ` ${sol.attributes.titre} ${sol.attributes.description} `)
.join(" "),
rex.attributes.situation_avant?.description,
rex.attributes.situation_apres?.description,
rex.attributes.partenaires,
rex.attributes.credits,
],
" ",
),
url: getFullUrl(PFMV_ROUTES.RETOUR_EXPERIENCE(rex.attributes.slug)),
codeInsee: (rex.attributes.location as GeoJsonAdresse).properties.citycode,
datePublication: rex.attributes.publishedAt,
image: getStrapiImageUrl(rex.attributes.image_principale, "medium"),
});

export async function GET() {
const response = await getAquagirRetoursExperiences();
console.log("Appel de l'API Aquagir");
if (response) {
return NextResponse.json(response.map(rexToAquagirRex));
}
captureError("Erreur lors de l'appel à l'API Aquagir");
return NextResponse.json(null);
}
3 changes: 3 additions & 0 deletions src/app/api/revalidate-cache/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export async function POST(request: NextRequest) {
if (payload.model === "webinaire") {
revalidateTag("webinaires");
return NextResponse.json({ message: "Successfully revalidated webinaires" }, { status: 200 });
} else if (payload.model === "retourExperience") {
revalidateTag("get-rex-aquagir");
return NextResponse.json({ message: "Successfully revalidated webinaires" }, { status: 200 });
}
return NextResponse.json({ message: "Nothing to revalidate" }, { status: 200 });
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ export const extractNameInitiales = (name: string) => {
const match = name.match(/^[^\s-]+|\S+$/g);
return match ? match.map((word) => word[0].toUpperCase()).join("") : "";
};

export const isBoolean = (param: any): boolean => {
return typeof param == "boolean";
};
7 changes: 5 additions & 2 deletions src/lib/strapi/queries/commonStrapiFilters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Media } from "@/src/lib/strapi/types/common/Media";
import { isBoolean } from "@/src/helpers/common";

type StrapiEqFilter = { attribute: string; value: string; operator: "eq"; relation: false };
type StrapiEqFilter = { attribute: string; value: string | boolean; operator: "eq"; relation: false };
type StrapiInFilter = { attribute: string; value: string[] | number[]; operator: "in"; relation: false };
type StrapiRelationFilter = { attribute: string; operator: "null" | "notNull"; relation: true };
type StrapiSortFilter = { attribute: string; order: "asc" | "desc" };
Expand Down Expand Up @@ -39,7 +40,9 @@ export class StrapiFilter {
.map((f) => {
switch (f.relation) {
case false:
return ` {${f.attribute}: {${f.operator}: ${JSON.stringify(f.value)}}} `;
return ` {${f.attribute}: {${f.operator}: ${
isBoolean(f.value) ? f.value : JSON.stringify(f.value)
}}} `;
case true:
return ` {${f.attribute} : {id : {${f.operator} : true}}} `;
}
Expand Down
59 changes: 59 additions & 0 deletions src/lib/strapi/queries/retoursExperienceQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,45 @@ const GET_RETOUR_EXPERIENCE_CARD_DATA = (
}
}`;

const GET_RETOUR_EXPERIENCE_FOR_AQUAGIR_DATA = (strapiFilter: StrapiFilter) => ` ${STRAPI_IMAGE_FRAGMENT} query {
retourExperiences ${strapiFilter.wholeFilterString()} {
data {
id
attributes {
titre
description
slug
location
citations {
auteur
texte
}
situation_avant {
description
}
situation_apres {
description
}
solution_retour_experiences {
data {
id
attributes {
titre
description
}
}
}
partenaires
credits
publishedAt
image_principale {
...ImageInfo
}
}
}
}
}`;

const GET_RETOUR_EXPERIENCE_CARD_DATA_WITH_CONTACTS = (
strapiFilter: StrapiFilter,
) => ` ${RETOUR_EXPERIENCE_WITH_CONTACTS} query {
Expand Down Expand Up @@ -144,6 +183,7 @@ export async function getRetoursExperiencesWithContacts(): Promise<RetourExperie
)?.retourExperiences as APIResponseCollection<RetourExperience>;
return safeReturnStrapiEntities(apiResponse);
}

export async function getRetoursExperiencesWithContactsById(id: string): Promise<RetourExperience | null> {
const filter = new StrapiFilter(true, [{ attribute: "id", operator: "eq", value: id, relation: false }]);
const apiResponse = (
Expand All @@ -161,3 +201,22 @@ export async function getAllCompleteRetoursExperiences(): Promise<RetourExperien
)?.retourExperiences as APIResponseCollection<RetourExperience>;
return safeReturnStrapiEntities(apiResponse);
}

export async function getAquagirRetoursExperiences(): Promise<RetourExperience[] | null> {
const filter = new StrapiFilter(
true,
[
{
attribute: "export_aquagir",
value: true,
operator: "eq",
relation: false,
},
],
{ attribute: "rank", order: "asc" },
);
const apiResponse = (
await strapiGraphQLCall(GET_RETOUR_EXPERIENCE_FOR_AQUAGIR_DATA(filter), { tag: "get-rex-aquagir" })
)?.retourExperiences as APIResponseCollection<RetourExperience>;
return safeReturnStrapiEntities(apiResponse);
}

0 comments on commit 325ce12

Please sign in to comment.