diff --git a/hyyypertool.code-workspace b/hyyypertool.code-workspace index 196613eb..ab579332 100644 --- a/hyyypertool.code-workspace +++ b/hyyypertool.code-workspace @@ -201,6 +201,17 @@ "problemMatcher": ["$tsc-watch"], "runOptions": { "instanceLimit": 1 }, }, + { + "label": "๐Ÿงช Watch Test : Current file", + "command": "bun test --watch ${fileDirname}/${fileBasename}", + "type": "shell", + "group": "build", + "options": { + "cwd": "${workspaceFolder:root}", + }, + "problemMatcher": ["$tsc-watch"], + "runOptions": { "instanceLimit": 1 }, + }, { "label": "๐Ÿงช Cypress Studio", "command": "pnpm run studio", diff --git a/packages/~/users/repository/src/count_gmail_members.test.ts b/packages/~/users/repository/src/count_gmail_members.test.ts new file mode 100644 index 00000000..a33cd036 --- /dev/null +++ b/packages/~/users/repository/src/count_gmail_members.test.ts @@ -0,0 +1,56 @@ +// + +import { + create_pink_diamond_user, + create_red_diamond_user, + create_unicorn_organization, +} from "@~/moncomptepro.database/seed/unicorn"; +import { + add_user_to_organization, + empty_database, + migrate, + pg, +} from "@~/moncomptepro.database/testing"; +import { beforeAll, beforeEach, expect, test } from "bun:test"; +import { count_gmail_members } from "./count_gmail_members"; + +// + +beforeAll(migrate); +beforeEach(empty_database); + +test("returns pink diamond", async () => { + const unicorn_organization_id = await create_unicorn_organization(pg); + const pink_diamond_user_id = await create_pink_diamond_user(pg); + await add_user_to_organization({ + organization_id: unicorn_organization_id, + user_id: pink_diamond_user_id, + }); + const red_diamond_user_id = await create_red_diamond_user(pg); + await add_user_to_organization({ + organization_id: unicorn_organization_id, + user_id: red_diamond_user_id, + }); + + const emails = await count_gmail_members(pg, { + siret: "๐Ÿฆ„", + domain: "@unicorn.xyz", + }); + + expect(emails).toEqual([ + { + email: "pink.diamond@unicorn.xyz", + organization: { + cached_libelle: "๐Ÿฆ„ libelle", + siret: "๐Ÿฆ„ siret", + }, + }, + { + email: "red.diamond@unicorn.xyz", + organization: { + cached_libelle: "๐Ÿฆ„ libelle", + siret: "๐Ÿฆ„ siret", + }, + }, + ]); +}); diff --git a/packages/~/users/repository/src/count_gmail_members.ts b/packages/~/users/repository/src/count_gmail_members.ts new file mode 100644 index 00000000..ed78d135 --- /dev/null +++ b/packages/~/users/repository/src/count_gmail_members.ts @@ -0,0 +1,37 @@ +// + +import { schema, type MonComptePro_PgDatabase } from "@~/moncomptepro.database"; +import { and, eq, like } from "drizzle-orm"; + +// + +export async function count_gmail_members( + pg: MonComptePro_PgDatabase, + { domain, siret }: { domain: string; siret: string }, +) { + return pg + .select({ + email: schema.users.email, + organization: { + cached_libelle: schema.organizations.cached_libelle, + siret: schema.organizations.siret, + }, + }) + .from(schema.users) + .innerJoin( + schema.users_organizations, + eq(schema.users.id, schema.users_organizations.user_id), + ) + .innerJoin( + schema.organizations, + eq(schema.users_organizations.organization_id, schema.organizations.id), + ) + .where( + and( + like(schema.organizations.siret, `${siret}%`), + like(schema.users.email, `%${domain}`), + ), + ); +} + +export type count_gmail_members_dto = ReturnType;