From f9f2502cbd3a8a253f2c8aa7c66ad8dca77d12ac 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 18:38:22 +0100 Subject: [PATCH] remove usages of userApi --- .../createInMemoryAgentRepository.ts | 3 ++- .../dbApi/kysely/createPgAgentRepository.ts | 6 +++++ api/src/core/adapters/userApi.ts | 23 +------------------ api/src/core/ports/DbApiV2.ts | 1 + api/src/core/ports/UserApi.ts | 4 ---- api/src/rpc/createTestCaller.ts | 3 +-- api/src/rpc/router.ts | 5 +--- api/src/rpc/start.ts | 7 +----- web/src/ui/pages/account/Account.tsx | 7 ++---- 9 files changed, 15 insertions(+), 44 deletions(-) diff --git a/api/src/core/adapters/dbApi/in-memory/createInMemoryAgentRepository.ts b/api/src/core/adapters/dbApi/in-memory/createInMemoryAgentRepository.ts index 86fbf641..e6492a74 100644 --- a/api/src/core/adapters/dbApi/in-memory/createInMemoryAgentRepository.ts +++ b/api/src/core/adapters/dbApi/in-memory/createInMemoryAgentRepository.ts @@ -33,7 +33,8 @@ export const createInMemoryAgentRepository = (): { }, getAllOrganizations: () => { throw new Error("Not implemented"); - } + }, + countAll: async () => agents.length }, testHelpers: { setAgents: (newAgents: DbAgentWithId[]) => { diff --git a/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts b/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts index 2e402af1..99feb735 100644 --- a/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts +++ b/api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts @@ -38,6 +38,12 @@ export const createPgAgentRepository = (db: Kysely): AgentRepository = declarations: [...usersDeclarations, ...referentsDeclarations] })) ), + countAll: () => + db + .selectFrom("agents") + .select(qb => qb.fn.countAll().as("count")) + .executeTakeFirstOrThrow() + .then(({ count }) => +count), getAllOrganizations: () => db .selectFrom("agents") diff --git a/api/src/core/adapters/userApi.ts b/api/src/core/adapters/userApi.ts index bf5a9113..a130bd6a 100644 --- a/api/src/core/adapters/userApi.ts +++ b/api/src/core/adapters/userApi.ts @@ -32,27 +32,6 @@ export function createKeycloakUserApi(params: KeycloakUserApiParams): { "body": { email } }) ), - "getAllowedEmailRegexp": memoize( - async () => { - const attributes = await keycloakAdminApiClient.getUserProfileAttributes(); - - let emailRegExpStr: string; - - try { - emailRegExpStr = (attributes.find(({ name }) => name === "email") as any).validations.pattern - .pattern; - } catch { - throw new Error(`Can't extract RegExp from ${JSON.stringify(attributes)}`); - } - - return emailRegExpStr; - }, - { - "promise": true, - maxAge, - "preFetch": true - } - ), "getUserCount": memoize( async () => { let count = 0; @@ -93,7 +72,7 @@ export function createKeycloakUserApi(params: KeycloakUserApiParams): { console.log("Starting userApi cache initialization..."); await Promise.all( - (["getUserCount", "getAllowedEmailRegexp"] as const).map(async function callee(methodName) { + (["getUserCount"] as const).map(async function callee(methodName) { const f = userApi[methodName]; await f(); diff --git a/api/src/core/ports/DbApiV2.ts b/api/src/core/ports/DbApiV2.ts index 2dd933ed..de75942e 100644 --- a/api/src/core/ports/DbApiV2.ts +++ b/api/src/core/ports/DbApiV2.ts @@ -99,6 +99,7 @@ export interface AgentRepository { remove: (agentId: number) => Promise; getByEmail: (email: string) => Promise; getAll: () => Promise; + countAll: () => Promise; getAllOrganizations: () => Promise; } diff --git a/api/src/core/ports/UserApi.ts b/api/src/core/ports/UserApi.ts index 429dd4cd..e5018151 100644 --- a/api/src/core/ports/UserApi.ts +++ b/api/src/core/ports/UserApi.ts @@ -1,9 +1,5 @@ export type UserApi = { updateUserEmail: (params: { userId: string; email: string }) => Promise; - getAllowedEmailRegexp: { - (): Promise; - clear: () => void; - }; getUserCount: { (): Promise; clear: () => void; diff --git a/api/src/rpc/createTestCaller.ts b/api/src/rpc/createTestCaller.ts index 15bc004e..bac6fb42 100644 --- a/api/src/rpc/createTestCaller.ts +++ b/api/src/rpc/createTestCaller.ts @@ -24,7 +24,7 @@ export const createTestCaller = async ({ user }: TestCallerConfig = { user: defa const externalSoftwareDataOrigin: ExternalDataOrigin = "wikidata"; const kyselyDb = new Kysely({ dialect: createPgDialect(testPgUrl) }); - const { context, dbApi, useCases } = await bootstrapCore({ + const { dbApi, useCases } = await bootstrapCore({ "dbConfig": { dbKind: "kysely", kyselyDb }, "keycloakUserApiParams": undefined, "githubPersonalAccessTokenForApiRateLimit": "fake-token", @@ -44,7 +44,6 @@ export const createTestCaller = async ({ user }: TestCallerConfig = { user: defa const { router } = createRouter({ useCases, dbApi, - coreContext: context, keycloakParams: undefined, redirectUrl: undefined, externalSoftwareDataOrigin, diff --git a/api/src/rpc/router.ts b/api/src/rpc/router.ts index ae1d2803..818ec8c6 100644 --- a/api/src/rpc/router.ts +++ b/api/src/rpc/router.ts @@ -8,7 +8,6 @@ import superjson from "superjson"; import type { Equals, ReturnType } from "tsafe"; import { assert } from "tsafe/assert"; import { z } from "zod"; -import type { Context as CoreContext } from "../core"; import { DbApiV2 } from "../core/ports/DbApiV2"; import { ExternalDataOrigin, @@ -35,7 +34,6 @@ import type { User } from "./user"; export function createRouter(params: { dbApi: DbApiV2; useCases: UseCases; - coreContext: CoreContext; keycloakParams: | (KeycloakParams & { organizationUserProfileAttributeName: string; @@ -51,7 +49,6 @@ export function createRouter(params: { }) { const { useCases, - coreContext, dbApi, keycloakParams, jwtClaimByUserKey, @@ -494,7 +491,7 @@ export function createRouter(params: { }); await dbApi.agent.update({ ...agent, email: newEmail }); }), - "getRegisteredUserCount": loggedProcedure.query(async () => coreContext.userApi.getUserCount()), + "getRegisteredUserCount": loggedProcedure.query(async () => dbApi.agent.countAll()), "getTotalReferentCount": loggedProcedure.query(async () => { const referentCount = await dbApi.softwareReferent.getTotalCount(); return { referentCount }; diff --git a/api/src/rpc/start.ts b/api/src/rpc/start.ts index ec256011..42a42e3d 100644 --- a/api/src/rpc/start.ts +++ b/api/src/rpc/start.ts @@ -68,11 +68,7 @@ export async function startRpcService(params: { const kyselyDb = new Kysely({ dialect: createPgDialect(databaseUrl) }); - const { - dbApi, - context: coreContext, - useCases - } = await bootstrapCore({ + const { dbApi, useCases } = await bootstrapCore({ "dbConfig": { "dbKind": "kysely", "kyselyDb": kyselyDb @@ -117,7 +113,6 @@ export async function startRpcService(params: { dbApi, getSoftwareExternalDataOptions, getSoftwareExternalData, - coreContext, jwtClaimByUserKey, "keycloakParams": keycloakParams === undefined diff --git a/web/src/ui/pages/account/Account.tsx b/web/src/ui/pages/account/Account.tsx index b4801fa6..2a5650f8 100644 --- a/web/src/ui/pages/account/Account.tsx +++ b/web/src/ui/pages/account/Account.tsx @@ -1,7 +1,7 @@ -import { useEffect, useState, useMemo } from "react"; +import { useEffect, useState } from "react"; import { tss } from "tss-react"; import { fr } from "@codegouvfr/react-dsfr"; -import { useTranslation, useGetOrganizationFullName, evtLang } from "ui/i18n"; +import { useTranslation, evtLang } from "ui/i18n"; import { assert } from "tsafe/assert"; import { Equals } from "tsafe"; import { declareComponentKeys } from "i18nifty"; @@ -209,9 +209,6 @@ function AccountReady(props: { className?: string }) { } const useStyles = tss.withName({ Account }).create({ - organizationInput: { - flex: 1 - }, "oidcInfos": { "paddingTop": fr.spacing("6v"), "maxWidth": 650,