mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-09 12:13:10 -05:00
21 lines
616 B
TypeScript
21 lines
616 B
TypeScript
import { sql } from "../sql";
|
|
import type { Badge, User } from "../types";
|
|
|
|
const countsByUserIdStm = sql.prepare(`
|
|
select "Badge"."code", "Badge"."displayName", count("BadgeOwner"."badgeId") as count
|
|
from "BadgeOwner"
|
|
join "Badge" on "Badge"."id" = "BadgeOwner"."badgeId"
|
|
where "BadgeOwner"."userId" = $userId
|
|
group by "BadgeOwner"."badgeId", "BadgeOwner"."userId"
|
|
`);
|
|
|
|
export type CountsByUserId = Array<
|
|
Pick<Badge, "code" | "displayName"> & {
|
|
count: number;
|
|
}
|
|
>;
|
|
|
|
export function countsByUserId(userId: User["id"]) {
|
|
return countsByUserIdStm.all({ userId }) as CountsByUserId;
|
|
}
|