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 | 97x 36x 61x 61x 61x 61x 64x 6x 9x 4x 64x 64x 61x 61x 61x 61x 61x 61x 97x 97x 97x 97x 3x 61x 61x 3x 61x 61x 2x 61x 61x 1x 61x 61x 28x 61x 28x 61x 97x 97x 1x 61x 61x 61x 14x 13x 13x 13x 13x 7x 7x 7x 7x 13x 6x 6x 1x 13x 13x 13x 13x 4x 4x 4x 13x 14x 14x 7x 7x 7x 14x 61x 64x 64x 64x 6x 6x 9x 5x 4x 9x 64x 4x 4x 3x 3x 3x 64x 64x 30x 30x 64x 64x 61x 13x 14x 14x 50x 48x 48x 48x 48x 52x 52x 52x 48x 48x 19x 19x 19x 48x 50x 50x 50x 64x 61x 15x 15x 15x 15x 61x | // Project Enrichment Helper - Adds taxonomy data to projects
import { inArray } from "drizzle-orm";
import type { Dal } from "../../dal";
import type { Services } from "../../services";
import type { getDb } from "../../db";
import {
serviceCategories,
localities,
zones,
cities,
pros,
roomTypes,
type Project,
type ServiceCategory,
type Locality,
type Zone,
type City,
type Pro,
type Room,
type Media,
type RoomType,
} from "../../db/schema";
import { resolveProjectCover } from "../../services/cover-cascade";
import { logger } from "../../lib/logger";
// Extended room type with media
export type RoomWithMedia = Room & {
roomTypeName?: string | null;
coverImage?: string | null;
mediaCount?: number;
media?: Media[];
};
// Extended project type with resolved taxonomy data
export type ProjectWithTaxonomy = Project & {
coverImage?: string | null;
workedAreaNames?: string[];
localityName?: string | null;
pro?: {
id: string;
businessName: string;
slug: string | null;
profileImage: string | null;
logoUrl: string | null;
city: string | null;
} | null;
rooms?: RoomWithMedia[];
};
// Helper function to enrich projects with taxonomy data
export async function enrichProjectsWithTaxonomy(
db: ReturnType<typeof getDb>,
_services: Services,
dal: Dal,
projectsList: Project[],
includeRoomsAndMedia = false,
): Promise<ProjectWithTaxonomy[]> {
if (projectsList.length === 0) {
return [];
}
// Collect all unique IDs for batch lookups
const workedAreaIdsSet = new Set<string>();
const localityIds = new Set<string>();
const proIds = new Set<string>();
for (const project of projectsList) {
if (project.workedAreaIds && project.workedAreaIds.length > 0) {
for (const areaId of project.workedAreaIds) {
// Only add IDs that are actual service category IDs (not special values like full_office)
if (!areaId.startsWith("full_")) {
workedAreaIdsSet.add(areaId);
}
}
}
if (project.localityId) localityIds.add(project.localityId);
proIds.add(project.proId);
}
// Fetch all taxonomy data in parallel (skip empty queries)
const workedAreaIdsArr = Array.from(workedAreaIdsSet);
const localityIdsArr = Array.from(localityIds);
const proIdsArr = Array.from(proIds);
const [serviceCategoriesData, localitiesData, prosData] =
await Promise.all([
workedAreaIdsArr.length > 0
? db.select().from(serviceCategories).where(inArray(serviceCategories.id, workedAreaIdsArr))
: [],
localityIdsArr.length > 0
? db.select().from(localities).where(inArray(localities.id, localityIdsArr))
: [],
db.select().from(pros).where(inArray(pros.id, proIdsArr)),
]);
// Fetch zones and cities for localities
const zoneIds = new Set(localitiesData.map((l) => l.zoneId));
const zonesData =
zoneIds.size > 0
? await db
.select()
.from(zones)
.where(inArray(zones.id, Array.from(zoneIds)))
: [];
const cityIds = new Set(zonesData.map((z) => z.cityId));
const citiesData =
cityIds.size > 0
? await db
.select()
.from(cities)
.where(inArray(cities.id, Array.from(cityIds)))
: [];
// Create lookup maps
const serviceCategoriesMap = new Map<string, ServiceCategory>();
for (const sc of serviceCategoriesData) {
serviceCategoriesMap.set(sc.id, sc);
}
const localitiesMap = new Map<string, Locality>();
for (const loc of localitiesData) {
localitiesMap.set(loc.id, loc);
}
const zonesMap = new Map<string, Zone>();
for (const zone of zonesData) {
zonesMap.set(zone.id, zone);
}
const citiesMap = new Map<string, City>();
for (const city of citiesData) {
citiesMap.set(city.id, city);
}
const prosMap = new Map<string, Pro>();
for (const pro of prosData) {
// Project enrichment only reads scalar fields off the parent pro
// (id, businessName, slug, logo, profileImage, cityId), so attach
// empty arrays for the junction-derived overlay fields rather than
// paying for a hydration round-trip we wouldn't use.
prosMap.set(pro.id, {
...pro,
serviceCategoryIds: [],
materialTagIds: [],
serviceAreaIds: [],
});
}
// Fetch cities for pros (based on their cityId)
const proCityIds = new Set(
prosData.filter((v) => v.cityId).map((v) => v.cityId as string),
);
const proCitiesData =
proCityIds.size > 0
? await db
.select()
.from(cities)
.where(inArray(cities.id, Array.from(proCityIds)))
: [];
const proCitiesMap = new Map<string, City>();
for (const city of proCitiesData) {
proCitiesMap.set(city.id, city);
}
// Fetch rooms and media if requested
const roomsMap = new Map<string, RoomWithMedia[]>();
const roomTypesMap = new Map<string, RoomType>();
if (includeRoomsAndMedia) {
// Get all project IDs
const projectIds = projectsList.map((p) => p.id);
// Batch fetch all rooms for all projects in a single query
const allRooms = await dal.rooms.findByProjectIds(projectIds);
// Group rooms by projectId and collect room type codes
const roomsByProjectId = new Map<string, Room[]>();
const roomTypeCodes = new Set<string>();
for (const room of allRooms) {
const existing = roomsByProjectId.get(room.projectId) || [];
existing.push(room);
roomsByProjectId.set(room.projectId, existing);
roomTypeCodes.add(room.roomType);
}
// Fetch room types
if (roomTypeCodes.size > 0) {
const roomTypesData = await db
.select()
.from(roomTypes)
.where(inArray(roomTypes.code, Array.from(roomTypeCodes)));
for (const rt of roomTypesData) {
roomTypesMap.set(rt.code, rt);
}
}
// Fetch media for all rooms
const roomIds = allRooms.map((r) => r.id);
const allMedia =
roomIds.length > 0 ? await dal.media.findByRoomIds(roomIds) : [];
// Group media by room ID
const mediaByRoomId = new Map<number, Media[]>();
for (const m of allMedia) {
const existing = mediaByRoomId.get(m.roomId) || [];
existing.push(m);
mediaByRoomId.set(m.roomId, existing);
}
// Build rooms with media and room type names, grouped by project
for (const projectId of projectIds) {
const projectRooms = roomsByProjectId.get(projectId) || [];
const roomsWithMedia: RoomWithMedia[] = projectRooms.map((room) => {
const roomMedia = mediaByRoomId.get(room.id) || [];
const coverMedia =
roomMedia.find((m) => m.isCover) ?? roomMedia[0] ?? null;
return {
...room,
roomTypeName:
roomTypesMap.get(room.roomType)?.displayName || room.roomType,
coverImage: coverMedia ? coverMedia.storageKey : null,
mediaCount: roomMedia.length,
media: roomMedia,
};
});
roomsMap.set(projectId, roomsWithMedia);
}
}
// Enrich projects with taxonomy data
const enrichedProjects: ProjectWithTaxonomy[] = projectsList.map(
(project) => {
const enriched: ProjectWithTaxonomy = { ...project };
// Preserve denormalized coverImage as fallback; cascade overrides below
enriched.coverImage = project.coverImage ?? null;
// Add worked area names
if (project.workedAreaIds && project.workedAreaIds.length > 0) {
const specialAreaNames: Record<string, string> = {
full_office: "Full Office",
full_home: "Full Home",
};
enriched.workedAreaNames = project.workedAreaIds
.map((id) => {
// Handle special values
if (specialAreaNames[id]) {
return specialAreaNames[id];
}
// Look up service category name
return serviceCategoriesMap.get(id)?.name;
})
.filter((name): name is string => name !== undefined);
}
// Add locality with zone and city
if (project.localityId) {
const locality = localitiesMap.get(project.localityId);
if (locality) {
const zone = zonesMap.get(locality.zoneId);
const city = zone ? citiesMap.get(zone.cityId) : undefined;
enriched.localityName = `${locality.name}${zone ? `, ${zone.name}` : ""}${city ? `, ${city.name}` : ""}`;
}
}
// Add pro info
const pro = prosMap.get(project.proId);
if (pro) {
const proCity = pro.cityId
? proCitiesMap.get(pro.cityId)
: undefined;
enriched.pro = {
id: pro.id,
businessName: pro.businessName,
slug: pro.slug,
profileImage: pro.profileImage,
logoUrl: pro.logoUrl,
city: proCity?.name || null,
};
}
// Add rooms with media if available
if (includeRoomsAndMedia) {
/* v8 ignore start -- V8 artifact: || fallback never reached */
enriched.rooms = roomsMap.get(project.id) || [];
/* v8 ignore stop */
}
return enriched;
},
);
// Resolve cover images using the cascade resolver.
// For detail views (includeRoomsAndMedia=true): rooms+media are already loaded in roomsMap.
// For list views: batch-fetch all rooms and their media, then apply cascade.
if (includeRoomsAndMedia) {
// Rooms with media already populated — use cascade directly.
for (const ep of enrichedProjects) {
/* v8 ignore start -- V8 artifact: || fallback never reached */
const rooms = roomsMap.get(ep.id) || [];
/* v8 ignore stop */
// Cast to the expected shape (RoomWithMedia ensures media is present)
const cascadeCover = resolveProjectCover(
/* v8 ignore start -- V8 artifact: || fallback never reached */
rooms.map((r) => ({ ...r, media: r.media || [] })),
/* v8 ignore stop */
ep.defaultRoomId,
);
if (cascadeCover) ep.coverImage = cascadeCover;
}
} else {
// List view: batch-fetch rooms and media for cover cascade.
const allProjectIds = enrichedProjects.map((p) => p.id);
/* v8 ignore start -- V8 artifact: ternary false branch never reached */
const listRooms =
allProjectIds.length > 0
? await dal.rooms.findByProjectIds(allProjectIds)
: [];
/* v8 ignore stop */
const listRoomIds = listRooms.map((r) => r.id);
const listMedia =
listRoomIds.length > 0
? await dal.media.findByRoomIds(listRoomIds)
: [];
// Group media by room ID
const listMediaByRoomId = new Map<number, Media[]>();
for (const m of listMedia) {
const existing = listMediaByRoomId.get(m.roomId) || [];
existing.push(m);
listMediaByRoomId.set(m.roomId, existing);
}
// Group rooms (with media) by project ID
const listRoomsByProjectId = new Map<
string,
Array<Room & { media: Media[] }>
>();
for (const room of listRooms) {
const existing = listRoomsByProjectId.get(room.projectId) || [];
existing.push({ ...room, media: listMediaByRoomId.get(room.id) || [] });
listRoomsByProjectId.set(room.projectId, existing);
}
// Apply cascade to each enriched project (only override if cascade finds a cover)
for (const ep of enrichedProjects) {
const rooms = listRoomsByProjectId.get(ep.id) || [];
const cascadeCover = resolveProjectCover(rooms, ep.defaultRoomId);
if (cascadeCover) ep.coverImage = cascadeCover;
}
}
// Filter out projects without any images (safety net after cascade)
const withCover = enrichedProjects.filter((p) => p.coverImage != null);
if (withCover.length < enrichedProjects.length) {
const excluded = enrichedProjects
.filter((p) => p.coverImage == null)
.map((p) => `${p.title} (${p.id})`);
logger.warn(
`Filtered ${excluded.length} project(s) without cover images:`,
excluded.join(", "),
);
}
return withCover;
}
|