Skip to content

Commit

Permalink
remove usages of userApi
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Dec 11, 2024
1 parent bf50208 commit f9f2502
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const createInMemoryAgentRepository = (): {
},
getAllOrganizations: () => {
throw new Error("Not implemented");
}
},
countAll: async () => agents.length
},
testHelpers: {
setAgents: (newAgents: DbAgentWithId[]) => {
Expand Down
6 changes: 6 additions & 0 deletions api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export const createPgAgentRepository = (db: Kysely<Database>): AgentRepository =
declarations: [...usersDeclarations, ...referentsDeclarations]
}))
),
countAll: () =>
db
.selectFrom("agents")
.select(qb => qb.fn.countAll<number>().as("count"))
.executeTakeFirstOrThrow()
.then(({ count }) => +count),
getAllOrganizations: () =>
db
.selectFrom("agents")
Expand Down
23 changes: 1 addition & 22 deletions api/src/core/adapters/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions api/src/core/ports/DbApiV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface AgentRepository {
remove: (agentId: number) => Promise<void>;
getByEmail: (email: string) => Promise<AgentWithId | undefined>;
getAll: () => Promise<AgentWithId[]>;
countAll: () => Promise<number>;
getAllOrganizations: () => Promise<string[]>;
}

Expand Down
4 changes: 0 additions & 4 deletions api/src/core/ports/UserApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export type UserApi = {
updateUserEmail: (params: { userId: string; email: string }) => Promise<void>;
getAllowedEmailRegexp: {
(): Promise<string>;
clear: () => void;
};
getUserCount: {
(): Promise<number>;
clear: () => void;
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 @@ -24,7 +24,7 @@ export const createTestCaller = async ({ user }: TestCallerConfig = { user: defa
const externalSoftwareDataOrigin: ExternalDataOrigin = "wikidata";
const kyselyDb = new Kysely<Database>({ dialect: createPgDialect(testPgUrl) });

const { context, dbApi, useCases } = await bootstrapCore({
const { dbApi, useCases } = await bootstrapCore({
"dbConfig": { dbKind: "kysely", kyselyDb },
"keycloakUserApiParams": undefined,
"githubPersonalAccessTokenForApiRateLimit": "fake-token",
Expand All @@ -44,7 +44,6 @@ export const createTestCaller = async ({ user }: TestCallerConfig = { user: defa
const { router } = createRouter({
useCases,
dbApi,
coreContext: context,
keycloakParams: undefined,
redirectUrl: undefined,
externalSoftwareDataOrigin,
Expand Down
5 changes: 1 addition & 4 deletions api/src/rpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +34,6 @@ import type { User } from "./user";
export function createRouter(params: {
dbApi: DbApiV2;
useCases: UseCases;
coreContext: CoreContext;
keycloakParams:
| (KeycloakParams & {
organizationUserProfileAttributeName: string;
Expand All @@ -51,7 +49,6 @@ export function createRouter(params: {
}) {
const {
useCases,
coreContext,
dbApi,
keycloakParams,
jwtClaimByUserKey,
Expand Down Expand Up @@ -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 };
Expand Down
7 changes: 1 addition & 6 deletions api/src/rpc/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ export async function startRpcService(params: {

const kyselyDb = new Kysely<Database>({ dialect: createPgDialect(databaseUrl) });

const {
dbApi,
context: coreContext,
useCases
} = await bootstrapCore({
const { dbApi, useCases } = await bootstrapCore({
"dbConfig": {
"dbKind": "kysely",
"kyselyDb": kyselyDb
Expand Down Expand Up @@ -117,7 +113,6 @@ export async function startRpcService(params: {
dbApi,
getSoftwareExternalDataOptions,
getSoftwareExternalData,
coreContext,
jwtClaimByUserKey,
"keycloakParams":
keycloakParams === undefined
Expand Down
7 changes: 2 additions & 5 deletions web/src/ui/pages/account/Account.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f9f2502

Please sign in to comment.