diff --git a/src/app/api/get-rex-aquagir/route.ts b/src/app/api/get-rex-aquagir/route.ts new file mode 100644 index 00000000..7c21d60c --- /dev/null +++ b/src/app/api/get-rex-aquagir/route.ts @@ -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); +} diff --git a/src/app/api/revalidate-cache/route.ts b/src/app/api/revalidate-cache/route.ts index 60c330a4..bea57b80 100644 --- a/src/app/api/revalidate-cache/route.ts +++ b/src/app/api/revalidate-cache/route.ts @@ -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) { diff --git a/src/helpers/common.tsx b/src/helpers/common.tsx index 34eed878..76c0f5b7 100644 --- a/src/helpers/common.tsx +++ b/src/helpers/common.tsx @@ -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"; +}; diff --git a/src/lib/strapi/queries/commonStrapiFilters.ts b/src/lib/strapi/queries/commonStrapiFilters.ts index cb2052be..591fe2ae 100644 --- a/src/lib/strapi/queries/commonStrapiFilters.ts +++ b/src/lib/strapi/queries/commonStrapiFilters.ts @@ -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" }; @@ -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}}} `; } diff --git a/src/lib/strapi/queries/retoursExperienceQueries.ts b/src/lib/strapi/queries/retoursExperienceQueries.ts index f49b9027..1d1dcd7a 100644 --- a/src/lib/strapi/queries/retoursExperienceQueries.ts +++ b/src/lib/strapi/queries/retoursExperienceQueries.ts @@ -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 { @@ -144,6 +183,7 @@ export async function getRetoursExperiencesWithContacts(): Promise; return safeReturnStrapiEntities(apiResponse); } + export async function getRetoursExperiencesWithContactsById(id: string): Promise { const filter = new StrapiFilter(true, [{ attribute: "id", operator: "eq", value: id, relation: false }]); const apiResponse = ( @@ -161,3 +201,22 @@ export async function getAllCompleteRetoursExperiences(): Promise; return safeReturnStrapiEntities(apiResponse); } + +export async function getAquagirRetoursExperiences(): Promise { + 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; + return safeReturnStrapiEntities(apiResponse); +}