Skip to content

Commit

Permalink
remove organisation from User, so that it has to be provided at the a…
Browse files Browse the repository at this point in the history
…pp level
  • Loading branch information
JeromeBu committed Dec 11, 2024
1 parent 5bbaa6b commit 96f7b1d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export const createPgAgentRepository = (db: Kysely<Database>): AgentRepository =
getAllOrganizations: () =>
db
.selectFrom("agents")
.where("organization", "is not", null)
.groupBy("organization")
.orderBy("organization")
.select("organization")
.select(({ ref }) => ref("organization").$castTo<string>().as("organization"))
.execute()
.then(results => results.map(({ organization }) => organization))
});
Expand Down
23 changes: 13 additions & 10 deletions api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ const makeGetSoftwareBuilder = (db: Kysely<Database>) =>
]);

type CountForOrganisationAndSoftwareId = {
organization: string;
organization: string | null;
softwareId: number;
type: "user" | "referent";
count: string;
Expand Down Expand Up @@ -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
);
};
Expand Down
2 changes: 1 addition & 1 deletion api/src/core/ports/DbApiV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface InstanceRepository {
export type DbAgent = {
id: number;
email: string;
organization: string;
organization: string | null;
about: string | undefined;
isPublic: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion api/src/core/usecases/getAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const makeGetAgent =
if (currentUser.email === email) {
const agentWithoutId = {
email: currentUser.email,
organization: currentUser.organization,
organization: null,
about: "",
isPublic: false
};
Expand Down
8 changes: 3 additions & 5 deletions api/src/core/usecases/getAgent.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ describe("getAgent", () => {

const currentUser: User = {
id: "user-id",
email: "[email protected]",
organization: "Some orga"
email: "[email protected]"
};

let agentRepository: AgentRepository;
Expand All @@ -52,8 +51,7 @@ describe("getAgent", () => {
email: privateAgent.email,
currentUser: {
id: "user-id",
email: "[email protected]",
organization: "Truc"
email: "[email protected]"
}
});

Expand All @@ -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: []
Expand Down
2 changes: 1 addition & 1 deletion api/src/core/usecases/readWriteSillData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions api/src/rpc/createTestCaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type TestCallerConfig = {

export const defaultUser: User = {
id: "1",
email: "[email protected]",
organization: "Default Organization"
email: "[email protected]"
};

export type ApiCaller = Awaited<ReturnType<typeof createTestCaller>>["apiCaller"];
Expand Down
4 changes: 2 additions & 2 deletions api/src/rpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down
44 changes: 22 additions & 22 deletions api/src/rpc/routes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Database>;

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
Expand All @@ -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
Expand All @@ -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
})
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -162,7 +162,7 @@ describe("RPC e2e tests", () => {
expect(agents).toHaveLength(1);
expectToMatchObject(agents[0], {
"email": defaultUser.email,
"organization": defaultUser.organization
"organization": null
});
});

Expand Down
4 changes: 1 addition & 3 deletions api/src/rpc/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});

{
Expand Down

0 comments on commit 96f7b1d

Please sign in to comment.