All files / routes/pro rooms.routes.ts

98.96% Statements 96/97
100% Branches 12/12
100% Functions 7/7
98.96% Lines 96/97

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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287                                                            1x     1x 4x 4x 4x 4x     4x     2x 1x   3x         1x 5x 5x 5x 5x     5x     2x   1x   4x         1x 3x 3x 3x 3x 3x 3x 3x     3x   3x           2x     2x         2x       2x   1x         1x 3x 3x 3x 3x 3x 3x   3x     3x   3x     2x               2x         2x               3x   1x         1x       4x 4x 4x 4x 4x 4x 4x   4x 2x             2x   2x     2x     2x           2x       2x               1x 3x 3x 3x 3x 3x 3x     3x   3x     2x               2x         2x               3x   1x         1x       5x 5x 5x 5x 5x 5x 5x   5x 2x             3x   2x     2x     2x             2x       2x   1x            
// Pro Room Routes - Manage project rooms
import { Hono } from "hono";
import type { Dal } from "../../dal";
import type { getDb } from "../../db";
import type { Services } from "../../services";
import { success, handleError } from "../../lib/response";
import { parseRequiredId } from "../../lib/utils";
import { requireProAccess } from "../../middleware";
import { ValidationError } from "../../lib/errors";
import {
	recomputeProjectAfterContentChange,
	invalidateProjectsListCache,
} from "../../lib/project-mutations";
import { invalidateEntity } from "../../lib/cache-invalidation";
import type { DualCache } from "../../lib/cache";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
		db: ReturnType<typeof getDb>;
		cache: DualCache;
		proId: string;
		proRole: string;
	};
};
 
const rooms = new Hono<Env>();
 
// List rooms for a project (with media counts)
rooms.get("/:proId/projects/:projectId/rooms", requireProAccess, async (c) => {
	try {
		const services = c.get("services");
		const proId = c.get("proId");
		const projectId = c.req.param("projectId");
 
		// Verify project belongs to pro
		await services.project.verifyProOwnership(projectId, proId);
 
		const roomsWithCounts =
			await services.room.getRoomsWithMediaCounts(projectId);
		return success(c, roomsWithCounts);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Get single room with media
rooms.get("/:proId/rooms/:roomId", requireProAccess, async (c) => {
	try {
		const services = c.get("services");
		const proId = c.get("proId");
		const roomId = parseRequiredId(c.req.param("roomId"), "room");
 
		// Verify room belongs to pro
		const room = await services.room.verifyProOwnership(roomId, proId);
 
		// Also fetch media
		const media = await services.media.getByRoomId(roomId);
 
		return success(c, { ...room, media });
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Create room in project
rooms.post("/:proId/projects/:projectId/rooms", requireProAccess, async (c) => {
	try {
		const services = c.get("services");
		const dal = c.get("dal");
		const db = c.get("db");
		const proId = c.get("proId");
		const projectId = c.req.param("projectId");
		const body = await c.req.json();
 
		// Verify project belongs to pro
		await services.project.verifyProOwnership(projectId, proId);
 
		const room = await services.room.create({
			...body,
			projectId,
		});
 
		// Re-compute enriched cache and cover image (non-blocking)
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(projectId, db, dal, services),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
		// Invalidate room detail+card point-keys (both keyed by room id) +
		// list gen. The id alone clears both; proId clears the parent pro caches.
		c.executionCtx.waitUntil(
			invalidateEntity(c.get("cache"), "room", { id: String(room.id), proId }),
		);
 
		return success(c, room, 201);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Update room
rooms.put("/:proId/rooms/:roomId", requireProAccess, async (c) => {
	try {
		const services = c.get("services");
		const dal = c.get("dal");
		const db = c.get("db");
		const proId = c.get("proId");
		const roomId = parseRequiredId(c.req.param("roomId"), "room");
 
		const body = await c.req.json();
 
		// Verify room belongs to pro (returns room with projectId)
		const existingRoom = await services.room.verifyProOwnership(roomId, proId);
 
		const room = await services.room.update(roomId, body);
 
		// Re-compute enriched cache and cover image (non-blocking)
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(
				existingRoom.projectId,
				db,
				dal,
				services,
			),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
		// Invalidate room detail point-keys. existingRoom has the slug (fetched
		// above by verifyProOwnership); id clears both detail and card caches.
		c.executionCtx.waitUntil(
			invalidateEntity(c.get("cache"), "room", {
				id: String(roomId),
				slug: existingRoom.slug ?? undefined,
				proId,
			}),
		);
 
		return success(c, room);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Set default room for a project
rooms.put(
	"/:proId/projects/:projectId/default-room",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const dal = c.get("dal");
			const db = c.get("db");
			const proId = c.get("proId");
			const projectId = c.req.param("projectId");
			const { roomId } = await c.req.json();
 
			if (!roomId || typeof roomId !== "number") {
				return handleError(
					c,
					new ValidationError("roomId is required and must be a number"),
				);
			}
 
			// Verify project belongs to pro
			await services.project.verifyProOwnership(projectId, proId);
 
			await services.room.setDefaultRoom(projectId, roomId);
 
			// Re-compute enriched cache and cover image (non-blocking)
			c.executionCtx.waitUntil(
				recomputeProjectAfterContentChange(projectId, db, dal, services),
			);
			c.executionCtx.waitUntil(
				invalidateProjectsListCache(c.get("cache"), proId),
			);
			// Invalidate the specific room's detail caches. The slug isn't cheaply
			// available here (only roomId from request body), so pass id+proId only.
			// The list gen bump in invalidateProjectsListCache covers list freshness.
			c.executionCtx.waitUntil(
				invalidateEntity(c.get("cache"), "room", { id: String(roomId), proId }),
			);
 
			return success(c, { message: "Default room updated" });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Delete room
rooms.delete("/:proId/rooms/:roomId", requireProAccess, async (c) => {
	try {
		const services = c.get("services");
		const dal = c.get("dal");
		const db = c.get("db");
		const proId = c.get("proId");
		const roomId = parseRequiredId(c.req.param("roomId"), "room");
 
		// Verify room belongs to pro (returns room with projectId)
		const existingRoom = await services.room.verifyProOwnership(roomId, proId);
 
		await services.room.delete(roomId);
 
		// Re-compute enriched cache and cover image (non-blocking)
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(
				existingRoom.projectId,
				db,
				dal,
				services,
			),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
		// Invalidate room detail point-keys. existingRoom has the slug (fetched
		// above by verifyProOwnership before delete), so both keys clear.
		c.executionCtx.waitUntil(
			invalidateEntity(c.get("cache"), "room", {
				id: String(roomId),
				slug: existingRoom.slug ?? undefined,
				proId,
			}),
		);
 
		return success(c, { message: "Room deleted successfully" });
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Reorder rooms in a project
rooms.post(
	"/:proId/projects/:projectId/rooms/reorder",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const dal = c.get("dal");
			const db = c.get("db");
			const proId = c.get("proId");
			const projectId = c.req.param("projectId");
			const { roomIds } = await c.req.json();
 
			if (!Array.isArray(roomIds) || roomIds.length === 0) {
				return handleError(
					c,
					new ValidationError("Room IDs array is required"),
				);
			}
 
			// Verify project belongs to pro
			await services.project.verifyProOwnership(projectId, proId);
 
			await services.room.reorder(projectId, roomIds);
 
			// Re-compute enriched cache and cover image (non-blocking)
			c.executionCtx.waitUntil(
				recomputeProjectAfterContentChange(projectId, db, dal, services),
			);
			c.executionCtx.waitUntil(
				invalidateProjectsListCache(c.get("cache"), proId),
			);
			// Reorder touches all rooms in the project but we don't have individual
			// slugs cheaply. The list gen bump above covers list freshness; for
			// detail caches, bump the room gen (via invalidateEntity without an id)
			// so point-keys become unreachable when next fetched.
			c.executionCtx.waitUntil(
				invalidateEntity(c.get("cache"), "room", { proId }),
			);
 
			return success(c, { message: "Rooms reordered successfully" });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default rooms;