Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove organisation from keycloak, and use the one stored in agents table #195

1 change: 0 additions & 1 deletion api/scripts/compile-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { env } from "../src/env";
(async () => {
const kyselyDb = new Kysely<Database>({ dialect: createPgDialect(env.databaseUrl) });
const { useCases } = await bootstrapCore({
"keycloakUserApiParams": undefined,
"dbConfig": {
"dbKind": "kysely",
"kyselyDb": kyselyDb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export const createInMemoryAgentRepository = (): {
},
getAll: () => {
throw new Error("Not implemented");
}
},
getAllOrganizations: () => {
throw new Error("Not implemented");
},
countAll: async () => agents.length
},
testHelpers: {
setAgents: (newAgents: DbAgentWithId[]) => {
Expand Down
17 changes: 16 additions & 1 deletion api/src/core/adapters/dbApi/kysely/createPgAgentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ export const createPgAgentRepository = (db: Kysely<Database>): AgentRepository =
about: about ?? undefined,
declarations: [...usersDeclarations, ...referentsDeclarations]
}))
)
),
countAll: () =>
db
.selectFrom("agents")
.select(qb => qb.fn.countAll<number>().as("count"))
.executeTakeFirstOrThrow()
.then(({ count }) => +count),
getAllOrganizations: () =>
db
.selectFrom("agents")
.where("organization", "is not", null)
.groupBy("organization")
.orderBy("organization")
.select(({ ref }) => ref("organization").$castTo<string>().as("organization"))
.execute()
.then(results => results.map(({ organization }) => organization))
});

const makeGetAgentBuilder = (db: Kysely<Database>) =>
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/adapters/dbApi/kysely/kysely.database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type Database = {
type AgentsTable = {
id: Generated<number>;
email: string;
organization: string;
organization: string | null;
about: string | null;
isPublic: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Kysely } from "kysely";

export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable("agents")
.alterColumn("organization", ac => ac.dropNotNull())
.execute();
}

export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable("agents")
.alterColumn("organization", ac => ac.setNotNull())
.execute();
}
167 changes: 0 additions & 167 deletions api/src/core/adapters/userApi.ts

This file was deleted.

23 changes: 0 additions & 23 deletions api/src/core/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Kysely } from "kysely";
import { createObjectThatThrowsIfAccessed } from "../tools/createObjectThatThrowsIfAccessed";
import { comptoirDuLibreApi } from "./adapters/comptoirDuLibreApi";
import { createKyselyPgDbApi } from "./adapters/dbApi/kysely/createPgDbApi";
import { Database } from "./adapters/dbApi/kysely/kysely.database";
Expand All @@ -10,13 +9,11 @@ import { createGetSoftwareLatestVersion } from "./adapters/getSoftwareLatestVers
import { getWikidataSoftware } from "./adapters/wikidata/getWikidataSoftware";
import { getWikidataSoftwareOptions } from "./adapters/wikidata/getWikidataSoftwareOptions";
import { halAdapter } from "./adapters/hal";
import { createKeycloakUserApi, type KeycloakUserApiParams } from "./adapters/userApi";
import type { ComptoirDuLibreApi } from "./ports/ComptoirDuLibreApi";
import { DbApiV2 } from "./ports/DbApiV2";
import type { ExternalDataOrigin, GetSoftwareExternalData } from "./ports/GetSoftwareExternalData";
import type { GetSoftwareExternalDataOptions } from "./ports/GetSoftwareExternalDataOptions";
import type { GetSoftwareLatestVersion } from "./ports/GetSoftwareLatestVersion";
import type { UserApi } from "./ports/UserApi";
import { UseCases } from "./usecases";
import { makeGetAgent } from "./usecases/getAgent";
import { makeGetSoftwareFormAutoFillDataFromExternalAndOtherSources } from "./usecases/getSoftwareFormAutoFillDataFromExternalAndOtherSources";
Expand All @@ -28,7 +25,6 @@ type DbConfig = PgDbConfig;

type ParamsOfBootstrapCore = {
dbConfig: DbConfig;
keycloakUserApiParams: KeycloakUserApiParams | undefined;
githubPersonalAccessTokenForApiRateLimit: string;
doPerPerformPeriodicalCompilation: boolean;
doPerformCacheInitialization: boolean;
Expand All @@ -41,7 +37,6 @@ type ParamsOfBootstrapCore = {
export type Context = {
paramsOfBootstrapCore: ParamsOfBootstrapCore;
dbApi: DbApiV2;
userApi: UserApi;
comptoirDuLibreApi: ComptoirDuLibreApi;
getSoftwareExternalData: GetSoftwareExternalData;
getSoftwareLatestVersion: GetSoftwareLatestVersion;
Expand All @@ -63,10 +58,8 @@ export async function bootstrapCore(
): Promise<{ dbApi: DbApiV2; context: Context; useCases: UseCases }> {
const {
dbConfig,
keycloakUserApiParams,
githubPersonalAccessTokenForApiRateLimit,
doPerPerformPeriodicalCompilation,
doPerformCacheInitialization,
externalSoftwareDataOrigin,
initializeSoftwareFromSource,
botAgentEmail,
Expand All @@ -81,20 +74,9 @@ export async function bootstrapCore(

const { dbApi } = getDbApiAndInitializeCache(dbConfig);

const { userApi, initializeUserApiCache } =
keycloakUserApiParams === undefined
? {
"userApi": createObjectThatThrowsIfAccessed<Context["userApi"]>({
"debugMessage": "No Keycloak server"
}),
"initializeUserApiCache": async () => {}
}
: createKeycloakUserApi(keycloakUserApiParams);

const context: Context = {
"paramsOfBootstrapCore": params,
dbApi,
userApi,
comptoirDuLibreApi,
getSoftwareExternalData,
getSoftwareLatestVersion
Expand All @@ -114,11 +96,6 @@ export async function bootstrapCore(
getAgent: makeGetAgent({ agentRepository: dbApi.agent })
};

if (doPerformCacheInitialization) {
console.log("Performing user cache initialization...");
await initializeUserApiCache();
}

if (initializeSoftwareFromSource) {
if (!botAgentEmail) throw new Error("No bot agent email provided");
if (externalSoftwareDataOrigin === "HAL") {
Expand Down
4 changes: 3 additions & 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 All @@ -99,6 +99,8 @@ export interface AgentRepository {
remove: (agentId: number) => Promise<void>;
getByEmail: (email: string) => Promise<AgentWithId | undefined>;
getAll: () => Promise<AgentWithId[]>;
countAll: () => Promise<number>;
getAllOrganizations: () => Promise<string[]>;
}

export interface SoftwareReferentRepository {
Expand Down
16 changes: 0 additions & 16 deletions api/src/core/ports/UserApi.ts

This file was deleted.

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
Loading
Loading