Skip to content

Commit

Permalink
feat(users): gmail gouv users kpi (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Oct 1, 2024
1 parent 4726159 commit 881dbde
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hyyypertool.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 56 additions & 0 deletions packages/~/users/repository/src/count_gmail_members.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
organization: {
cached_libelle: "🦄 libelle",
siret: "🦄 siret",
},
},
{
email: "[email protected]",
organization: {
cached_libelle: "🦄 libelle",
siret: "🦄 siret",
},
},
]);
});
37 changes: 37 additions & 0 deletions packages/~/users/repository/src/count_gmail_members.ts
Original file line number Diff line number Diff line change
@@ -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<typeof count_gmail_members>;

0 comments on commit 881dbde

Please sign in to comment.