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 | 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 6x 6x 5x 1x 1x 2x 2x 2x 1x 1x 1x 3x 3x 3x 3x 3x 3x 2x 2x 2x 1x 1x 3x 3x 3x 3x 3x 3x 2x 2x 2x 1x 1x 4x 4x 4x 4x 4x 3x 2x 2x 2x 1x 3x 3x 3x 3x 3x 2x 3x 2x 2x 1x 1x 3x 3x 3x 3x 3x 2x 2x 2x 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 { CACHE_KEYS, createDualCache, type DualCache } from "../../lib/cache";
import { NotFoundError } from "../../lib/errors";
import { pingIndexNow } from "../../lib/indexnow";
import { logger } from "../../lib/logger";
import { invalidateProjectsListCache } from "../../lib/project-mutations";
import {
handleError,
success,
successWithPagination,
} from "../../lib/response";
import { buildPaginationMeta, requireUser } from "../../lib/utils";
import type { Services } from "../../services";
type Env = {
Bindings: CloudflareBindings;
Variables: {
user: { id: string; name: string; email: string } | null;
session: unknown;
dal: Dal;
services: Services;
cache: DualCache;
};
};
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 cache = c.get("cache");
const user = requireUser(c.get("user"));
const body = await c.req.json();
const project = await services.project.create(body, user.id);
// Invalidate marketplace list caches so the new project appears immediately.
Eif (project.proId) {
c.executionCtx.waitUntil(
invalidateProjectsListCache(cache, project.proId),
);
}
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 cache = c.get("cache");
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,
);
// Invalidate marketplace list caches for the owning pro.
Eif (project.proId) {
c.executionCtx.waitUntil(
invalidateProjectsListCache(cache, project.proId),
);
}
return success(c, project);
} catch (err) {
return handleError(c, err);
}
});
// Delete project
projects.delete("/:id", async (c) => {
try {
const services = c.get("services");
const cache = c.get("cache");
const id = c.req.param("id");
// Fetch proId before deleting so we can invalidate the right list cache.
const existing = await services.project.getById(id);
await services.project.delete(id);
c.executionCtx.waitUntil(
invalidateProjectsListCache(cache, existing.proId),
);
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 cache = c.get("cache");
const user = requireUser(c.get("user"));
const project = await services.project.publish(c.req.param("id"), user.id);
// Notify IndexNow that the project page is live (no-op until secret is set).
c.executionCtx.waitUntil(
pingIndexNow(c.env, [`/projects/${project.slug ?? c.req.param("id")}`]),
);
// Invalidate marketplace list caches so the newly published project appears.
if (project.proId) {
c.executionCtx.waitUntil(
invalidateProjectsListCache(cache, project.proId),
);
}
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 cache = c.get("cache");
const user = requireUser(c.get("user"));
const project = await services.project.archive(c.req.param("id"), user.id);
// Invalidate marketplace list caches so the archived project disappears.
Eif (project.proId) {
c.executionCtx.waitUntil(
invalidateProjectsListCache(cache, project.proId),
);
}
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),
// Bump the projects list generation — isFeatured drives list
// ordering/filtering and is a returned field, so the 24h-cached
// marketplace projects list must be invalidated on toggle.
invalidateProjectsListCache(cache, existing.proId),
]).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;
|