-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.ts
More file actions
55 lines (47 loc) · 1.77 KB
/
Copy pathqueries.ts
File metadata and controls
55 lines (47 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { db } from "@/lib/db";
import { profiles, subscriptions } from "@/lib/db/schema";
import { eq} from "drizzle-orm";
import type { UserRow } from "./profile-role-label";
import { pgSchema, uuid, text } from "drizzle-orm/pg-core";
const USERS_SUBSCRIPTION_STATUS_ACTIVE = "active" as const;
const auth = pgSchema("auth");
/** Read-only model for Supabase Auth — not managed by Drizzle migrations. */
const authUsers = auth.table("users", {
id: uuid("id").primaryKey(),
email: text("email"),
});
/** Profiles joined to Supabase Auth emails and subscription status. */
export async function listUsersWithEmails(): Promise<UserRow[]> {
const rows = await db
.select({
id: profiles.id,
firstName: profiles.firstName,
lastName: profiles.lastName,
role: profiles.role,
lastLoginAt: profiles.lastLoginAt,
stripeCustomerId: profiles.stripeCustomerId,
subscriptionStatus: subscriptions.status,
email: authUsers.email,
})
.from(profiles)
.innerJoin(authUsers, eq(authUsers.id, profiles.id))
.leftJoin(subscriptions, eq(subscriptions.userId, profiles.id));
return rows.map((row) => ({
id: row.id,
firstName: row.firstName,
lastName: row.lastName,
role: row.role,
lastLoginAt: row.lastLoginAt,
stripeCustomerId: row.stripeCustomerId ?? null,
email: row.email ?? "",
isActive: row.subscriptionStatus === USERS_SUBSCRIPTION_STATUS_ACTIVE,
}));
}
/** Distinct `profiles.role` values present in the database. */
export async function listDistinctProfileRoles(): Promise<string[]> {
const rows = await db
.selectDistinct({ role: profiles.role })
.from(profiles)
.orderBy(profiles.role);
return rows.map((r) => r.role);
}