Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 89x 1x 7x 5x 1x 2x 2x 1x 2x 2x 2x 4x 4x 2x 2x 2x 2x 3x 3x 3x 2x 2x 2x 2x | // Data Access Layer for User Tenant Roles
import { eq, and } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type { UserTenantRole, NewUserTenantRole } from "../db/schema";
export class UserTenantRolesDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async findByUserId(userId: string): Promise<UserTenantRole[]> {
return this.db
.select()
.from(schema.userTenantRoles)
.where(eq(schema.userTenantRoles.userId, userId));
}
async findByUserIdAndTenantType(
userId: string,
tenantType: "platform" | "pro",
): Promise<UserTenantRole[]> {
return this.db
.select()
.from(schema.userTenantRoles)
.where(
and(
eq(schema.userTenantRoles.userId, userId),
eq(schema.userTenantRoles.tenantType, tenantType),
),
);
}
async findPlatformRoles(userId: string): Promise<UserTenantRole[]> {
return this.findByUserIdAndTenantType(userId, "platform");
}
async findProRoles(userId: string): Promise<UserTenantRole[]> {
return this.findByUserIdAndTenantType(userId, "pro");
}
async findByProId(
proId: string,
options?: { includeInactive?: boolean },
): Promise<UserTenantRole[]> {
const conditions = [
eq(schema.userTenantRoles.tenantType, "pro"),
eq(schema.userTenantRoles.tenantId, proId),
];
// By default, only return active members
if (!options?.includeInactive) {
conditions.push(eq(schema.userTenantRoles.isActive, true));
}
return this.db
.select()
.from(schema.userTenantRoles)
.where(and(...conditions));
}
async findUserProRole(
userId: string,
proId: string,
): Promise<UserTenantRole | undefined> {
const result = await this.db
.select()
.from(schema.userTenantRoles)
.where(
and(
eq(schema.userTenantRoles.userId, userId),
eq(schema.userTenantRoles.tenantType, "pro"),
eq(schema.userTenantRoles.tenantId, proId),
),
)
.limit(1);
return result[0];
}
async isPlatformAdmin(userId: string): Promise<boolean> {
const roles = await this.findPlatformRoles(userId);
return roles.some((r) => r.role === "admin" || r.role === "super_admin");
}
async create(data: NewUserTenantRole): Promise<UserTenantRole> {
const result = await this.db
.insert(schema.userTenantRoles)
.values(data)
.returning();
return result[0];
}
async delete(id: number): Promise<boolean> {
const result = await this.db
.delete(schema.userTenantRoles)
.where(eq(schema.userTenantRoles.id, id))
.returning();
return result.length > 0;
}
async getProUsersWithContact(
proId: string,
): Promise<
{ userId: string; email: string; name: string; phoneNumber: string | null; role: string }[]
> {
return this.db
.select({
userId: schema.users.id,
email: schema.users.email,
name: schema.users.name,
phoneNumber: schema.users.phoneNumber,
role: schema.userTenantRoles.role,
})
.from(schema.userTenantRoles)
.innerJoin(
schema.users,
eq(schema.userTenantRoles.userId, schema.users.id),
)
.where(
and(
eq(schema.userTenantRoles.tenantType, "pro"),
eq(schema.userTenantRoles.tenantId, proId),
eq(schema.userTenantRoles.isActive, true),
),
);
}
async setActive(
id: number,
isActive: boolean,
): Promise<UserTenantRole | null> {
const result = await this.db
.update(schema.userTenantRoles)
.set({ isActive })
.where(eq(schema.userTenantRoles.id, id))
.returning();
return result[0] ?? null;
}
async updateRole(
id: number,
role: string,
): Promise<UserTenantRole | null> {
const result = await this.db
.update(schema.userTenantRoles)
.set({ role })
.where(eq(schema.userTenantRoles.id, id))
.returning();
return result[0] ?? null;
}
async findById(id: number): Promise<UserTenantRole | undefined> {
const result = await this.db
.select()
.from(schema.userTenantRoles)
.where(eq(schema.userTenantRoles.id, id))
.limit(1);
return result[0];
}
}
|