All files / routes/admin search.routes.ts

87.3% Statements 110/126
88.46% Branches 115/130
64% Functions 16/25
90.17% Lines 101/112

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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425                                                                                                    1x 1x   1x   1x 46x 46x 46x     46x   46x 37x 1x               36x   9x       45x 45x 45x 45x     45x                               46x                                     44x     44x     44x     44x     44x     44x     44x 1x           44x     44x     44x 44x 44x 44x           44x 44x 44x     44x 44x               44x 44x 44x 44x 20x 20x                             20x             44x     44x 65x   65x   18x                           18x         47x   14x 6x   14x 5x 5x                                     14x         33x       17x 7x   17x             16x 16x 17x 17x 2x 1x   2x 1x       15x 6x 6x 6x 6x                                   15x         16x       16x 7x 3x 1x 2x   16x                             16x             63x 63x     63x       62x             41x 1x     41x               4x                 1x 3x               3x             3x   12x 12x 11x 2x                         3x                    
// Admin Search Routes - Rebuild MiniSearch indexes and upload to R2
import { Hono } from "hono";
import type { Dal } from "../../dal";
import type { Services } from "../../services";
import { handleError } from "../../lib/response";
import { getDb } from "../../db";
import {
	businessTypes,
	serviceCategories,
	materialTags,
	cities,
	localities,
	zones,
	projects,
	proServiceCategories,
	proMaterialTags,
	proServiceAreas,
	type BusinessType,
	type ServiceCategory,
	type MaterialTag,
	type City,
	type Locality,
} from "../../db/schema";
import { and, desc, eq, inArray, isNotNull } from "drizzle-orm";
import { queryActiveTaxonomy } from "../taxonomy/helpers";
import { SearchService } from "../../lib/search/search-service";
import {
	buildProDocuments,
	buildProjectDocuments,
	buildRoomDocuments,
	buildBlogDocuments,
	groupRowsByProId,
	type ProRow,
	type ProjectRow,
	type RoomRow,
	type BlogRow,
} from "../../lib/search/index-builder";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
	};
};
 
type EntityType = "pro" | "project" | "room" | "blog";
 
const VALID_TYPES = new Set<EntityType>(["pro", "project", "room", "blog"]);
const ALL_TYPES: EntityType[] = ["pro", "project", "room", "blog"];
 
const searchAdmin = new Hono<Env>();
 
searchAdmin.post("/rebuild", async (c) => {
	try {
		const dal = c.get("dal");
		const db = getDb(c.env.DB);
 
		// Parse optional ?type= query param
		const typeParam = c.req.query("type");
		let typesToRebuild: EntityType[];
		if (typeParam) {
			if (!VALID_TYPES.has(typeParam as EntityType)) {
				return c.json(
					{
						success: false,
						error: `Invalid type: "${typeParam}". Valid types: pro, project, room, blog`,
					},
					400,
				);
			}
			typesToRebuild = [typeParam as EntityType];
		} else {
			typesToRebuild = [...ALL_TYPES];
		}
 
		// Fetch all taxonomy lookups needed upfront (only fetch what's needed)
		const needsPro = typesToRebuild.includes("pro");
		const needsProject = typesToRebuild.includes("project");
		const needsRoom = typesToRebuild.includes("room");
		const needsBlog = typesToRebuild.includes("blog");
 
		// Parallel-fetch all needed taxonomy + entity data
		const needsZones = needsProject || needsRoom;
		const [
			rawBusinessTypes,
			rawServiceCategories,
			rawMaterialTags,
			rawCities,
			rawLocalities,
			rawZones,
			rawRoomTypes,
			rawBlogCategories,
			rawPros,
			rawProjects,
			rawBlogs,
			rawProCategories,
			rawProMaterials,
			rawProAreas,
		] = await Promise.all([
			needsPro ? queryActiveTaxonomy<BusinessType>(db, businessTypes) : Promise.resolve([]),
			needsPro ? queryActiveTaxonomy<ServiceCategory>(db, serviceCategories) : Promise.resolve([]),
			needsProject ? queryActiveTaxonomy<MaterialTag>(db, materialTags) : Promise.resolve([]),
			(needsPro || needsBlog || needsProject || needsRoom) ? queryActiveTaxonomy<City>(db, cities) : Promise.resolve([]),
			(needsProject || needsRoom) ? db.select().from(localities) : Promise.resolve([]),
			needsZones ? db.select().from(zones) : Promise.resolve([]),
			needsRoom ? dal.roomTypes.findAll() : Promise.resolve([]),
			needsBlog ? dal.blogCategories.findAll({}, 0, 10000) : Promise.resolve([]),
			// Always fetch published pros — needed for visibility filtering of projects, rooms, and blogs
			dal.pros.findAll({ status: "published" }, 0, 100000),
			(needsProject || needsRoom) ? dal.projects.findAll({ status: "published" }, 0, 100000) : Promise.resolve([]),
			needsBlog ? dal.blogs.findAll({ status: "published" }, 0, 100000) : Promise.resolve([]),
			needsPro ? db.select().from(proServiceCategories) : Promise.resolve([]),
			needsPro ? db.select().from(proMaterialTags) : Promise.resolve([]),
			needsPro ? db.select().from(proServiceAreas) : Promise.resolve([]),
		]);
 
		// Build taxonomy lookup Maps
		const businessTypeMap = new Map<string, string>(
			rawBusinessTypes.map((bt) => [bt.id, bt.name]),
		);
		const serviceCategoryMap = new Map<string, string>(
			rawServiceCategories.map((sc) => [sc.id, sc.name]),
		);
		const materialTagMap = new Map<string, string>(
			rawMaterialTags.map((mt) => [mt.id, mt.name]),
		);
		const cityMap = new Map<string, string>(
			rawCities.map((c) => [c.id, c.name]),
		);
		const localityMap = new Map<string, string>(
			(rawLocalities as Locality[]).map((l) => [l.id, l.name]),
		);
		const roomTypeMap = new Map<string, string>(
			rawRoomTypes.map((rt) => [rt.code, rt.displayName]),
		);
		const blogCategoryMap = new Map<string, string>(
			rawBlogCategories.map((bc) => [bc.id, bc.name]),
		);
 
		// Style tags on projects and rooms are stored as raw string codes (not IDs),
		// so use an identity map: code → code (they're already human-readable).
		// This works because resolveIds will just return the code as-is.
		const styleTagIdentityMap = new Map<string, string>();
 
		// Materials on rooms are also stored as raw string codes (not IDs)
		const roomMaterialIdentityMap = new Map<string, string>();
 
		// Resolve project city via locality → zone → city for facetCityId.
		const zoneToCity = new Map<string, string>();
		for (const z of rawZones) zoneToCity.set(z.id, z.cityId);
		const localityToCity = new Map<string, string>();
		for (const l of rawLocalities as Locality[]) {
			const cid = zoneToCity.get(l.zoneId);
			if (cid) localityToCity.set(l.id, cid);
		}
 
		// Group junction rows into per-pro maps for buildProDocuments.
		const proCategoriesMap = groupRowsByProId(rawProCategories, (r) => r.categoryId);
		const proMaterialsMap = groupRowsByProId(rawProMaterials, (r) => r.tagId);
		const proAreasMap = groupRowsByProId(rawProAreas, (r) => r.localityId);
 
		// Build a set of published pro IDs for visibility filtering
		const publishedProIds = new Set(rawPros.map((p) => p.id));
		const proLookup = new Map(rawPros.map((p) => [p.id, p]));
 
		// Fetch first cover image per pro (for display in autocomplete).
		// Chunk the proId list so the `inArray(...)` bind count never
		// exceeds D1's 100-parameter statement limit (limit is actually
		// 100 per query, and we leave headroom for the two other
		// parameters on this WHERE). Without chunking, a growing pro
		// catalog would silently break the search index rebuild.
		const allProIds = rawPros.map((p) => p.id);
		const PRO_ID_CHUNK = 90;
		const firstCoverByProId = new Map<string, string>();
		for (let i = 0; i < allProIds.length; i += PRO_ID_CHUNK) {
			const chunk = allProIds.slice(i, i + PRO_ID_CHUNK);
			const coverPhotos = await db
				.select({
					proId: projects.proId,
					coverImage: projects.coverImage,
				})
				.from(projects)
				.where(
					and(
						inArray(projects.proId, chunk),
						eq(projects.status, "published"),
						isNotNull(projects.coverImage),
					),
				)
				.orderBy(desc(projects.dateCreated));
 
			for (const row of coverPhotos) {
				if (row.coverImage && !firstCoverByProId.has(row.proId)) {
					firstCoverByProId.set(row.proId, row.coverImage);
				}
			}
		}
 
		const stats: Record<string, { documents: number; size: string }> = {};
 
		// Rebuild each requested type
		for (const type of typesToRebuild) {
			let docs: ReturnType<typeof buildProDocuments> = [];
 
			if (type === "pro") {
				// Only published pros (already filtered by DAL query)
				const proRows: ProRow[] = rawPros.map((p) => ({
					id: p.id,
					slug: p.slug ?? p.id,
					businessName: p.businessName,
					description: p.description,
					businessTypeId: p.businessTypeId,
					cityId: p.cityId,
					serviceCategoryIds: proCategoriesMap.get(p.id) ?? [],
					materialTagIds: proMaterialsMap.get(p.id) ?? [],
					serviceAreaIds: proAreasMap.get(p.id) ?? [],
					logoUrl: p.logoUrl,
					profileImage: p.profileImage,
					coverImage: firstCoverByProId.get(p.id) ?? null,
				}));
				docs = buildProDocuments(proRows, {
					businessTypes: businessTypeMap,
					cities: cityMap,
					serviceCategories: serviceCategoryMap,
				});
			} else if (type === "project") {
				// Only published projects whose parent pro is also published
				const visibleProjects = rawProjects.filter(
					(p) => publishedProIds.has(p.proId),
				);
				const projectRows: ProjectRow[] = visibleProjects.map((p) => {
					const parentPro = proLookup.get(p.proId);
					return {
						id: p.id,
						slug: p.slug ?? p.id,
						title: p.title,
						description: p.description,
						propertyType: p.propertyType,
						localityId: p.localityId,
						cityId: p.localityId ? localityToCity.get(p.localityId) ?? null : null,
						styleTagIds: p.styleTagIds ? JSON.stringify(p.styleTagIds) : null,
						materialTagIds: p.materialTagIds
							? JSON.stringify(p.materialTagIds)
							: null,
						coverImage: p.coverImage,
						proName: parentPro?.businessName ?? null,
						proLogo: parentPro?.logoUrl ?? parentPro?.profileImage ?? null,
						proId: p.proId,
						qualityScore: p.qualityScore ?? 50,
					};
				});
				docs = buildProjectDocuments(projectRows, {
					localities: localityMap,
					styleTags: styleTagIdentityMap,
					materialTags: materialTagMap,
				});
			} else if (type === "room") {
				// Only rooms from published projects of published pros —
				// findPublishedRooms now resolves this via a correlated
				// EXISTS on projects/pros, no project-ID materialization.
				const visibleProjectLookup = new Map(
					rawProjects.filter((p) => publishedProIds.has(p.proId)).map((p) => [p.id, p]),
				);
				const { rooms: rawRooms } = await dal.rooms.findPublishedRooms(
					{},
					1,
					100000,
				);
 
				// Fetch room media for cover images
				const allRoomIds = rawRooms.map((r) => r.id);
				const roomMedia = allRoomIds.length > 0 ? await dal.media.findByRoomIds(allRoomIds) : [];
				const roomCoverMap = new Map<number, string>();
				for (const m of roomMedia) {
					if (!roomCoverMap.has(m.roomId)) {
						roomCoverMap.set(m.roomId, m.storageKey);
					}
					if (m.isCover) {
						roomCoverMap.set(m.roomId, m.storageKey);
					}
				}
 
				const roomRows: RoomRow[] = rawRooms.map((r) => {
					const parentProject = visibleProjectLookup.get(r.projectId);
					const parentPro = parentProject ? proLookup.get(parentProject.proId) : undefined;
					const localityId = parentProject?.localityId ?? null;
					return {
						id: String(r.id),
						slug: r.slug ?? String(r.id),
						name: r.name,
						description: r.description,
						roomType: r.roomType,
						styleTags: r.styleTags ? JSON.stringify(r.styleTags) : null,
						materials: r.materials ? JSON.stringify(r.materials) : null,
						coverImage: roomCoverMap.get(r.id) ?? null,
						proName: parentPro?.businessName ?? null,
						projectTitle: parentProject?.title ?? null,
						proLogo: parentPro?.logoUrl ?? parentPro?.profileImage ?? null,
						localityId,
						cityId: localityId ? localityToCity.get(localityId) ?? null : null,
						proId: parentProject?.proId ?? "",
						qualityScore: parentProject?.qualityScore ?? 50,
					};
				});
				docs = buildRoomDocuments(roomRows, {
					roomTypes: roomTypeMap,
					styleTags: styleTagIdentityMap,
					materialTags: roomMaterialIdentityMap,
				});
			E} else if (type === "blog") {
				// Only published blogs where:
				// - editorial blogs are always included
				// - pro-owned blogs included only if the owner pro is published
				const visibleBlogs = rawBlogs.filter((b) => {
					if (b.ideaSource === "editorial") return true;
					if (b.ideaSourceProId && publishedProIds.has(b.ideaSourceProId))
						return true;
					return false;
				});
				const blogRows: BlogRow[] = visibleBlogs.map((b) => ({
					id: b.id,
					slug: b.slug,
					title: b.title,
					metaDescription: b.metaDescription,
					primaryKeyword: b.primaryKeyword,
					secondaryKeywords: b.secondaryKeywords
						? JSON.stringify(b.secondaryKeywords)
						: null,
					categoryId: b.categoryId,
					cityId: b.cityId,
					featuredImageUrl: b.featuredImageUrl,
					readTimeMinutes: b.readTimeMinutes,
					categoryName: b.categoryId ? blogCategoryMap.get(b.categoryId) ?? null : null,
				}));
				docs = buildBlogDocuments(blogRows, {
					blogCategories: blogCategoryMap,
					cities: cityMap,
				});
			}
 
			// Build MiniSearch index
			const indexJson = SearchService.buildIndex(docs);
			const sizeKb = (Buffer.byteLength(indexJson, "utf8") / 1024).toFixed(1);
 
			// Upload to R2
			await c.env.R2.put(`search-indexes/${type}s.json`, indexJson, {
				httpMetadata: { contentType: "application/json" },
			});
 
			stats[type] = {
				documents: docs.length,
				size: `${sizeKb} KB`,
			};
		}
 
		// Update KV version key to invalidate cached SearchService instances
		if (c.env.KV_CACHE) {
			await c.env.KV_CACHE.put("search-index-version", Date.now().toString());
		}
 
		return c.json({
			success: true,
			data: {
				rebuilt: stats,
				timestamp: new Date().toISOString(),
			},
		});
	} catch (err) {
		return handleError(c, err);
	}
});
 
// #358: admin page used to show "not built yet" on every mount because it only
// captured rebuild responses in local state — it never asked R2 whether the
// indexes actually existed. The hourly cron does populate them (see
// `scheduled` handler in `apps/api/src/index.ts`), so this endpoint surfaces
// the real state via R2 `head()` per index.
searchAdmin.get("/status", async (c) => {
	try {
		const results: Record<
			EntityType,
			{
				exists: boolean;
				sizeBytes: number | null;
				lastModified: string | null;
			}
		> = {
			pro: { exists: false, sizeBytes: null, lastModified: null },
			project: { exists: false, sizeBytes: null, lastModified: null },
			room: { exists: false, sizeBytes: null, lastModified: null },
			blog: { exists: false, sizeBytes: null, lastModified: null },
		};
 
		await Promise.all(
			ALL_TYPES.map(async (type) => {
				try {
					const obj = await c.env.R2.head(`search-indexes/${type}s.json`);
					if (obj) {
						results[type] = {
							exists: true,
							sizeBytes: obj.size,
							// R2Object.uploaded is always present per Workers runtime types.
							lastModified: obj.uploaded.toISOString(),
						};
					}
				} catch {
					// Treat any head() failure as "not built" rather than 500ing the whole page.
				}
			}),
		);
 
		return c.json({
			success: true,
			data: { indexes: results },
		});
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default searchAdmin;