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

Feat/rows in repetition champ #58

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dnum-mi/ds-api-client",
"version": "1.4.1",
"version": "1.5.0",
DIDIERRobin marked this conversation as resolved.
Show resolved Hide resolved
"description": "Un DS Client GraphQL prenant en charge Node et les navigateurs pour les scripts ou les applications simples",
"author": "DNUM-MI",
"type": "commonjs",
Expand Down
2 changes: 1 addition & 1 deletion src/demarche.spec.ts → src/demarche/demarche.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dsApiClient } from "./__helpers";
import { dsApiClient } from "../__helpers";

describe("Demarche (unit)", () => {
it("Has to retrieve demarche number 1", async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/demarche.ts → src/demarche/demarche.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GraphQLClient } from "graphql-request";
import { Demarche } from "./@types/types";
import { Demarche } from "../@types/types";

import queryDemarche from "./graphql/getDemarche";
import queryDemarcheDossiers from "./graphql/getDemarcheDossiers";
import queryDemarcheDeletedDossiers from "./graphql/getDemarcheDeletedDossiers";
import { graphQlRequest } from "./common";
import queryDemarche from "../graphql/getDemarche";
import queryDemarcheDossiers from "../graphql/getDemarcheDossiers";
import queryDemarcheDeletedDossiers from "../graphql/getDemarcheDeletedDossiers";
import { graphQlRequest } from "../common";

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

Expand Down
160 changes: 160 additions & 0 deletions src/dossier/dossier-custom-champ.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { graphQlRequest } from "../common";
import { getDossierWithCustomChamp } from "./dossier-custom-champ";

jest.mock("../common", () => ({
graphQlRequest: jest.fn(),
}));

describe("Dossier custom champ function", () => {
beforeEach(() => {
(graphQlRequest as jest.Mock).mockClear();
});

it("Should return initial object without modification", () => {
(graphQlRequest as jest.Mock).mockResolvedValue({
dossier: {
demarche: {
revision: {
champDescriptors: [{}],
},
},
hello: "world",
champs: [
{
id: "toto",
value: 42,
},
],
},
});
expect(getDossierWithCustomChamp(null, null)).resolves.toEqual({
dossier: {
demarche: {
revision: {
champDescriptors: [{}],
},
},
hello: "world",
champs: [
{
id: "toto",
value: 42,
},
],
},
});
});

it("Should link descriptor on simple champ", async () => {
(graphQlRequest as jest.Mock).mockResolvedValue({
dossier: {
demarche: {
revision: {
champDescriptors: [
{
id: "toto",
something: "else",
},
],
},
},
champs: [
{
id: "toto",
value: 42,
},
],
},
});
const result = await getDossierWithCustomChamp(null, null);
expect(result.dossier.champs).toEqual([
{
id: "toto",
value: 42,
champDescriptor: {
id: "toto",
something: "else",
},
},
]);
});

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",
},
],
},
],
},
},
champs: [
{
id: "Q2hhbXAtMTA2NQ==",
__typename: "RepetitionChamp",
value: 42,
rows: [
{
champs: [
{
id: "Q2hhbXAtMTA2NnwwMUgyN1hKVDczWTFRMUIxOTMxRk45NTVWUw==",
__typename: "PaysChamp",
label: "Pays d'origine du financement",
stringValue: "Anguilla",
pays: {
name: "Anguilla",
code: "AI",
},
},
],
},
],
},
],
},
});
const result = await getDossierWithCustomChamp(null, null);
expect(result.dossier.champs).toEqual([
{
id: "Q2hhbXAtMTA2NQ==",
__typename: "RepetitionChamp",
value: 42,
champDescriptor: {
id: "Q2hhbXAtMTA2NQ==",
something: "else",
champDescriptors: undefined,
},
rows: [
{
champs: [
{
id: "Q2hhbXAtMTA2NnwwMUgyN1hKVDczWTFRMUIxOTMxRk45NTVWUw==",
__typename: "PaysChamp",
label: "Pays d'origine du financement",
stringValue: "Anguilla",
champDescriptor: {
id: "Q2hhbXAtMTA2Ng==",
label: "Pays d'origine du financement",
},
pays: {
name: "Anguilla",
code: "AI",
},
},
],
},
],
},
]);
});
});
60 changes: 60 additions & 0 deletions src/dossier/dossier-custom-champ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { GraphQLClient } from "graphql-request";
import { graphQlRequest } from "../common";
import getDossierQuery from "../graphql/getDossier";
import { Champ, ChampDescriptor, Dossier } from "../@types/types";
import { getDossierType } from "./dossier";

type CustomChamp = Champ & {
champDescriptor: ChampDescriptor;
};
type DossierWithCustomChamp = Dossier & {
champs: Array<CustomChamp>;
};
type getDossierWithCustomChampType = { dossier: DossierWithCustomChamp };

export const getDossierWithCustomChamp = async (
client: GraphQLClient,
idDossier: number,
): Promise<getDossierWithCustomChampType> => {
const result = await graphQlRequest<getDossierType>(client, getDossierQuery, {
dossierNumber: idDossier,
});
const _hashDescriptor = Object.fromEntries(
result.dossier.demarche?.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(),
);
result.dossier.champs.forEach((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.map((row) => ({
champs: row.champs.map((subChamp) => {
const smallId = Buffer.from(
Buffer.from(subChamp.id, "base64")
.toString("binary")
.substring(0, 10),
"binary",
).toString("base64");
(subChamp as CustomChamp).champDescriptor = _hashDescriptor[smallId];
}),
}));
}
return champ;
});
return result as getDossierWithCustomChampType;
};
2 changes: 1 addition & 1 deletion src/dossier.spec.ts → src/dossier/dossier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dsApiClient } from "./__helpers";
import { dsApiClient } from "../__helpers";

describe("Dossier (unit)", () => {
it("Has to retrieve dossier number ", async () => {
Expand Down
11 changes: 5 additions & 6 deletions src/dossier.ts → src/dossier/dossier.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { GraphQLClient } from "graphql-request";
import { Dossier, DossierModifierAnnotationTextInput } from "./@types/types";
import { Dossier, DossierModifierAnnotationTextInput } from "../@types/types";
import getDossierQuery from "../graphql/getDossier";
import updateAnnotationPrivateQuery from "../graphql/dossierModifierAnnotationText";
import { graphQlRequest } from "../common";

import getDossierQuery from "./graphql/getDossier";
import updateAnnotationPrivateQuery from "./graphql/dossierModifierAnnotationText";
import { graphQlRequest } from "./common";

type getDossierType = { dossier: Partial<Dossier> };
export type getDossierType = { dossier: Partial<Dossier> };

export const getDossier = async (
client: GraphQLClient,
Expand Down
14 changes: 11 additions & 3 deletions src/ds-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import {
getDemarche,
getDemarcheDeletedDossiers,
getDemarcheDossiers,
} from "./demarche";
import { getDossier, writeInPrivateAnnotation } from "./dossier";
import { getGroupInstructeur } from "./groupeInstructeur";
} from "./demarche/demarche";
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;
Expand Down Expand Up @@ -37,6 +41,10 @@ export class DsApiClient {
return await getDossier(this.client, idDossier);
}

async dossierWithCustomChamp(idDossier: number) {
return await getDossierWithCustomChamp(this.client, idDossier);
}

async writeInPrivateAnnotation(input: DossierModifierAnnotationTextInput) {
return await writeInPrivateAnnotation(this.client, input);
}
Expand Down
5 changes: 5 additions & 0 deletions src/graphql/fragment/RootChampFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { gql } from "graphql-request";
export default gql`
fragment RootChampFragment on Champ {
... on RepetitionChamp {
rows {
champs {
...ChampFragment
}
}
champs {
...ChampFragment
}
Expand Down
Loading