From 96f7b1dcef23aa72f25c0be53dfe504028701821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Burkard?= <22095555+JeromeBu@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:38:05 +0100 Subject: [PATCH] remove organisation from User, so that it has to be provided at the app level --- .../dbApi/kysely/createPgAgentRepository.ts | 3 +- .../kysely/createPgSoftwareRepository.ts | 23 +++++----- api/src/core/ports/DbApiV2.ts | 2 +- api/src/core/usecases/getAgent.ts | 2 +- api/src/core/usecases/getAgent.unit.test.ts | 8 ++-- .../core/usecases/readWriteSillData/types.ts | 2 +- api/src/rpc/createTestCaller.ts | 3 +- api/src/rpc/router.ts | 4 +- api/src/rpc/routes.e2e.test.ts | 44 +++++++++---------- api/src/rpc/user.ts | 4 +- 10 files changed, 47 insertions(+), 48 deletions(-) diff --git a/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts b/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts index 65765d57..2e402af1 100644 --- a/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts +++ b/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts @@ -41,9 +41,10 @@ export const createPgAgentRepository = (db: Kysely): AgentRepository = getAllOrganizations: () => db .selectFrom("agents") + .where("organization", "is not", null) .groupBy("organization") .orderBy("organization") - .select("organization") + .select(({ ref }) => ref("organization").$castTo().as("organization")) .execute() .then(results => results.map(({ organization }) => organization)) }); diff --git a/api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts b/api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts index e5195422..d0f998ea 100644 --- a/api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts +++ b/api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts @@ -446,7 +446,7 @@ const makeGetSoftwareBuilder = (db: Kysely) => ]); type CountForOrganisationAndSoftwareId = { - organization: string; + organization: string | null; softwareId: number; type: "user" | "referent"; count: string; @@ -490,16 +490,19 @@ const getUserAndReferentCountByOrganizationBySoftwareId = async ( .execute(); return [...softwareReferentCountBySoftwareId, ...softwareUserCountBySoftwareId].reduce( - (acc, { organization, softwareId, type, count }): UserAndReferentCountByOrganizationBySoftwareId => ({ - ...acc, - [softwareId]: { - ...(acc[softwareId] ?? {}), - [organization]: { - ...(acc[softwareId]?.[organization] ?? defaultCount), - [type]: +count + (acc, { organization, softwareId, type, count }): UserAndReferentCountByOrganizationBySoftwareId => { + const orga = organization ?? "NO_ORGANIZATION"; + return { + ...acc, + [softwareId]: { + ...(acc[softwareId] ?? {}), + [orga]: { + ...(acc[softwareId]?.[orga] ?? defaultCount), + [type]: +count + } } - } - }), + }; + }, {} as UserAndReferentCountByOrganizationBySoftwareId ); }; diff --git a/api/src/core/ports/DbApiV2.ts b/api/src/core/ports/DbApiV2.ts index 8a09ad9f..2dd933ed 100644 --- a/api/src/core/ports/DbApiV2.ts +++ b/api/src/core/ports/DbApiV2.ts @@ -86,7 +86,7 @@ export interface InstanceRepository { export type DbAgent = { id: number; email: string; - organization: string; + organization: string | null; about: string | undefined; isPublic: boolean; }; diff --git a/api/src/core/usecases/getAgent.ts b/api/src/core/usecases/getAgent.ts index 55e0fa16..deba461e 100644 --- a/api/src/core/usecases/getAgent.ts +++ b/api/src/core/usecases/getAgent.ts @@ -22,7 +22,7 @@ export const makeGetAgent = if (currentUser.email === email) { const agentWithoutId = { email: currentUser.email, - organization: currentUser.organization, + organization: null, about: "", isPublic: false }; diff --git a/api/src/core/usecases/getAgent.unit.test.ts b/api/src/core/usecases/getAgent.unit.test.ts index 7e1b2281..7879ff95 100644 --- a/api/src/core/usecases/getAgent.unit.test.ts +++ b/api/src/core/usecases/getAgent.unit.test.ts @@ -29,8 +29,7 @@ describe("getAgent", () => { const currentUser: User = { id: "user-id", - email: "bob@mail.com", - organization: "Some orga" + email: "bob@mail.com" }; let agentRepository: AgentRepository; @@ -52,8 +51,7 @@ describe("getAgent", () => { email: privateAgent.email, currentUser: { id: "user-id", - email: "some@mail.com", - organization: "Truc" + email: "some@mail.com" } }); @@ -71,7 +69,7 @@ describe("getAgent", () => { const expectedAgent: AgentWithId = { id: expect.any(Number), email: currentUser.email, - organization: currentUser.organization, + organization: null, isPublic: false, about: "", declarations: [] diff --git a/api/src/core/usecases/readWriteSillData/types.ts b/api/src/core/usecases/readWriteSillData/types.ts index 29921e18..3003b720 100644 --- a/api/src/core/usecases/readWriteSillData/types.ts +++ b/api/src/core/usecases/readWriteSillData/types.ts @@ -84,7 +84,7 @@ export type Agent = { //NOTE: Undefined if the agent isn't referent of at least one software // If it's the user the email is never undefined. email: string; - organization: string; + organization: string | null; declarations: (DeclarationFormData & { softwareName: string })[]; isPublic: boolean; about: string | undefined; diff --git a/api/src/rpc/createTestCaller.ts b/api/src/rpc/createTestCaller.ts index 15804b3b..15bc004e 100644 --- a/api/src/rpc/createTestCaller.ts +++ b/api/src/rpc/createTestCaller.ts @@ -15,8 +15,7 @@ type TestCallerConfig = { export const defaultUser: User = { id: "1", - email: "default.user@mail.com", - organization: "Default Organization" + email: "default.user@mail.com" }; export type ApiCaller = Awaited>["apiCaller"]; diff --git a/api/src/rpc/router.ts b/api/src/rpc/router.ts index 46345014..22cbf0bc 100644 --- a/api/src/rpc/router.ts +++ b/api/src/rpc/router.ts @@ -197,7 +197,7 @@ export function createRouter(params: { if (!agent) { agentId = await dbApi.agent.add({ email: user.email, - organization: user.organization, + organization: null, about: undefined, isPublic: false }); @@ -268,7 +268,7 @@ export function createRouter(params: { if (!agent) { agentId = await dbApi.agent.add({ email: user.email, - organization: user.organization, + organization: null, about: undefined, isPublic: false }); diff --git a/api/src/rpc/routes.e2e.test.ts b/api/src/rpc/routes.e2e.test.ts index cf113269..4dad4169 100644 --- a/api/src/rpc/routes.e2e.test.ts +++ b/api/src/rpc/routes.e2e.test.ts @@ -17,37 +17,37 @@ import { ApiCaller, createTestCaller, defaultUser } from "./createTestCaller"; const softwareFormData = createSoftwareFormData(); const declarationFormData = createDeclarationFormData(); -describe("stripNullOrUndefined", () => { - it("removes null and undefined values", () => { - const stripped = stripNullOrUndefinedValues({ - "a": null, - "b": undefined, - "c": 0, - "d": 1, - "e": "", - "f": "yolo" - }); - expect(stripped.hasOwnProperty("a")).toBe(false); - expect(stripped.hasOwnProperty("b")).toBe(false); - expect(stripped).toStrictEqual({ "c": 0, "d": 1, "e": "", "f": "yolo" }); - }); -}); - describe("RPC e2e tests", () => { let apiCaller: ApiCaller; let kyselyDb: Kysely; + describe("stripNullOrUndefined", () => { + it("removes null and undefined values", () => { + const stripped = stripNullOrUndefinedValues({ + "a": null, + "b": undefined, + "c": 0, + "d": 1, + "e": "", + "f": "yolo" + }); + expect(stripped.hasOwnProperty("a")).toBe(false); + expect(stripped.hasOwnProperty("b")).toBe(false); + expect(stripped).toStrictEqual({ "c": 0, "d": 1, "e": "", "f": "yolo" }); + }); + }); + describe("getAgents - wrong paths", () => { it("fails with UNAUTHORIZED if user is not logged in", async () => { ({ apiCaller, kyselyDb } = await createTestCaller({ user: undefined })); - expect(apiCaller.getAgents()).rejects.toThrow("UNAUTHORIZED"); + await expect(apiCaller.getAgents()).rejects.toThrow("UNAUTHORIZED"); }); }); describe("createUserOrReferent - Wrong paths", () => { it("fails with UNAUTHORIZED if user is not logged in", async () => { ({ apiCaller, kyselyDb } = await createTestCaller({ user: undefined })); - expect( + await expect( apiCaller.createUserOrReferent({ formData: declarationFormData, softwareId: 123 @@ -57,7 +57,7 @@ describe("RPC e2e tests", () => { it("fails when software is not found in SILL", async () => { ({ apiCaller, kyselyDb } = await createTestCaller()); - expect( + await expect( apiCaller.createUserOrReferent({ formData: declarationFormData, softwareId: 404 @@ -69,7 +69,7 @@ describe("RPC e2e tests", () => { describe("createSoftware - Wrong paths", () => { it("fails with UNAUTHORIZED if user is not logged in", async () => { ({ apiCaller, kyselyDb } = await createTestCaller({ user: undefined })); - expect( + await expect( apiCaller.createSoftware({ formData: softwareFormData }) @@ -114,7 +114,7 @@ describe("RPC e2e tests", () => { expectToMatchObject(agent, { id: expect.any(Number), email: defaultUser.email, - organization: defaultUser.organization + organization: null }); const softwareRows = await getSoftwareRows(); @@ -162,7 +162,7 @@ describe("RPC e2e tests", () => { expect(agents).toHaveLength(1); expectToMatchObject(agents[0], { "email": defaultUser.email, - "organization": defaultUser.organization + "organization": null }); }); diff --git a/api/src/rpc/user.ts b/api/src/rpc/user.ts index d277302d..35a4ed77 100644 --- a/api/src/rpc/user.ts +++ b/api/src/rpc/user.ts @@ -5,13 +5,11 @@ import { parsedJwtPayloadToUser } from "../tools/parsedJwtPayloadToUser"; export type User = { id: string; email: string; - organization: string; }; const zUser = z.object({ "id": z.string(), - "email": z.string(), - "organization": z.string() + "email": z.string() }); {