Skip to content

Commit

Permalink
fix: add annotation descriptor in annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
baudointran committed Feb 8, 2024
1 parent 8d2fbd7 commit 47e53f6
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 60 deletions.
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
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

0 comments on commit 47e53f6

Please sign in to comment.