Skip to content

Commit

Permalink
feat: get file from dossier
Browse files Browse the repository at this point in the history
  • Loading branch information
baudointran committed Feb 6, 2024
1 parent 71bc0a0 commit 0239936
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 2 deletions.
105 changes: 104 additions & 1 deletion src/dossier/dossier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { GraphQLClient } from "graphql-request";
import { Dossier, DossierModifierAnnotationTextInput } from "../@types/types";
import {
Dossier,
DossierModifierAnnotationTextInput,
File,
PieceJustificativeChamp,
RepetitionChamp,
} from "../@types/types";
import getDossierQuery from "../graphql/getDossier";
import getFilesFromDossierQuery from "../graphql/getFilesFromDossier";
import getOneFileFromDossierQuery from "../graphql/getOneFileFromDossier";
import updateAnnotationPrivateQuery from "../graphql/dossierModifierAnnotationText";
import { graphQlRequest } from "../common";

Expand Down Expand Up @@ -39,3 +47,98 @@ export const writeInPrivateAnnotation = async (
);
});
};

const notEmptyChampsFilter = (ch) =>
ch.id ||
((ch as RepetitionChamp).rows && (ch as RepetitionChamp).rows.length);

export const getFilesFromDossier = async (
client: GraphQLClient,
idDossier: number,
): Promise<getDossierType> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getFilesFromDossierQuery,
{
dossierNumber: idDossier,
},
);
return {
dossier: {
id: dossier.dossier.id,
number: dossier.dossier.number,
annotations: dossier.dossier.annotations?.filter(notEmptyChampsFilter),
champs: dossier.dossier.champs?.filter(notEmptyChampsFilter),
messages: dossier.dossier.messages?.filter(
(mes) => mes.attachments && mes.attachments.length,
),
attestation: dossier.dossier.attestation,
},
};
};
type champType = {
champ: {
id?: string;
files: Partial<File>[];
dossierId: string;
dossierNumber: number;
};
};

export const getOneFileFromDossier = async (
client: GraphQLClient,
idDossier: number,
idChamp: string,
): Promise<champType> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getOneFileFromDossierQuery,
{
dossierNumber: idDossier,
champId: idChamp,
includeAnnotations: true,
includeChamps: true,
includeMessages: true,
},
);

const champ: Partial<PieceJustificativeChamp> = [
...(dossier.dossier.annotations || []),
...(dossier.dossier.champs || []),
...(dossier.dossier.messages.map((mes) => ({
id: mes.id,
files: mes.attachments,
})) || []),
].flat()[0];

return {
champ: {
id: champ.id,
files: champ.files,
dossierId: dossier.dossier.id,
dossierNumber: dossier.dossier.number,
},
};
};

export const getAttestationFromDossier = async (
client: GraphQLClient,
idDossier: number,
): Promise<champType> => {
const dossier = await graphQlRequest<getDossierType>(
client,
getOneFileFromDossierQuery,
{
dossierNumber: idDossier,
includeAttestation: true,
},
);

return {
champ: {
dossierId: dossier.dossier.id,
dossierNumber: dossier.dossier.number,
files: dossier.dossier.attestation ? [dossier.dossier.attestation] : [],
},
};
};
19 changes: 18 additions & 1 deletion src/ds-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
getDemarcheDossiers,
getDemarcheDossierWithCustomChamp,
} from "./demarche/demarche";
import { getDossier, writeInPrivateAnnotation } from "./dossier/dossier";
import {
getAttestationFromDossier,
getFilesFromDossier,
getDossier,
getOneFileFromDossier,
writeInPrivateAnnotation,
} from "./dossier/dossier";
import { getGroupInstructeur } from "./groupeInstructeur/groupeInstructeur";
import { DossierModifierAnnotationTextInput } from "./@types/types";
import { getDossierWithCustomChamp } from "./dossier/dossier-custom-champ";
Expand Down Expand Up @@ -77,4 +83,15 @@ export class DsApiClient {
async groupeInstructeur(idGroupeInstructeur: number) {
return await getGroupInstructeur(this.client, idGroupeInstructeur);
}

async DossierFiles(idDossier) {
return await getFilesFromDossier(this.client, idDossier);
}
async dossierFile(idDossier: number, idChamp: string) {
return await getOneFileFromDossier(this.client, idDossier, idChamp);
}

async dossierAttestation(idDossier: number) {
return await getAttestationFromDossier(this.client, idDossier);
}
}
10 changes: 10 additions & 0 deletions src/graphql/fragment/PieceJustificativeFragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { gql } from "graphql-request";

export default gql`
fragment PieceJustificativeFragment on PieceJustificativeChamp {
id
files {
...FileFragment
}
}
`;
36 changes: 36 additions & 0 deletions src/graphql/getFilesFromDossier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { gql } from "graphql-request";
import FileFragment from "./fragment/FileFragment";
import PieceJustificativeFragment from "./fragment/PieceJustificativeFragment";

export default gql`
query getFilesFromDossier($dossierNumber: Int!) {
dossier(number: $dossierNumber) {
id
annotations {
...PieceJustificativeFragment
... on RepetitionChamp {
rows {
champs {
...PieceJustificativeFragment
}
}
}
}
champs {
...PieceJustificativeFragment
... on RepetitionChamp {
rows {
champs {
...PieceJustificativeFragment
}
}
}
}
attestation {
...FileFragment
}
}
}
${FileFragment}
${PieceJustificativeFragment}
`;
37 changes: 37 additions & 0 deletions src/graphql/getOneFileFromDossier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { gql } from "graphql-request";
import FileFragment from "./fragment/FileFragment";
import PieceJustificativeFragment from "./fragment/PieceJustificativeFragment";

export default gql`
query getOneFileFromDossier(
$dossierNumber: Int!
$champId: ID
$includeAnnotations: Boolean = false
$includeChamps: Boolean = false
$includeMessages: Boolean = false
$includeAttestation: Boolean = false
) {
dossier(number: $dossierNumber) {
id
number
annotations(id: $champId) @include(if: $includeAnnotations) {
...PieceJustificativeFragment
}
attestation @include(if: $includeAttestation) {
...FileFragment
}
champs(id: $champId) @include(if: $includeChamps) {
...PieceJustificativeFragment
}
messages(id: $champId) @include(if: $includeMessages) {
attachments {
...FileFragment
}
id
}
}
}
${FileFragment}
${PieceJustificativeFragment}
`;
76 changes: 76 additions & 0 deletions test/ds-api.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { DsApiClient } from "../src/index";

const { API_URL, API_TOKEN } = process.env;

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

beforeAll(() => {
dsApiClient = new DsApiClient(API_URL, API_TOKEN);
});
it("Has to retrieve demarche number 1", async () => {
const response = await dsApiClient.demarche(1);
expect(response).toMatchObject({
demarche: {
number: 1,
title: "test procedure 20220920",
description: "test procedure 20220920",
},
});
});

it("Has to retrieve demarche number 1", async () => {
const response = await dsApiClient.demarche(1);
expect(response?.demarche?.number).toBe(1);
});

it("get demarche with dossier ids number 1", async () => {
const response = await dsApiClient.demarcheDossierIds(1);
expect(response?.demarche?.number).toBe(1);
expect(response?.demarche?.dossiers.nodes).toHaveLength(1);
expect(response?.demarche?.dossiers.nodes).toMatchObject([
{
number: 220,
},
]);
});

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);
});

it("get one files from dossiers in repetable of annotation ", async () => {
const response = await dsApiClient.dossierFile(
221,
"Q2hhbXAtMTQ1OHwwMUhOWVkyMENWR1pNMUtUOEI2RTk5Q1QyMg==",
);
expect(response?.champ).toMatchObject({
id: "Q2hhbXAtMTQ1OHwwMUhOWVkyMENWR1pNMUtUOEI2RTk5Q1QyMg==",
dossierNumber: 221,
});
expect(response?.champ.files).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);
});

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);
});
});

0 comments on commit 0239936

Please sign in to comment.