Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/champ descriptor in annotation #90

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 126 additions & 24 deletions src/dossier/dossier-custom-champ.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -27,8 +29,10 @@ describe("Dossier custom champ function", () => {
});
expect(getDossierWithCustomChamp(null, null)).resolves.toEqual({
dossier: {
revision: {
champDescriptors: [{}],
demarche: {
revision: {
champDescriptors: [{}],
},
},
hello: "world",
champs: [
Expand All @@ -44,20 +48,34 @@ 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: [
{
id: "toto",
value: 42,
},
],
annotations: [
{
id: "totoAnnotation",
value: 42,
},
],
},
});
const result = await getDossierWithCustomChamp(null, null);
Expand All @@ -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: [
{
Expand All @@ -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);
Expand All @@ -122,6 +189,7 @@ describe("Dossier custom champ function", () => {
__typename: "RepetitionChamp",
value: 42,
champDescriptor: {
__typename: "RepetitionChampDescriptor",
id: "Q2hhbXAtMTA2NQ==",
something: "else",
champDescriptors: undefined,
Expand All @@ -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",
},
},
],
},
],
},
]);
});
});
96 changes: 60 additions & 36 deletions src/dossier/dossier-custom-champ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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<ChampDescriptor>,
listChamps: Array<Champ>,
) => {
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<Dossier>,
): 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 (
Expand Down
29 changes: 4 additions & 25 deletions src/dossier/dossier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,12 @@ export const getFilesFromDossier = async (
},
};
};
type champType = {
champ: {
id?: string;
files: Partial<File>[];
dossierId: string;
dossierNumber: number;
};
};

export const getOneFileFromDossier = async (
client: GraphQLClient,
idDossier: number,
idChamp: string,
): Promise<champType> => {
): Promise<File[]> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getOneFileFromDossierQuery,
Expand All @@ -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<champType> => {
): Promise<File[]> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getOneFileFromDossierQuery,
Expand All @@ -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] : [];
};
3 changes: 3 additions & 0 deletions src/graphql/fragment/DemarcheDescriptorFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ export default gql`
dateDerniereModification
dateDepublication
dateFermeture
revision {
...RevisionFragment
}
}
`;
1 change: 1 addition & 0 deletions src/graphql/getFilesFromDossier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default gql`
query getFilesFromDossier($dossierNumber: Int!) {
dossier(number: $dossierNumber) {
id
number
annotations {
...PieceJustificativeFragment
... on RepetitionChamp {
Expand Down
Loading