Skip to content

Commit

Permalink
fix: partenaire multi client api-depot
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed May 6, 2024
1 parent 1a3112c commit f07aecd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 51 deletions.
28 changes: 16 additions & 12 deletions components/api-depot/client-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ interface ClientItemProps {
client: ClientApiDepotType;
mandataire: MandataireApiDepotType;
chefDeFile: ChefDeFileApiDepotType;
partenaire: PartenaireDeLaChartType;
partenaires: PartenaireDeLaChartType[];
isDemo: boolean;
}

const ClientItem = ({
client,
mandataire,
chefDeFile,
partenaire,
partenaires,
isDemo,
}: ClientItemProps) => (
<tr>
Expand All @@ -47,16 +47,20 @@ const ClientItem = ({
/>
</td>
<td className="fr-col fr-my-1v">
{partenaire ? (
<Link
legacyBehavior
passHref
href={{
pathname: `/partenaires-de-la-charte/${partenaire._id}`,
}}
>
{partenaire.name}
</Link>
{partenaires && partenaires.length > 0 ? (
partenaires.map((partenaire) => (
<div key={partenaire._id}>
<Link
legacyBehavior
passHref
href={{
pathname: `/partenaires-de-la-charte/${partenaire._id}`,
}}
>
{partenaire.name}
</Link>
</div>
))
) : (
<Badge severity="warning">Non partenaire</Badge>
)}
Expand Down
5 changes: 3 additions & 2 deletions components/api-depot/clients-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
/>
Expand Down
27 changes: 11 additions & 16 deletions components/partenaires-de-la-charte/partenaire-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -102,7 +101,7 @@ const newPartenaireForm = {
infos: "",
perimeter: "",
dataGouvOrganizationId: "",
apiDepotClientId: "",
apiDepotClientId: [],
};

export const PartenaireForm = ({
Expand All @@ -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 (
Expand Down Expand Up @@ -483,12 +472,18 @@ export const PartenaireForm = ({
/>
</div>
<div className="fr-col-6">
<AutocompleteInput
id={uniqueId()}

<MultiSelectInput
label="Client api depot ID"
value={formData.apiDepotClientId}
options={optionClients}
value={clientApiDepotLabel}
onChange={handleEdit("apiDepotClientId")}
placeholder="Sélectionnez un ou plusieurs client de l'api-depot"
onChange={(apiDepotClientId) => {
setFormData((state) => ({
...state,
apiDepotClientId,
}));
}}
/>
</div>
</div>
Expand Down
25 changes: 6 additions & 19 deletions components/partenaires-de-la-charte/partenaire-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,14 @@ export const PartenaireItem = ({
</td>
<td className="fr-col fr-my-1v">
{dataGouvOrganizationId && (
<Link
legacyBehavior
passHref
href={{
pathname: `/moissonneur-bal/organizations/${dataGouvOrganizationId}`,
}}
>
<Button priority="secondary">Moissonneur</Button>
</Link>
<Badge severity="info" noIcon>
Organisation Moissonnée
</Badge>
)}
{apiDepotClientId && (
<Link
legacyBehavior
passHref
href={{
pathname: `/api-depot/client`,
query: { clientId: apiDepotClientId },
}}
>
<Button priority="secondary">Api depot</Button>
</Link>
<Badge severity="new" noIcon>
{apiDepotClientId.length} Client(s) api-depot
</Badge>
)}
</td>
<td className="fr-col fr-my-1v">
Expand Down
2 changes: 1 addition & 1 deletion server/lib/partenaire-de-la-charte/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
27 changes: 27 additions & 0 deletions server/migrations/multi-apiDepotClientId.js
Original file line number Diff line number Diff line change
@@ -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)
})
2 changes: 1 addition & 1 deletion types/partenaire-de-la-charte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type PartenaireDeLaChartType = {
charteURL?: string;
signatureDate?: string;
dataGouvOrganizationId?: string;
apiDepotClientId: string;
apiDepotClientId: string[];
};

export type PartenaireDeLaCharteCommuneType = PartenaireDeLaChartType & {
Expand Down

0 comments on commit f07aecd

Please sign in to comment.