-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): gmail gouv users kpi (#561)
- Loading branch information
1 parent
4726159
commit 881dbde
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/~/users/repository/src/count_gmail_members.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |