All files / routes/pro media.routes.ts

100% Statements 127/127
100% Branches 12/12
100% Functions 9/9
100% Lines 127/127

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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                                          1x                   8x 8x       1x 3x 3x 3x 3x     3x   1x 1x   2x         1x 2x 2x 2x 2x 2x 2x   2x     2x   2x           1x 1x     1x       1x   1x         1x       3x 3x 3x 3x 3x 3x   3x   3x 1x       2x   2x     1x 1x     1x       1x   1x           1x 3x 3x 3x 3x 3x 3x   3x     2x         1x     1x       1x     1x       1x   2x         1x 2x 2x 2x 2x 2x 2x     2x           2x         2x     1x     1x       1x   1x         1x 3x 3x 3x 3x 3x 3x   3x 3x   3x 1x       2x           2x   1x           1x       1x     1x       1x   1x         1x       3x 3x 3x 3x 3x 3x   3x   3x 1x             2x   2x     1x 1x     1x       1x   1x           1x 3x 3x 3x 3x 3x 3x   3x 3x   3x 1x       2x   2x     1x 1x     1x       1x   1x          
// Pro Media Routes - Manage room media (images and videos)
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 { parseIntId, parseRequiredId } from "../../lib/utils";
import { requireProAccess } from "../../middleware";
import { ValidationError } from "../../lib/errors";
import {
	recomputeProjectAfterContentChange,
	invalidateProjectsListCache,
} from "../../lib/project-mutations";
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 media = new Hono<Env>();
 
/**
 * Helper to get projectId from a roomId.
 * Uses the room service which already fetches the room.
 */
async function getProjectIdFromRoom(
	services: Services,
	roomId: number,
): Promise<string> {
	const room = await services.room.getById(roomId);
	return room.projectId;
}
 
// List media for a room
media.get("/:proId/rooms/:roomId/media", 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
		await services.media.verifyRoomOwnership(roomId, proId);
 
		const mediaList = await services.media.getByRoomId(roomId);
		return success(c, mediaList);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Add media to a room (metadata only - actual upload is handled by uploads route)
media.post("/:proId/rooms/:roomId/media", 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
		await services.media.verifyRoomOwnership(roomId, proId);
 
		const mediaItem = await services.media.create({
			...body,
			roomId,
		});
 
		// Re-compute enriched cache and cover image (non-blocking)
		const projectId = await getProjectIdFromRoom(services, roomId);
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(projectId, db, dal, services),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
 
		return success(c, mediaItem, 201);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Add multiple media to a room
media.post(
	"/:proId/rooms/:roomId/media/bulk",
	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 { items } = await c.req.json();
 
			if (!Array.isArray(items) || items.length === 0) {
				return handleError(c, new ValidationError("Items array is required"));
			}
 
			// Verify room belongs to pro
			await services.media.verifyRoomOwnership(roomId, proId);
 
			const mediaItems = await services.media.createMany(roomId, items);
 
			// Re-compute enriched cache and cover image (non-blocking)
			const projectId = await getProjectIdFromRoom(services, roomId);
			c.executionCtx.waitUntil(
				recomputeProjectAfterContentChange(projectId, db, dal, services),
			);
			c.executionCtx.waitUntil(
				invalidateProjectsListCache(c.get("cache"), proId),
			);
 
			return success(c, mediaItems, 201);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Update media (caption, alt text, etc.)
media.put("/:proId/media/:mediaId", 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 mediaId = parseRequiredId(c.req.param("mediaId"), "media");
 
		const body = await c.req.json();
 
		// Verify media belongs to pro (returns media with roomId)
		const existingMedia = await services.media.verifyProOwnership(
			mediaId,
			proId,
		);
 
		const mediaItem = await services.media.update(mediaId, body);
 
		// Re-compute enriched cache and cover image (non-blocking)
		const projectId = await getProjectIdFromRoom(
			services,
			existingMedia.roomId,
		);
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(projectId, db, dal, services),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
 
		return success(c, mediaItem);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Delete media
media.delete("/:proId/media/:mediaId", 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 mediaId = parseRequiredId(c.req.param("mediaId"), "media");
 
		// Verify media belongs to pro (returns media with roomId)
		const existingMedia = await services.media.verifyProOwnership(
			mediaId,
			proId,
		);
 
		// Get projectId before deleting (the room lookup needs to happen before delete)
		const projectId = await getProjectIdFromRoom(
			services,
			existingMedia.roomId,
		);
 
		await services.media.delete(mediaId);
 
		// 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),
		);
 
		return success(c, { message: "Media deleted successfully" });
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Move media to another room
media.post("/:proId/media/:mediaId/move", 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 mediaId = parseRequiredId(c.req.param("mediaId"), "media");
 
		const { roomId: newRoomId } = await c.req.json();
		const newRoomIdParsed = parseIntId(newRoomId);
 
		if (!newRoomIdParsed) {
			return handleError(c, new ValidationError("Invalid target room ID"));
		}
 
		// Verify media belongs to pro
		const existingMedia = await services.media.verifyProOwnership(
			mediaId,
			proId,
		);
 
		// Verify target room belongs to pro
		await services.media.verifyRoomOwnership(newRoomIdParsed, proId);
 
		const movedMedia = await services.media.moveToRoom(
			mediaId,
			newRoomIdParsed,
		);
 
		// Re-compute enriched cache and cover image (non-blocking) - same project for both rooms
		const projectId = await getProjectIdFromRoom(
			services,
			existingMedia.roomId,
		);
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(projectId, db, dal, services),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
 
		return success(c, movedMedia);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Reorder media in a room
media.post(
	"/:proId/rooms/:roomId/media/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 roomId = parseRequiredId(c.req.param("roomId"), "room");
 
			const { mediaIds } = await c.req.json();
 
			if (!Array.isArray(mediaIds) || mediaIds.length === 0) {
				return handleError(
					c,
					new ValidationError("Media IDs array is required"),
				);
			}
 
			// Verify room belongs to pro
			await services.media.verifyRoomOwnership(roomId, proId);
 
			await services.media.reorder(roomId, mediaIds);
 
			// Re-compute enriched cache and cover image (non-blocking)
			const projectId = await getProjectIdFromRoom(services, roomId);
			c.executionCtx.waitUntil(
				recomputeProjectAfterContentChange(projectId, db, dal, services),
			);
			c.executionCtx.waitUntil(
				invalidateProjectsListCache(c.get("cache"), proId),
			);
 
			return success(c, { message: "Media reordered successfully" });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Set cover image for a room
media.post("/:proId/rooms/:roomId/cover", 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 { mediaId } = await c.req.json();
		const mediaIdParsed = parseIntId(mediaId);
 
		if (!mediaIdParsed) {
			return handleError(c, new ValidationError("Invalid media ID"));
		}
 
		// Verify room belongs to pro
		await services.media.verifyRoomOwnership(roomId, proId);
 
		await services.media.setCoverImage(roomId, mediaIdParsed);
 
		// Re-compute enriched cache and cover image (non-blocking)
		const projectId = await getProjectIdFromRoom(services, roomId);
		c.executionCtx.waitUntil(
			recomputeProjectAfterContentChange(projectId, db, dal, services),
		);
		c.executionCtx.waitUntil(
			invalidateProjectsListCache(c.get("cache"), proId),
		);
 
		return success(c, { message: "Cover image set successfully" });
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default media;