From ba136baa34dff36d731d4aee2efceaa9f65390cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Ta=C3=AFeb?= Date: Mon, 9 Dec 2024 10:58:40 +0100 Subject: [PATCH] chore : change sourcing_cms jsonb[] field to sourcing_rex jsonb field --- .../migration.sql | 9 +++++++++ prisma/schema.prisma | 2 +- .../generate-sourcing-contacts-csv-action.ts | 18 ++++++++++-------- .../update-rex-contact-in-projet-action.ts | 13 +++++++------ .../contacts/sourcing-contact-save-button.tsx | 4 ++-- src/components/sourcing/sourcing.tsx | 2 +- .../tableau-de-bord/tableau-de-bord-suivi.tsx | 6 +++++- src/lib/prisma/prismaProjetQueries.ts | 6 +++--- 8 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 prisma/migrations/20241209092748_add_sourcing_rex_column_in_projet_table/migration.sql diff --git a/prisma/migrations/20241209092748_add_sourcing_rex_column_in_projet_table/migration.sql b/prisma/migrations/20241209092748_add_sourcing_rex_column_in_projet_table/migration.sql new file mode 100644 index 00000000..91c8d628 --- /dev/null +++ b/prisma/migrations/20241209092748_add_sourcing_rex_column_in_projet_table/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - You are about to drop the column `sourcing_cms` on the `projet` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "projet" DROP COLUMN "sourcing_cms", +ADD COLUMN "sourcing_rex" JSONB; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ec2e471b..d6e9a534 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -185,7 +185,7 @@ model projet { deleter User? @relation(name: "projet_deleter", fields: [deleted_by], references: [id]) users user_projet[] is_public Boolean? - sourcing_cms Json[] + sourcing_rex Json? sourcing_user_projets projet_sourcing_contact[] @relation("sourcing_projet") } diff --git a/src/actions/projets/generate-sourcing-contacts-csv-action.ts b/src/actions/projets/generate-sourcing-contacts-csv-action.ts index 77984295..306b0768 100644 --- a/src/actions/projets/generate-sourcing-contacts-csv-action.ts +++ b/src/actions/projets/generate-sourcing-contacts-csv-action.ts @@ -26,8 +26,8 @@ export const generateSourcingContactsCsvAction = async ( const inProgressContacts = projet?.sourcing_user_projets?.map((c) => userProjetToSourcingContactWithProjet(c.sourced_user_projet)) || []; - const rexContactIds = projet?.sourcing_cms as RexContactId[]; - const uniqueRexContactIds = Array.from(new Set(rexContactIds.map((r) => r.rexId))); + const rexContactIds = projet?.sourcing_rex as RexContactId[] | null; + const uniqueRexContactIds = Array.from(new Set(rexContactIds?.map((r) => r.rexId))); const allRex = await Promise.all( uniqueRexContactIds.map((rexContactId) => getRetoursExperiencesWithContactsById(rexContactId.toString())), ); @@ -36,12 +36,14 @@ export const generateSourcingContactsCsvAction = async ( const currentRexContacts = currentRex?.attributes.contacts as unknown as StrapiSourcingContact[]; if (!currentRexContacts) return []; - return rexContactIds - .filter((rexContactId) => rexContactId.rexId === currentRex?.id) - .map((rexContactId) => currentRexContacts.find((contact) => contact.id === rexContactId.contactId)) - .filter((contact): contact is StrapiSourcingContact => contact !== undefined) - .map((contact) => currentRex && strapiContactToSourcingContact(contact, currentRex)) - .filter((contact): contact is SourcingContact => contact !== null); + return ( + rexContactIds + ?.filter((rexContactId) => rexContactId.rexId === currentRex?.id) + .map((rexContactId) => currentRexContacts.find((contact) => contact.id === rexContactId.contactId)) + .filter((contact): contact is StrapiSourcingContact => contact !== undefined) + .map((contact) => currentRex && strapiContactToSourcingContact(contact, currentRex)) + .filter((contact): contact is SourcingContact => contact !== null) || [] + ); }); const allContacts = [...inProgressContacts, ...rexContacts]; diff --git a/src/actions/projets/update-rex-contact-in-projet-action.ts b/src/actions/projets/update-rex-contact-in-projet-action.ts index 54f669ef..49c06b71 100644 --- a/src/actions/projets/update-rex-contact-in-projet-action.ts +++ b/src/actions/projets/update-rex-contact-in-projet-action.ts @@ -4,7 +4,7 @@ import { ResponseAction } from "../actions-types"; import { customCaptureException } from "@/src/lib/sentry/sentryCustomMessage"; import { ProjetWithRelations } from "@/src/lib/prisma/prismaCustomTypes"; import { PermissionManager } from "@/src/helpers/permission-manager"; -import { getProjetWithRelationsById, updateSourcingCmsProjet } from "@/src/lib/prisma/prismaProjetQueries"; +import { getProjetWithRelationsById, updateSourcingRexProjet } from "@/src/lib/prisma/prismaProjetQueries"; import isEqual from "lodash/isEqual"; import { RexContactId } from "@/src/components/sourcing/types"; @@ -29,13 +29,14 @@ export const updateRexContactInProjetAction = async ( return { type: "error", message: "PROJET_UPDATE_UNAUTHORIZED" }; } - let newSourcingCms = (projetToUpdate.sourcing_cms as RexContactId[]).filter( - (savedContact) => !isEqual(savedContact, rexContactId), - ); + let newSourcingRex = + (projetToUpdate.sourcing_rex as RexContactId[] | null)?.filter( + (savedContact) => !isEqual(savedContact, rexContactId), + ) || []; if (typeUpdate === "add") { - newSourcingCms = [...newSourcingCms, rexContactId]; + newSourcingRex = [...newSourcingRex, rexContactId]; } - projetToUpdate = await updateSourcingCmsProjet(projetId, newSourcingCms); + projetToUpdate = await updateSourcingRexProjet(projetId, newSourcingRex); return { type: "success", projet: projetToUpdate }; } catch (e) { diff --git a/src/components/sourcing/contacts/sourcing-contact-save-button.tsx b/src/components/sourcing/contacts/sourcing-contact-save-button.tsx index 5334eb34..65cceefd 100644 --- a/src/components/sourcing/contacts/sourcing-contact-save-button.tsx +++ b/src/components/sourcing/contacts/sourcing-contact-save-button.tsx @@ -31,12 +31,12 @@ export const SourcingContactSaveButton = ({ projetId, contact, className }: Sour if (contact.type === "rex" && projet) { setSaved( - (projet.sourcing_cms as RexContactId[]).some((savedRexContactId) => + (projet.sourcing_rex as RexContactId[] | null)?.some((savedRexContactId) => isEqual(savedRexContactId, { rexId: contact.id.rexId, contactId: contact.id.contactId, }), - ), + ) || false, ); } else if (contact.type === "in-progress") { setSaved( diff --git a/src/components/sourcing/sourcing.tsx b/src/components/sourcing/sourcing.tsx index 856e35e9..5c29f13e 100644 --- a/src/components/sourcing/sourcing.tsx +++ b/src/components/sourcing/sourcing.tsx @@ -20,7 +20,7 @@ export const Sourcing = () => { const currentProjet = useProjetsStore((state) => state.getCurrentProjet()); const isLecteur = useIsLecteur(currentProjet?.id); const inProgressProjetContacts = currentProjet?.sourcing_user_projets; - const rexContactIds = currentProjet?.sourcing_cms as RexContactId[] | undefined; + const rexContactIds = currentProjet?.sourcing_rex as RexContactId[] | undefined; const { contactTypeFilters, diff --git a/src/components/tableau-de-bord/tableau-de-bord-suivi.tsx b/src/components/tableau-de-bord/tableau-de-bord-suivi.tsx index ed468a9e..90446fd8 100644 --- a/src/components/tableau-de-bord/tableau-de-bord-suivi.tsx +++ b/src/components/tableau-de-bord/tableau-de-bord-suivi.tsx @@ -12,6 +12,7 @@ import { getLastCompletedEstimation } from "@/src/helpers/estimation"; // eslint-disable-next-line max-len import { TableauDeBordSuiviWithEstimation } from "@/src/components/tableau-de-bord/tableau-de-bord-suivi-card-with-estimation"; import { TableauDeBordMaturite } from "./tableau-de-bord-maturite"; +import { RexContactId } from "@/src/components/sourcing/types"; export const TableauDeBordSuivi = () => { return ( @@ -87,7 +88,10 @@ const cards: TableauDeBordSuiviCardProps[] = [ title: "Annuaire des projets Plus fraƮche ma ville", index: 6, progress: (projet: ProjetWithRelations | undefined) => - (projet?.sourcing_user_projets?.length || 0) > 0 || (projet?.sourcing_cms.length || 0) > 0 ? "100" : "0", + (projet?.sourcing_user_projets?.length || 0) > 0 || + ((projet?.sourcing_rex as RexContactId[] | null)?.length || 0) > 0 + ? "100" + : "0", disabled: false, type: "sourcing", picto: , diff --git a/src/lib/prisma/prismaProjetQueries.ts b/src/lib/prisma/prismaProjetQueries.ts index a1f92b70..9a545472 100644 --- a/src/lib/prisma/prismaProjetQueries.ts +++ b/src/lib/prisma/prismaProjetQueries.ts @@ -412,9 +412,9 @@ export const getPublicProjetById = async (projetId: number): Promise => { return prismaClient.projet.update({ where: { @@ -422,7 +422,7 @@ export const updateSourcingCmsProjet = ( deleted_at: null, }, data: { - sourcing_cms: sourcingCms, + sourcing_rex: sourcingRex, }, include: projetIncludes, });