All files / routes/pro/team team-status.ts

100% Statements 45/45
100% Branches 28/28
100% Functions 2/2
100% Lines 45/45

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                                          2x     2x     2x 10x 10x 10x 10x 10x     10x 1x               9x 9x 2x       7x 1x                 6x 1x                 5x 1x               4x 4x 1x       3x 3x   3x   3x                     2x 9x 9x 9x 9x     9x 2x               7x 7x 2x       5x 1x     4x 4x 1x       3x 3x   3x   3x                      
// Team Member Status - Deactivate and reactivate team members
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 teamStatus = new Hono<Env>();
 
// All routes require pro access
teamStatus.use("/:proId/*", requireProAccess);
 
// Deactivate a team member
teamStatus.post("/:proId/team/:roleId/deactivate", 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 deactivate team members
	if (proRole !== "owner") {
		return error(
			c,
			"FORBIDDEN",
			"Only owners can deactivate team members",
			403,
		);
	}
 
	const role = await dal.userTenantRoles.findById(roleId);
	if (!role || role.tenantId !== proId) {
		return error(c, "NOT_FOUND", "Team member not found", 404);
	}
 
	// Can't deactivate owner
	if (role.role === "owner") {
		return error(
			c,
			"CANNOT_DEACTIVATE_OWNER",
			"Cannot deactivate the owner",
			400,
		);
	}
 
	// Can't deactivate yourself
	if (role.userId === currentUser?.id) {
		return error(
			c,
			"CANNOT_DEACTIVATE_SELF",
			"Cannot deactivate yourself",
			400,
		);
	}
 
	// Already deactivated
	if (!role.isActive) {
		return error(
			c,
			"ALREADY_DEACTIVATED",
			"Team member is already deactivated",
			400,
		);
	}
 
	const updated = await dal.userTenantRoles.setActive(roleId, false);
	if (!updated) {
		return error(c, "UPDATE_FAILED", "Failed to deactivate team member", 500);
	}
 
	// Invalidate cached roles for the deactivated user
	const cache = createDualCache(c.env.KV_CACHE);
	await invalidateUserRoles(cache, role.userId);
 
	const user = await dal.users.findById(role.userId);
 
	return success(c, {
		id: updated.id,
		userId: updated.userId,
		role: updated.role,
		isActive: updated.isActive,
		user: user ? { id: user.id, name: user.name, email: user.email } : null,
		dateCreated: updated.dateCreated,
	});
});
 
// Reactivate a team member
teamStatus.post("/:proId/team/:roleId/reactivate", 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 reactivate team members
	if (proRole !== "owner") {
		return error(
			c,
			"FORBIDDEN",
			"Only owners can reactivate team members",
			403,
		);
	}
 
	const role = await dal.userTenantRoles.findById(roleId);
	if (!role || role.tenantId !== proId) {
		return error(c, "NOT_FOUND", "Team member not found", 404);
	}
 
	// Already active
	if (role.isActive) {
		return error(c, "ALREADY_ACTIVE", "Team member is already active", 400);
	}
 
	const updated = await dal.userTenantRoles.setActive(roleId, true);
	if (!updated) {
		return error(c, "UPDATE_FAILED", "Failed to reactivate team member", 500);
	}
 
	// Invalidate cached roles for the reactivated user
	const cacheForReactivate = createDualCache(c.env.KV_CACHE);
	await invalidateUserRoles(cacheForReactivate, role.userId);
 
	const user = await dal.users.findById(role.userId);
 
	return success(c, {
		id: updated.id,
		userId: updated.userId,
		role: updated.role,
		isActive: updated.isActive,
		user: user ? { id: user.id, name: user.name, email: user.email } : null,
		dateCreated: updated.dateCreated,
	});
});
 
export default teamStatus;