From e1127cd0a75f89111fd8aa57acf6cc067cc5074c Mon Sep 17 00:00:00 2001 From: baudointran Date: Thu, 8 Feb 2024 15:31:33 +0100 Subject: [PATCH 1/2] fix: fonction get files return files --- src/dossier/dossier.ts | 29 ++++------------------------- test/ds-api.e2e-spec.ts | 27 ++++++--------------------- 2 files changed, 10 insertions(+), 46 deletions(-) diff --git a/src/dossier/dossier.ts b/src/dossier/dossier.ts index 43483a1..2950520 100644 --- a/src/dossier/dossier.ts +++ b/src/dossier/dossier.ts @@ -76,20 +76,12 @@ export const getFilesFromDossier = async ( }, }; }; -type champType = { - champ: { - id?: string; - files: Partial[]; - dossierId: string; - dossierNumber: number; - }; -}; export const getOneFileFromDossier = async ( client: GraphQLClient, idDossier: number, idChamp: string, -): Promise => { +): Promise => { const dossier = await graphQlRequest( client, getOneFileFromDossierQuery, @@ -111,20 +103,13 @@ export const getOneFileFromDossier = async ( })) || []), ].flat()[0]; - return { - champ: { - id: champ.id, - files: champ.files, - dossierId: dossier.dossier.id, - dossierNumber: dossier.dossier.number, - }, - }; + return champ.files; }; export const getAttestationFromDossier = async ( client: GraphQLClient, idDossier: number, -): Promise => { +): Promise => { const dossier = await graphQlRequest( client, getOneFileFromDossierQuery, @@ -134,11 +119,5 @@ export const getAttestationFromDossier = async ( }, ); - return { - champ: { - dossierId: dossier.dossier.id, - dossierNumber: dossier.dossier.number, - files: dossier.dossier.attestation ? [dossier.dossier.attestation] : [], - }, - }; + return dossier.dossier.attestation ? [dossier.dossier.attestation] : []; }; diff --git a/test/ds-api.e2e-spec.ts b/test/ds-api.e2e-spec.ts index 2813e0e..f802055 100644 --- a/test/ds-api.e2e-spec.ts +++ b/test/ds-api.e2e-spec.ts @@ -36,12 +36,9 @@ describe.skip("ds api", () => { }); it("get one files from dossiers in annatation", async () => { - const response = await dsApiClient.dossierFile(221, "Q2hhbXAtMTQ1Ng=="); - expect(response?.champ).toMatchObject({ - id: "Q2hhbXAtMTQ1Ng==", - dossierNumber: 221, - }); - expect(response?.champ.files).toHaveLength(1); + const response = await dsApiClient.dossierFile(221, "Q2hhbXAtMTQ1MA=="); + console.log(response); + expect(response).toHaveLength(1); }); it("get one files from dossiers in repetable of annotation ", async () => { @@ -49,28 +46,16 @@ describe.skip("ds api", () => { 221, "Q2hhbXAtMTQ1OHwwMUhOWVkyMENWR1pNMUtUOEI2RTk5Q1QyMg==", ); - expect(response?.champ).toMatchObject({ - id: "Q2hhbXAtMTQ1OHwwMUhOWVkyMENWR1pNMUtUOEI2RTk5Q1QyMg==", - dossierNumber: 221, - }); - expect(response?.champ.files).toHaveLength(1); + expect(response).toHaveLength(1); }); it("get one files from dossiers in message ", async () => { const response = await dsApiClient.dossierFile(221, "Q29tbWVudGFpcmUtMjM3"); - expect(response?.champ).toMatchObject({ - id: "Q29tbWVudGFpcmUtMjM3", - dossierNumber: 221, - }); - expect(response?.champ.files).toHaveLength(1); + expect(response).toHaveLength(1); }); it("get attestation from dossiers ", async () => { const response = await dsApiClient.dossierAttestation(222); - expect(response?.champ.id).toBeUndefined(); - expect(response?.champ).toMatchObject({ - dossierNumber: 222, - }); - expect(response?.champ.files).toHaveLength(1); + expect(response).toHaveLength(1); }); }); From 0fafa887bfe1a84df73968629052c591586ecf56 Mon Sep 17 00:00:00 2001 From: baudointran Date: Thu, 8 Feb 2024 17:01:49 +0100 Subject: [PATCH 2/2] fix: add annotation descriptor in annotation --- src/dossier/dossier-custom-champ.spec.ts | 150 +++++++++++++++--- src/dossier/dossier-custom-champ.ts | 96 ++++++----- .../fragment/DemarcheDescriptorFragment.ts | 3 + src/graphql/getFilesFromDossier.ts | 1 + 4 files changed, 190 insertions(+), 60 deletions(-) diff --git a/src/dossier/dossier-custom-champ.spec.ts b/src/dossier/dossier-custom-champ.spec.ts index c14330e..1ce88c8 100644 --- a/src/dossier/dossier-custom-champ.spec.ts +++ b/src/dossier/dossier-custom-champ.spec.ts @@ -13,8 +13,10 @@ describe("Dossier custom champ function", () => { it("Should return initial object without modification", () => { (graphQlRequest as jest.Mock).mockResolvedValue({ dossier: { - revision: { - champDescriptors: [{}], + demarche: { + revision: { + champDescriptors: [{}], + }, }, hello: "world", champs: [ @@ -27,8 +29,10 @@ describe("Dossier custom champ function", () => { }); expect(getDossierWithCustomChamp(null, null)).resolves.toEqual({ dossier: { - revision: { - champDescriptors: [{}], + demarche: { + revision: { + champDescriptors: [{}], + }, }, hello: "world", champs: [ @@ -44,13 +48,21 @@ describe("Dossier custom champ function", () => { it("Should link descriptor on simple champ", async () => { (graphQlRequest as jest.Mock).mockResolvedValue({ dossier: { - revision: { - champDescriptors: [ - { - id: "toto", - something: "else", - }, - ], + demarche: { + revision: { + champDescriptors: [ + { + id: "toto", + something: "else", + }, + ], + annotationDescriptors: [ + { + id: "totoAnnotation", + something: "elseAnnotation", + }, + ], + }, }, champs: [ { @@ -58,6 +70,12 @@ describe("Dossier custom champ function", () => { value: 42, }, ], + annotations: [ + { + id: "totoAnnotation", + value: 42, + }, + ], }, }); const result = await getDossierWithCustomChamp(null, null); @@ -71,24 +89,50 @@ describe("Dossier custom champ function", () => { }, }, ]); + expect(result.dossier.annotations).toEqual([ + { + id: "totoAnnotation", + value: 42, + champDescriptor: { + id: "totoAnnotation", + something: "elseAnnotation", + }, + }, + ]); }); it("Should link repetable children champs", async () => { (graphQlRequest as jest.Mock).mockResolvedValue({ dossier: { - revision: { - champDescriptors: [ - { - id: "Q2hhbXAtMTA2NQ==", - something: "else", - champDescriptors: [ - { - id: "Q2hhbXAtMTA2Ng==", - label: "Pays d'origine du financement", - }, - ], - }, - ], + demarche: { + revision: { + champDescriptors: [ + { + __typename: "RepetitionChampDescriptor", + id: "Q2hhbXAtMTA2NQ==", + something: "else", + champDescriptors: [ + { + id: "Q2hhbXAtMTA2Ng==", + label: "Pays d'origine du financement", + }, + ], + }, + ], + annotationDescriptors: [ + { + __typename: "RepetitionChampDescriptor", + id: "A2hhbXAtMTA2NQ==", + something: "elseAnnotation", + champDescriptors: [ + { + id: "A2hhbXAtMTA2Ng==", + label: "Pays d'origine du financement pour Annotation", + }, + ], + }, + ], + }, }, champs: [ { @@ -113,6 +157,29 @@ describe("Dossier custom champ function", () => { ], }, ], + annotations: [ + { + id: "A2hhbXAtMTA2NQ==", + __typename: "RepetitionChamp", + value: 42, + rows: [ + { + champs: [ + { + id: "A2hhbXAtMTA2NnwwMUgyN1hKVDczWTFRMUIxOTMxRk45NTVWUw==", + __typename: "PaysChamp", + label: "Pays d'origine du financement pour Annotation", + stringValue: "Anguilla", + pays: { + name: "Anguilla", + code: "AI", + }, + }, + ], + }, + ], + }, + ], }, }); const result = await getDossierWithCustomChamp(null, null); @@ -122,6 +189,7 @@ describe("Dossier custom champ function", () => { __typename: "RepetitionChamp", value: 42, champDescriptor: { + __typename: "RepetitionChampDescriptor", id: "Q2hhbXAtMTA2NQ==", something: "else", champDescriptors: undefined, @@ -148,5 +216,39 @@ describe("Dossier custom champ function", () => { ], }, ]); + + expect(result.dossier.annotations).toEqual([ + { + id: "A2hhbXAtMTA2NQ==", + __typename: "RepetitionChamp", + value: 42, + champDescriptor: { + __typename: "RepetitionChampDescriptor", + id: "A2hhbXAtMTA2NQ==", + something: "elseAnnotation", + champDescriptors: undefined, + }, + rows: [ + { + champs: [ + { + id: "A2hhbXAtMTA2NnwwMUgyN1hKVDczWTFRMUIxOTMxRk45NTVWUw==", + __typename: "PaysChamp", + label: "Pays d'origine du financement pour Annotation", + stringValue: "Anguilla", + champDescriptor: { + id: "A2hhbXAtMTA2Ng==", + label: "Pays d'origine du financement pour Annotation", + }, + pays: { + name: "Anguilla", + code: "AI", + }, + }, + ], + }, + ], + }, + ]); }); }); diff --git a/src/dossier/dossier-custom-champ.ts b/src/dossier/dossier-custom-champ.ts index 2e2d3d2..63a0881 100644 --- a/src/dossier/dossier-custom-champ.ts +++ b/src/dossier/dossier-custom-champ.ts @@ -3,7 +3,12 @@ import { graphQlRequest } from "../common"; import getDossierQuery from "../graphql/getDossier"; import { getDossierType } from "./dossier"; import { CustomChamp, DossierWithCustomChamp } from "./custom-champ.type"; -import { Dossier } from "../@types/generated-types"; +import { + Champ, + ChampDescriptor, + Dossier, + RepetitionChampDescriptor, +} from "../@types/generated-types"; type getDossierWithCustomChampType = { dossier: DossierWithCustomChamp }; @@ -18,46 +23,65 @@ const extractSmallId = (base64String: string): string => { return base64String; }; +const hashDescriptionMapFn = (descriptor) => { + if (descriptor["__typename"] === "RepetitionChampDescriptor") { + return [ + [descriptor.id, { ...descriptor, champDescriptors: undefined }], + ...(descriptor as RepetitionChampDescriptor).champDescriptors.map( + (subDescriptor) => [subDescriptor.id, subDescriptor], + ), + ]; + } + return [[descriptor.id, descriptor]]; +}; + +const addDescriptorInchampMapFn = (_hashDescriptor) => (champ) => { + (champ as CustomChamp).champDescriptor = _hashDescriptor[champ.id]; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore it does exist + if (champ.__typename === "RepetitionChamp") { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + champ.rows.forEach((row) => { + row.champs.forEach((subChamp) => { + (subChamp as CustomChamp).champDescriptor = + _hashDescriptor[extractSmallId(subChamp.id)]; + }); + }); + } + return champ; +}; + +const _mergeChampAndDescriptor = ( + champDescriptor: Array, + listChamps: Array, +) => { + if (!champDescriptor) { + throw new Error("Cannot map descriptor without revision in dossier."); + } + const _hashDescriptor = Object.fromEntries( + champDescriptor?.map(hashDescriptionMapFn).flat(1), + ); + + return listChamps.map(addDescriptorInchampMapFn(_hashDescriptor)); +}; + export const mergeChampAndChampDescriptor = ( dossier: Partial, ): void => { - if (!dossier.revision?.champDescriptors) { - throw new Error( - "Cannot map champs descriptor without revision in dossier.", + if (dossier.champs) { + dossier.champs = _mergeChampAndDescriptor( + dossier.demarche.revision?.champDescriptors, + dossier.champs, + ); + } + + if (dossier.annotations) { + dossier.annotations = _mergeChampAndDescriptor( + dossier.demarche.revision?.annotationDescriptors, + dossier.annotations, ); } - const _hashDescriptor = Object.fromEntries( - dossier.revision.champDescriptors - ?.map((descriptor) => { - if (descriptor.champDescriptors?.length) { - return [ - [descriptor.id, { ...descriptor, champDescriptors: undefined }], - ...descriptor.champDescriptors.map((subDescriptor) => [ - subDescriptor.id, - subDescriptor, - ]), - ]; - } - return [[descriptor.id, descriptor]]; - }) - .flat(1), - ); - dossier.champs = dossier.champs.map((champ) => { - (champ as CustomChamp).champDescriptor = _hashDescriptor[champ.id]; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore it does exist - if (champ.__typename === "RepetitionChamp") { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - champ.rows.forEach((row) => { - row.champs.forEach((subChamp) => { - (subChamp as CustomChamp).champDescriptor = - _hashDescriptor[extractSmallId(subChamp.id)]; - }); - }); - } - return champ; - }); }; export const getDossierWithCustomChamp = async ( diff --git a/src/graphql/fragment/DemarcheDescriptorFragment.ts b/src/graphql/fragment/DemarcheDescriptorFragment.ts index 8039688..d4a28f6 100644 --- a/src/graphql/fragment/DemarcheDescriptorFragment.ts +++ b/src/graphql/fragment/DemarcheDescriptorFragment.ts @@ -13,5 +13,8 @@ export default gql` dateDerniereModification dateDepublication dateFermeture + revision { + ...RevisionFragment + } } `; diff --git a/src/graphql/getFilesFromDossier.ts b/src/graphql/getFilesFromDossier.ts index 4d0ccdf..50fefb1 100644 --- a/src/graphql/getFilesFromDossier.ts +++ b/src/graphql/getFilesFromDossier.ts @@ -6,6 +6,7 @@ export default gql` query getFilesFromDossier($dossierNumber: Int!) { dossier(number: $dossierNumber) { id + number annotations { ...PieceJustificativeFragment ... on RepetitionChamp {