From f07aecd28ff60502dc3fbf5da945a2f88c57cedb Mon Sep 17 00:00:00 2001 From: Fufeck Date: Mon, 29 Apr 2024 12:18:15 +0200 Subject: [PATCH] fix: partenaire multi client api-depot --- components/api-depot/client-item.tsx | 28 +++++++++++-------- components/api-depot/clients-list.tsx | 5 ++-- .../partenaire-form.tsx | 27 ++++++++---------- .../partenaire-item.tsx | 25 ++++------------- server/lib/partenaire-de-la-charte/schemas.js | 2 +- server/migrations/multi-apiDepotClientId.js | 27 ++++++++++++++++++ types/partenaire-de-la-charte.ts | 2 +- 7 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 server/migrations/multi-apiDepotClientId.js diff --git a/components/api-depot/client-item.tsx b/components/api-depot/client-item.tsx index b8d723f..1754fc7 100644 --- a/components/api-depot/client-item.tsx +++ b/components/api-depot/client-item.tsx @@ -12,7 +12,7 @@ interface ClientItemProps { client: ClientApiDepotType; mandataire: MandataireApiDepotType; chefDeFile: ChefDeFileApiDepotType; - partenaire: PartenaireDeLaChartType; + partenaires: PartenaireDeLaChartType[]; isDemo: boolean; } @@ -20,7 +20,7 @@ const ClientItem = ({ client, mandataire, chefDeFile, - partenaire, + partenaires, isDemo, }: ClientItemProps) => ( @@ -47,16 +47,20 @@ const ClientItem = ({ /> - {partenaire ? ( - - {partenaire.name} - + {partenaires && partenaires.length > 0 ? ( + partenaires.map((partenaire) => ( +
+ + {partenaire.name} + +
+ )) ) : ( Non partenaire )} diff --git a/components/api-depot/clients-list.tsx b/components/api-depot/clients-list.tsx index 9634dbd..eb5dc3d 100644 --- a/components/api-depot/clients-list.tsx +++ b/components/api-depot/clients-list.tsx @@ -92,8 +92,9 @@ const ClientsList = ({ isDemo = false }: ClientsListProps) => { chefDeFile={data.chefsDeFile.find( ({ _id }) => _id === client.chefDeFile )} - partenaire={data.partenaires.find( - ({ apiDepotClientId }) => apiDepotClientId === client._id + partenaires={data.partenaires.filter( + ({ apiDepotClientId }) => + apiDepotClientId?.includes(client._id) )} isDemo={isDemo} /> diff --git a/components/partenaires-de-la-charte/partenaire-form.tsx b/components/partenaires-de-la-charte/partenaire-form.tsx index 0c682ae..5182c3c 100644 --- a/components/partenaires-de-la-charte/partenaire-form.tsx +++ b/components/partenaires-de-la-charte/partenaire-form.tsx @@ -21,7 +21,6 @@ import { MultiSelectInput } from "../multi-select-input"; import type { CommuneType } from "../commune-input"; import { CommuneInput } from "../commune-input"; import SelectInput from "@/components/select-input"; -import Tooltip from "@/components/tooltip"; import { capitalize } from "@/lib/util/string"; import AutocompleteInput from "../autocomplete-input"; import { getClients } from "@/lib/api-depot"; @@ -102,7 +101,7 @@ const newPartenaireForm = { infos: "", perimeter: "", dataGouvOrganizationId: "", - apiDepotClientId: "", + apiDepotClientId: [], }; export const PartenaireForm = ({ @@ -128,16 +127,6 @@ export const PartenaireForm = ({ >([]); const isCandidate = data && !data.signatureDate; - const clientApiDepotLabel = useMemo(() => { - if (optionClients.length > 0 && formData.apiDepotClientId) { - return ( - optionClients.find(({ value }) => value === formData.apiDepotClientId) - ?.label || formData.apiDepotClientId - ); - } - return formData.apiDepotClientId; - }, [optionClients, formData.apiDepotClientId]); - const dataGouvOrganizationLabel = useMemo(() => { if (optionOrganizations.length > 0 && formData.dataGouvOrganizationId) { return ( @@ -483,12 +472,18 @@ export const PartenaireForm = ({ />
- { + setFormData((state) => ({ + ...state, + apiDepotClientId, + })); + }} />
diff --git a/components/partenaires-de-la-charte/partenaire-item.tsx b/components/partenaires-de-la-charte/partenaire-item.tsx index 4c3e162..904eceb 100644 --- a/components/partenaires-de-la-charte/partenaire-item.tsx +++ b/components/partenaires-de-la-charte/partenaire-item.tsx @@ -55,27 +55,14 @@ export const PartenaireItem = ({ {dataGouvOrganizationId && ( - - - + + Organisation Moissonnée + )} {apiDepotClientId && ( - - - + + {apiDepotClientId.length} Client(s) api-depot + )} diff --git a/server/lib/partenaire-de-la-charte/schemas.js b/server/lib/partenaire-de-la-charte/schemas.js index f8f3b5a..ed21f08 100644 --- a/server/lib/partenaire-de-la-charte/schemas.js +++ b/server/lib/partenaire-de-la-charte/schemas.js @@ -12,7 +12,7 @@ const createBaseSchema = { codeDepartement: { isRequired: false, type: "array" }, services: { isRequired: false, type: "array" }, dataGouvOrganizationId: { isRequired: false, type: "string" }, - apiDepotClientId: { isRequired: false, type: "string" }, + apiDepotClientId: { isRequired: false, type: "array" }, }; const createCommuneSchema = { diff --git a/server/migrations/multi-apiDepotClientId.js b/server/migrations/multi-apiDepotClientId.js new file mode 100644 index 0000000..6bf78c2 --- /dev/null +++ b/server/migrations/multi-apiDepotClientId.js @@ -0,0 +1,27 @@ +const mongoClient = require('../utils/mongo-client') + +const collectionName = "partenaires-de-la-charte"; + +async function main() { + await mongoClient.connect() + + const pdlcs = await mongoClient.db.collection(collectionName).find( + {apiDepotClientId: {$ne: null}}, + ).toArray() + + for (const pdlc of pdlcs) { + await mongoClient.db.collection(collectionName).updateOne( + {_id: pdlc._id}, + {$set: {apiDepotClientId: [pdlc.apiDepotClientId]}}, + ) + } + + await mongoClient.disconnect() +} + +main().catch(error => { + console.error(error) + process.exit(1) +}).then(() => { + process.exit(0) +}) diff --git a/types/partenaire-de-la-charte.ts b/types/partenaire-de-la-charte.ts index e87ca20..ad6064f 100644 --- a/types/partenaire-de-la-charte.ts +++ b/types/partenaire-de-la-charte.ts @@ -35,7 +35,7 @@ export type PartenaireDeLaChartType = { charteURL?: string; signatureDate?: string; dataGouvOrganizationId?: string; - apiDepotClientId: string; + apiDepotClientId: string[]; }; export type PartenaireDeLaCharteCommuneType = PartenaireDeLaChartType & {