Skip to content

Commit

Permalink
building another function to have CustomChamp on list of dossier in d…
Browse files Browse the repository at this point in the history
…emarche.
  • Loading branch information
DIDIERRobin committed Jul 31, 2023
1 parent 2f51fb0 commit 1056be1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 49 deletions.
33 changes: 32 additions & 1 deletion src/demarche/demarche.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { GraphQLClient } from "graphql-request";
import { Demarche } from "../@types/types";
import {
Demarche,
DossierConnection,
DossierWithCustomChamp,
Maybe,
} from "../@types/types";

import queryDemarche from "../graphql/getDemarche";
import queryDemarcheDossiers from "../graphql/getDemarcheDossiers";
import queryDemarcheDeletedDossiers from "../graphql/getDemarcheDeletedDossiers";
import { graphQlRequest } from "../common";
import { mergeChampAndChampDescriptor } from "../dossier/dossier-custom-champ";

type getDemarcheType = { demarche: Partial<Demarche> };

type getDemarcheWithCustomChampType = {
demarche: Partial<Demarche> & {
dossiers: DossierConnection & {
nodes: Maybe<Array<Maybe<DossierWithCustomChamp>>>;
};
};
};

export const getDemarche = async (
client: GraphQLClient,
idDemarche: number,
Expand All @@ -26,6 +40,23 @@ export const getDemarcheDossiers = async (
});
};

export const getDemarcheDossierWithCustomChamp = async (
client: GraphQLClient,
idDemarche: number,
): Promise<getDemarcheWithCustomChampType> => {
const result = await graphQlRequest<getDemarcheWithCustomChampType>(
client,
queryDemarcheDossiers,
{
demarcheNumber: idDemarche,
},
);
result.demarche.dossiers.nodes.forEach((dossier) => {
mergeChampAndChampDescriptor(dossier);
});
return result;
};

export const getDemarcheDeletedDossiers = async (
client: GraphQLClient,
idDemarche: number,
Expand Down
56 changes: 24 additions & 32 deletions src/dossier/dossier-custom-champ.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ describe("Dossier custom champ function", () => {
it("Should return initial object without modification", () => {
(graphQlRequest as jest.Mock).mockResolvedValue({
dossier: {
demarche: {
revision: {
champDescriptors: [{}],
},
revision: {
champDescriptors: [{}],
},
hello: "world",
champs: [
Expand All @@ -29,10 +27,8 @@ describe("Dossier custom champ function", () => {
});
expect(getDossierWithCustomChamp(null, null)).resolves.toEqual({
dossier: {
demarche: {
revision: {
champDescriptors: [{}],
},
revision: {
champDescriptors: [{}],
},
hello: "world",
champs: [
Expand All @@ -48,15 +44,13 @@ describe("Dossier custom champ function", () => {
it("Should link descriptor on simple champ", async () => {
(graphQlRequest as jest.Mock).mockResolvedValue({
dossier: {
demarche: {
revision: {
champDescriptors: [
{
id: "toto",
something: "else",
},
],
},
revision: {
champDescriptors: [
{
id: "toto",
something: "else",
},
],
},
champs: [
{
Expand All @@ -82,21 +76,19 @@ describe("Dossier custom champ function", () => {
it("Should link repetable children champs", async () => {
(graphQlRequest as jest.Mock).mockResolvedValue({
dossier: {
demarche: {
revision: {
champDescriptors: [
{
id: "Q2hhbXAtMTA2NQ==",
something: "else",
champDescriptors: [
{
id: "Q2hhbXAtMTA2Ng==",
label: "Pays d'origine du financement",
},
],
},
],
},
revision: {
champDescriptors: [
{
id: "Q2hhbXAtMTA2NQ==",
something: "else",
champDescriptors: [
{
id: "Q2hhbXAtMTA2Ng==",
label: "Pays d'origine du financement",
},
],
},
],
},
champs: [
{
Expand Down
31 changes: 21 additions & 10 deletions src/dossier/dossier-custom-champ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ 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";

type getDossierWithCustomChampType = { dossier: DossierWithCustomChamp };

export const getDossierWithCustomChamp = async (
client: GraphQLClient,
idDossier: number,
): Promise<getDossierWithCustomChampType> => {
const result = await graphQlRequest<getDossierType>(client, getDossierQuery, {
dossierNumber: idDossier,
});
export const mergeChampAndChampDescriptor = (
dossier: Partial<Dossier>,
): void => {
if (!dossier.revision?.champDescriptors) {
throw new Error(
"Cannot map champs descriptor without revision in dossier.",
);
}
const _hashDescriptor = Object.fromEntries(
result.dossier.demarche?.revision?.champDescriptors
dossier.revision.champDescriptors
?.map((descriptor) => {
if (descriptor.champDescriptors?.length) {
return [
Expand All @@ -30,7 +31,7 @@ export const getDossierWithCustomChamp = async (
})
.flat(),
);
result.dossier.champs.forEach((champ) => {
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
Expand All @@ -51,5 +52,15 @@ export const getDossierWithCustomChamp = async (
}
return champ;
});
};

export const getDossierWithCustomChamp = async (
client: GraphQLClient,
idDossier: number,
): Promise<getDossierWithCustomChampType> => {
const result = await graphQlRequest<getDossierType>(client, getDossierQuery, {
dossierNumber: idDossier,
});
mergeChampAndChampDescriptor(result.dossier);
return result as getDossierWithCustomChampType;
};
17 changes: 11 additions & 6 deletions src/ds-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { GraphQLClient } from "graphql-request";
import {
getDemarche,
getDemarcheDeletedDossiers,
getDemarcheDossiers,
getDemarcheDossiers, getDemarcheDossierWithCustomChamp,
} from "./demarche/demarche";
import {
getDossier,
writeInPrivateAnnotation,
} from "./dossier/dossier";
import { getDossier, writeInPrivateAnnotation } from "./dossier/dossier";
import { getGroupInstructeur } from "./groupeInstructeur/groupeInstructeur";
import { DossierModifierAnnotationTextInput } from "./@types/types";
import { getDossierWithCustomChamp } from "./dossier/dossier-custom-champ";

export class DsApiClient {
client: GraphQLClient;
private client: GraphQLClient;

constructor(url: string, token: string) {
this.client = new GraphQLClient(url, {
Expand All @@ -29,10 +26,18 @@ export class DsApiClient {
return await getDemarche(this.client, idDemarche);
}

async getClient() {
return this.client;
}

async demarcheDossiers(idDemarche: number) {
return await getDemarcheDossiers(this.client, idDemarche);
}

async demarcheDossierWithCustomChamp(idDemarche: number) {
return await getDemarcheDossierWithCustomChamp(this.client, idDemarche);
}

async demarcheDeletedDossiers(idDemarche: number) {
return await getDemarcheDeletedDossiers(this.client, idDemarche);
}
Expand Down

0 comments on commit 1056be1

Please sign in to comment.