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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 2x 2x 2x 7x 7x 7x 7x 7x 2x 5x 5x 5x 5x 5x 2x 8x 8x 8x 8x 1x 7x 7x 1x 6x 1x 5x 5x 1x 4x 4x 1x 3x 3x 1x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 1x 7x 7x 1x 6x 11x 6x 1x 5x 1x 4x 4x 1x 3x 3x 3x 3x 2x 7x 7x 7x 7x 7x 7x 1x 6x 10x 6x 1x 5x 1x 4x 1x 3x 3x 1x 2x 2x 2x | // Team Members - CRUD operations
import { Hono } from "hono";
import type { Dal } from "../../../dal";
import type { Services } from "../../../services";
import { success, error } from "../../../lib/response";
import { requireProAccess } from "../../../middleware";
import { createDualCache } from "../../../lib/cache";
import { invalidateUserRoles } from "../../../lib/role-cache";
type Env = {
Bindings: CloudflareBindings;
Variables: {
user: { id: string; name: string; email: string } | null;
session: unknown;
dal: Dal;
services: Services;
proId: string;
proRole: string;
};
};
const teamMembers = new Hono<Env>();
// All routes require pro access
teamMembers.use("/:proId/*", requireProAccess);
// Get team members for a pro
teamMembers.get("/:proId/team", async (c) => {
const dal = c.get("dal");
const proId = c.req.param("proId");
const proRole = c.get("proRole");
const includeInactive = c.req.query("includeInactive") === "true";
// Only owners and managers can view team
if (!["owner", "manager"].includes(proRole)) {
return error(
c,
"FORBIDDEN",
"Only owners and managers can view team members",
403,
);
}
// Get all roles for this pro
const roles = await dal.userTenantRoles.findByProId(proId, {
includeInactive,
});
// Get user details for each role
const members = await Promise.all(
roles.map(async (role) => {
const user = await dal.users.findById(role.userId);
return {
id: role.id,
userId: role.userId,
role: role.role,
isActive: role.isActive,
user: user
? {
id: user.id,
name: user.name,
email: user.email,
}
: null,
dateCreated: role.dateCreated,
};
}),
);
return success(c, members);
});
// Add a team member to pro
teamMembers.post("/:proId/team", async (c) => {
const dal = c.get("dal");
const proId = c.req.param("proId");
const proRole = c.get("proRole");
// Only owners can add team members
if (proRole !== "owner") {
return error(c, "FORBIDDEN", "Only owners can add team members", 403);
}
const body = await c.req.json<{
email: string;
role: "owner" | "manager" | "staff";
}>();
if (!body.email) {
return error(c, "MISSING_EMAIL", "Email is required", 400);
}
if (!["owner", "manager", "staff"].includes(body.role)) {
return error(c, "INVALID_ROLE", "Role must be owner, manager, or staff", 400);
}
// Find user by email
const user = await dal.users.findByEmail(body.email);
if (!user) {
return error(c, "USER_NOT_FOUND", "No user found with that email", 404);
}
// Check if user already has a role for this pro
const existingRole = await dal.userTenantRoles.findUserProRole(
user.id,
proId,
);
if (existingRole) {
return error(c, "ALREADY_MEMBER", "User is already a team member", 400);
}
// Check if user belongs to any other pro
const proRoles = await dal.userTenantRoles.findProRoles(user.id);
if (proRoles.length > 0) {
return error(
c,
"ALREADY_IN_PRO",
"This user is already part of another organization",
409,
);
}
// Add the role
const newRole = await dal.userTenantRoles.create({
userId: user.id,
tenantType: "pro",
tenantId: proId,
role: body.role,
});
// Invalidate cached roles for the affected user
const cache = createDualCache(c.env.KV_CACHE);
await invalidateUserRoles(cache, user.id);
return success(
c,
{
id: newRole.id,
userId: user.id,
role: newRole.role,
isActive: newRole.isActive,
user: {
id: user.id,
name: user.name,
email: user.email,
},
dateCreated: newRole.dateCreated,
},
201,
);
});
// Update a team member's role
teamMembers.put("/:proId/team/:roleId", async (c) => {
const dal = c.get("dal");
const proId = c.req.param("proId");
const roleId = parseInt(c.req.param("roleId"), 10);
const proRole = c.get("proRole");
// Only owners can update roles
if (proRole !== "owner") {
return error(
c,
"FORBIDDEN",
"Only owners can update team member roles",
403,
);
}
const body = await c.req.json<{
role: "manager" | "staff";
}>();
if (!["manager", "staff"].includes(body.role)) {
return error(c, "INVALID_ROLE", "Role must be manager or staff", 400);
}
// Get the existing role
const roles = await dal.userTenantRoles.findByProId(proId);
const existingRole = roles.find((r) => r.id === roleId);
if (!existingRole) {
return error(c, "NOT_FOUND", "Team member not found", 404);
}
// Can't change owner's role
if (existingRole.role === "owner") {
return error(c, "CANNOT_MODIFY_OWNER", "Cannot modify owner's role", 400);
}
// Update role in-place (single UPDATE query, no gap in access)
const newRole = await dal.userTenantRoles.updateRole(roleId, body.role);
if (!newRole) {
return error(c, "UPDATE_FAILED", "Failed to update role", 500);
}
// Invalidate cached roles for the affected user
const cacheForUpdate = createDualCache(c.env.KV_CACHE);
await invalidateUserRoles(cacheForUpdate, existingRole.userId);
const user = await dal.users.findById(existingRole.userId);
return success(c, {
id: newRole.id,
userId: existingRole.userId,
role: newRole.role,
isActive: newRole.isActive,
user: user
? {
id: user.id,
name: user.name,
email: user.email,
}
: null,
dateCreated: newRole.dateCreated,
});
});
// Remove a team member
teamMembers.delete("/:proId/team/:roleId", async (c) => {
const dal = c.get("dal");
const proId = c.req.param("proId");
const roleId = parseInt(c.req.param("roleId"), 10);
const proRole = c.get("proRole");
const currentUser = c.get("user");
// Only owners can remove team members
if (proRole !== "owner") {
return error(c, "FORBIDDEN", "Only owners can remove team members", 403);
}
// Get the role to be deleted
const roles = await dal.userTenantRoles.findByProId(proId, {
includeInactive: true,
});
const roleToDelete = roles.find((r) => r.id === roleId);
if (!roleToDelete) {
return error(c, "NOT_FOUND", "Team member not found", 404);
}
// Can't remove owner
if (roleToDelete.role === "owner") {
return error(c, "CANNOT_REMOVE_OWNER", "Cannot remove the owner", 400);
}
// Can't remove yourself
if (roleToDelete.userId === currentUser?.id) {
return error(
c,
"CANNOT_REMOVE_SELF",
"Cannot remove yourself from the team",
400,
);
}
const deleted = await dal.userTenantRoles.delete(roleId);
if (!deleted) {
return error(c, "DELETE_FAILED", "Failed to remove team member", 500);
}
// Invalidate cached roles for the removed user
const cacheForDelete = createDualCache(c.env.KV_CACHE);
await invalidateUserRoles(cacheForDelete, roleToDelete.userId);
return success(c, { message: "Team member removed successfully" });
});
export default teamMembers;
|