Skip to content

Commit

Permalink
fix: bug in DS to get champ in repetitionChamp by id
Browse files Browse the repository at this point in the history
  • Loading branch information
baudointran committed Mar 11, 2024
1 parent d5b20a1 commit 053d6f1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
24 changes: 20 additions & 4 deletions src/dossier/dossier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphQLClient } from "graphql-request";
import {
Champ,
Dossier,
DossierModifierAnnotationTextInput,
File,
Expand Down Expand Up @@ -86,22 +87,23 @@ export const getOneFileFromDossier = async (
client: GraphQLClient,
idDossier: number,
idChamp: string,
//TODO: Bug dans DS
idChampParent?: string,
): Promise<File[]> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getOneFileFromDossierQuery,
{
dossierNumber: idDossier,
champId: idChamp,
champId: idChampParent ?? idChamp,
includeAnnotations: true,
includeChamps: true,
includeMessages: true,
},
);

const dossier1 = getDossierMergeFileInFiles(dossier.dossier);

const champ: Partial<PieceJustificativeChamp> = [
const champ: Partial<Champ | PieceJustificativeChamp> = [
...(dossier1.annotations || []),
...(dossier1.champs || []),
...(dossier1.messages.map((mes) => ({
Expand All @@ -110,7 +112,21 @@ export const getOneFileFromDossier = async (
})) || []),
].flat()[0];

return champ.files;
if (champ.__typename === "RepetitionChamp") {
if (!idChampParent)
throw new Error("L'identifiant du champ parent est manquant");

return (champ as RepetitionChamp).rows.flatMap((row) =>
row.champs
.filter(
(ch: Champ) =>
ch.__typename === "PieceJustificativeChamp" && idChamp === ch.id,
)
.flatMap((ch: PieceJustificativeChamp) => ch.files),
);
}

return (champ as PieceJustificativeChamp).files;
};

export const getAttestationFromDossier = async (
Expand Down
13 changes: 11 additions & 2 deletions src/ds-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ export class DsApiClient {
async DossierFiles(idDossier) {
return await getFilesFromDossier(this.client, idDossier);
}
async dossierFile(idDossier: number, idChamp: string) {
return await getOneFileFromDossier(this.client, idDossier, idChamp);
async dossierFile(
idDossier: number,
idChamp: string,
idChampParent?: string,
) {
return await getOneFileFromDossier(
this.client,
idDossier,
idChamp,
idChampParent,
);
}

async dossierAttestation(idDossier: number) {
Expand Down
6 changes: 6 additions & 0 deletions src/graphql/getFilesFromDossier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export default gql`
id
number
annotations {
__typename
...PieceJustificativeFragment
... on RepetitionChamp {
__typename
id
rows {
champs {
...PieceJustificativeFragment
Expand All @@ -20,8 +23,11 @@ export default gql`
champs {
...PieceJustificativeFragment
... on RepetitionChamp {
__typename
id
rows {
champs {
__typename
...PieceJustificativeFragment
}
}
Expand Down
26 changes: 25 additions & 1 deletion src/graphql/getOneFileFromDossier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,36 @@ export default gql`
id
number
annotations(id: $champId) @include(if: $includeAnnotations) {
...PieceJustificativeFragment
... on RepetitionChamp {
__typename
rows {
champs {
__typename
... on PieceJustificativeChamp {
...PieceJustificativeFragment
}
}
}
}
... on PieceJustificativeChamp {
__typename
...PieceJustificativeFragment
}
}
attestation @include(if: $includeAttestation) {
__typename
...FileFragment
}
champs(id: $champId) @include(if: $includeChamps) {
__typename
... on RepetitionChamp {
rows {
champs {
__typename
...PieceJustificativeFragment
}
}
}
...PieceJustificativeFragment
}
messages(id: $champId) @include(if: $includeMessages) {
Expand Down
7 changes: 4 additions & 3 deletions test/ds-api.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DsApiClient } from "../src/index";

const { API_URL, API_TOKEN } = process.env;

describe.skip("ds api", () => {
describe("ds api", () => {
let dsApiClient;

beforeAll(() => {
Expand Down Expand Up @@ -37,16 +37,17 @@ describe.skip("ds api", () => {

it("get one files from dossiers in annatation", async () => {
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 () => {
const response = await dsApiClient.dossierFile(
221,
"Q2hhbXAtMTQ1OHwwMUhOWVkyMENWR1pNMUtUOEI2RTk5Q1QyMg==",
"Q2hhbXAtMTQ1MnwwMUhOWVk0OVFLRkpTMTFGMFdRRk1RSDVWRw==",
"Q2hhbXAtMTQ1MQ==",
);
expect(response).toHaveLength(1);
expect(response[0]).toHaveProperty("checksum", "LFOK4lV89YdX1f3iNHxWrg==");
});

it("get one files from dossiers in message ", async () => {
Expand Down

0 comments on commit 053d6f1

Please sign in to comment.