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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 218x 1x 10x 8x 1x 2x 2x 1x 2x 6x 6x 6x 6x 7x 7x 7x 2x 2x 2x 2x 3x 3x 3x 2x 2x 2x 2x | // Data Access Layer for User Tenant Roles
import { and, eq } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type { NewUserTenantRole, UserTenantRole } from "../db/schema";
import * as schema 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));
}
/**
* Any grant for this (user, pro) — **including a deactivated one**.
*
* NOT AN ACCESS CHECK. Use {@link findActiveUserProRole} for that. This
* answers "is there already a row for this pair", which is the question the
* three membership writers ask before inviting or adding someone
* (`routes/invitations.ts`, `routes/pro/team/team-members.ts`,
* `routes/admin/pros/team.routes.ts`): a deactivated member IS still a
* member, so they must match here or a re-invite would insert a second
* grant and trip `user_tenant_roles_grant_uniq` (migration 0025).
*/
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];
}
/**
* The user's ACTIVE grant for one pro, or undefined — the access check.
*
* `is_active = 0` is how a member is revoked without deleting the audit row
* (`POST /api/pro/:proId/team/:roleId/deactivate`), so a deactivated grant
* must confer nothing. Same rule as `isPlatformAdmin` above and
* `getProRoleFromRoles` on the cached path (`lib/role-cache.ts`); this is
* the uncached path, for the one route that authorises without
* `requireProAccess` in front of it (`GET /api/stats/project/:projectId`).
*
* `limit(1)` because every caller asks "do they have access", not "which of
* their roles is the most privileged" — the unique index constrains
* (user, tenant, role), so a user may legitimately hold more than one role
* for the same pro and any active one answers this question. Ranking lives
* in `getProRoleFromRoles`, which is the only place that needs it.
*/
async findActiveUserProRole(
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),
eq(schema.userTenantRoles.isActive, true),
),
)
.limit(1);
return result[0];
}
async isPlatformAdmin(userId: string): Promise<boolean> {
const roles = await this.findPlatformRoles(userId);
// A deactivated grant confers nothing: `is_active = 0` is how an admin
// is revoked without deleting the audit row. Same rule as
// `isPlatformAdminFromRoles` on the cached path (lib/role-cache.ts).
return roles.some(
(r) => (r.role === "admin" || r.role === "super_admin") && r.isActive,
);
}
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];
}
}
|