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 | 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 6x 6x 5x 1x 1x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 2x 4x 4x 1x 3x 3x 1x 2x 2x 2x 2x 2x 2x | // Admin Project Routes
import { Hono } from "hono";
import { z } from "zod";
import type { Dal } from "../../dal";
import type { Services } from "../../services";
import {
success,
successWithPagination,
handleError,
} from "../../lib/response";
import { buildPaginationMeta, requireUser } from "../../lib/utils";
import { NotFoundError } from "../../lib/errors";
import { createDualCache, CACHE_KEYS } from "../../lib/cache";
import { logger } from "../../lib/logger";
type Env = {
Bindings: CloudflareBindings;
Variables: {
user: { id: string; name: string; email: string } | null;
session: unknown;
dal: Dal;
services: Services;
};
};
const projects = new Hono<Env>();
// List all projects with filters and pagination
// Supports both page-based (page=1) and offset-based (offset=0) pagination
projects.get("/", async (c) => {
try {
const services = c.get("services");
const limitParam = Number(c.req.query("limit") || 20);
const safeLimit = Math.min(100, Math.max(1, limitParam));
// Support both page-based and offset-based pagination
let page: number;
const offsetParam = c.req.query("offset");
const pageParam = c.req.query("page");
if (offsetParam !== undefined) {
// Offset-based pagination (used by portal infinite scroll)
const offset = Math.max(0, Number(offsetParam));
page = Math.floor(offset / safeLimit) + 1;
} else {
// Page-based pagination
page = Math.max(1, Number(pageParam || 1));
}
const filters = {
proId: c.req.query("proId"),
status: c.req.query("status"),
search: c.req.query("search"),
};
const { projects: data, total } = await services.project.list(
filters,
page,
safeLimit,
);
return successWithPagination(
c,
data,
buildPaginationMeta(total, page, safeLimit),
);
} catch (err) {
return handleError(c, err);
}
});
// Get single project by ID
projects.get("/:id", async (c) => {
try {
const services = c.get("services");
const project = await services.project.getById(c.req.param("id"));
return success(c, { ...project, photos: [] });
} catch (err) {
return handleError(c, err);
}
});
// Create new project
projects.post("/", async (c) => {
try {
const services = c.get("services");
const user = requireUser(c.get("user"));
const body = await c.req.json();
const project = await services.project.create(body, user.id);
return success(c, project, 201);
} catch (err) {
return handleError(c, err);
}
});
// Update project
projects.put("/:id", async (c) => {
try {
const services = c.get("services");
const user = requireUser(c.get("user"));
const body = await c.req.json();
const project = await services.project.update(
c.req.param("id"),
body,
user.id,
);
return success(c, project);
} catch (err) {
return handleError(c, err);
}
});
// Delete project
projects.delete("/:id", async (c) => {
try {
const services = c.get("services");
await services.project.delete(c.req.param("id"));
return success(c, { message: "Project deleted successfully" });
} catch (err) {
return handleError(c, err);
}
});
// Publish project
projects.post("/:id/publish", async (c) => {
try {
const services = c.get("services");
const user = requireUser(c.get("user"));
const project = await services.project.publish(c.req.param("id"), user.id);
return success(c, project);
} catch (err) {
return handleError(c, err);
}
});
// Archive project
projects.post("/:id/archive", async (c) => {
try {
const services = c.get("services");
const user = requireUser(c.get("user"));
const project = await services.project.archive(c.req.param("id"), user.id);
return success(c, project);
} catch (err) {
return handleError(c, err);
}
});
// Set featured status for a project.
// Mirrors the pros featured toggle pattern (pros.routes.ts POST /:id/featured).
// Uses PUT (idempotent toggle), validates body with Zod, routes through
// dal.projects.save so quality score recomputes (1B).
// Invalidates homepage + room-category cache after successful save (6B3).
const featuredSchema = z.object({
isFeatured: z.boolean(),
});
projects.put("/:id/featured", async (c) => {
try {
requireUser(c.get("user"));
const dal = c.get("dal");
const id = c.req.param("id");
const body = featuredSchema.safeParse(await c.req.json());
if (!body.success) {
return c.json({ success: false, error: { message: "isFeatured (boolean) is required" } }, 400);
}
// Verify project exists before mutating.
const existing = await dal.projects.findById(id);
if (!existing) {
throw new NotFoundError("Project", id);
}
// save() triggers quality score recompute (1B).
const updated = await dal.projects.save(id, { isFeatured: body.data.isFeatured });
if (!updated) {
throw new NotFoundError("Project", id);
}
// Invalidate cached homepage + room-category pages (6B3).
// Non-blocking: cache invalidation failures log + continue.
try {
const cache = createDualCache(c.env.KV_CACHE);
const hour = new Date().getUTCHours();
c.executionCtx.waitUntil(
Promise.all([
cache.delete(`${CACHE_KEYS.MARKETPLACE_HOMEPAGE}:h${hour}`),
cache.delete(`${CACHE_KEYS.MARKETPLACE_ROOM_CATEGORIES}:h${hour}`),
// Also invalidate the base keys (no hour suffix) for current cache pattern
cache.delete(CACHE_KEYS.MARKETPLACE_HOMEPAGE),
cache.delete(CACHE_KEYS.MARKETPLACE_ROOM_CATEGORIES),
]).catch((err) => {
logger.error("[admin/projects.featured] cache invalidation failed", err);
}),
);
} catch { /* executionCtx unavailable in tests */ }
return success(c, updated);
} catch (err) {
return handleError(c, err);
}
});
export default projects;
|