All files / routes/admin cache.routes.ts

94.44% Statements 17/18
80% Branches 8/10
100% Functions 2/2
94.11% Lines 16/17

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                                    1x                                         1x                   1x   6x 6x 1x                           5x 5x         5x 2x                         3x 54x     3x 3x 6x         3x                    
import { Hono } from "hono";
import type { Dal } from "../../dal";
import type { Services } from "../../services";
import { CACHE_KEYS, createDualCache } from "../../lib/cache";
import { purgeMarketplaceCache } from "../../lib/edge-cache";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
		cache: import("../../lib/cache").DualCache;
	};
};
 
/** All known static cache keys to purge */
const PURGE_KEYS = [
	CACHE_KEYS.MARKETPLACE_HOMEPAGE,
	CACHE_KEYS.MARKETPLACE_ROOM_CATEGORIES,
	CACHE_KEYS.MARKETPLACE_STATS,
	CACHE_KEYS.TAXONOMY_ALL,
	CACHE_KEYS.TAXONOMY_CRITICAL,
	CACHE_KEYS.TAXONOMY_EXTENDED,
	CACHE_KEYS.TAXONOMY_BUSINESS_TYPES,
	CACHE_KEYS.TAXONOMY_EXECUTION_MODELS,
	CACHE_KEYS.TAXONOMY_CUSTOMER_SEGMENTS,
	CACHE_KEYS.TAXONOMY_PROJECT_SCALES,
	CACHE_KEYS.TAXONOMY_SERVICE_CATEGORIES,
	CACHE_KEYS.TAXONOMY_MATERIAL_TAGS,
	CACHE_KEYS.TAXONOMY_STYLE_TAGS,
	CACHE_KEYS.TAXONOMY_BRANDS,
	CACHE_KEYS.TAXONOMY_CITIES,
	CACHE_KEYS.TAXONOMY_ROOM_TYPES,
	CACHE_KEYS.TAXONOMY_TIMELINE_CATEGORIES,
	CACHE_KEYS.TAXONOMY_ENRICHMENT,
];
 
const cacheRoutes = new Hono<Env>();
 
/**
 * POST /api/admin/cache/purge
 * Purge all cache layers (L1 in-memory, L2 KV, edge cache).
 * Blocked in production — intended for E2E testing only.
 *
 * Body: { scope: "all" }
 * Returns: { success: true, data: { purged: [...], timestamp: "..." } }
 */
cacheRoutes.post("/purge", async (c) => {
	// Safety: block in production
	const environment = c.env.ENVIRONMENT;
	if (environment === "production") {
		return c.json(
			{
				success: false,
				error: {
					code: "FORBIDDEN_IN_PRODUCTION",
					message: "Cache purge is not allowed in production",
				},
			},
			403,
		);
	}
 
	// Validate scope
	let body: { scope?: string };
	try {
		body = await c.req.json();
	} catch {
		body = {};
	}
 
	if (!body.scope || body.scope !== "all") {
		return c.json(
			{
				success: false,
				error: {
					code: "INVALID_SCOPE",
					message: 'scope must be "all"',
				},
			},
			400,
		);
	}
 
	// Purge L1 + L2 via DualCache
	const cache = c.get("cache") ?? createDualCache(c.env.KV_CACHE);
	await Promise.all(PURGE_KEYS.map((key) => cache.delete(key)));
 
	// Attempt edge cache purge (no-op in local/test where caches is undefined)
	try {
		const baseUrl = c.env.BETTER_AUTH_URL || c.req.url.replace(/\/api\/.*/, "");
		await purgeMarketplaceCache(baseUrl);
	} catch {
		// Edge cache purge failure is non-fatal
	}
 
	return c.json({
		success: true,
		data: {
			purged: [...PURGE_KEYS],
			timestamp: new Date().toISOString(),
		},
	});
});
 
export default cacheRoutes;