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 b9fea1e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 27 deletions.
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
2 changes: 1 addition & 1 deletion api/src/rpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,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

0 comments on commit b9fea1e

Please sign in to comment.