Skip to content

Commit

Permalink
chore : change sourcing_cms jsonb[] field to sourcing_rex jsonb field
Browse files Browse the repository at this point in the history
  • Loading branch information
rtaieb committed Dec 9, 2024
1 parent 8f345ef commit ba136ba
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
18 changes: 10 additions & 8 deletions src/actions/projets/generate-sourcing-contacts-csv-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
);
Expand All @@ -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];
Expand Down
13 changes: 7 additions & 6 deletions src/actions/projets/update-rex-contact-in-projet-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/components/sourcing/sourcing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/components/tableau-de-bord/tableau-de-bord-suivi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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: <PictoTableauDeBordSelector pictoId="sourcing" className="w-20" />,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/prisma/prismaProjetQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,17 @@ export const getPublicProjetById = async (projetId: number): Promise<ProjetWithP
});
};

export const updateSourcingCmsProjet = (
export const updateSourcingRexProjet = (
projetId: number,
sourcingCms: RexContactId[],
sourcingRex: RexContactId[],
): Promise<ProjetWithRelations | null> => {
return prismaClient.projet.update({
where: {
id: projetId,
deleted_at: null,
},
data: {
sourcing_cms: sourcingCms,
sourcing_rex: sourcingRex,
},
include: projetIncludes,
});
Expand Down

0 comments on commit ba136ba

Please sign in to comment.