All files / routes/taxonomy batch.routes.ts

100% Statements 35/35
100% Branches 6/6
100% Functions 3/3
100% Lines 35/35

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                                                                                                                                  2x     2x 4x 4x 4x   4x     4x 1x                   3x                 2x                 2x             2x   1x         2x 6x 6x   6x     6x 2x                 4x               3x               3x       3x   1x         2x 6x 6x   6x     6x 2x               4x             3x             3x       3x   1x          
// Taxonomy Batch Routes - /all, /critical, /extended aggregate endpoints
import { Hono } from "hono";
import { getDb } from "../../db";
import {
	businessTypes,
	timelineCategories,
	customerSegments,
	projectScales,
	serviceCategories,
	materialTags,
	brands,
	cities,
	roomTypes,
	type BusinessType,
	type TimelineCategory,
	type CustomerSegment,
	type ProjectScale,
	type ServiceCategory,
	type MaterialTag,
	type Brand,
	type City,
	type RoomType,
} from "../../db/schema";
import { success, handleError } from "../../lib/response";
import {
	createDualCache,
	cache,
	CACHE_KEYS,
	CACHE_TTL,
	TAXONOMY_CACHE_TTL,
} from "../../lib/cache";
import {
	queryActiveTaxonomy,
	groupByCategory,
	buildHierarchy,
} from "./helpers";
 
type Env = {
	Bindings: CloudflareBindings;
};
 
interface TaxonomyAllResponse {
	businessTypes: BusinessType[];
	customerSegments: CustomerSegment[];
	serviceCategories: (ServiceCategory & { children?: ServiceCategory[] })[];
	materialTags: Record<string, MaterialTag[]>;
	brands: Record<string, Brand[]>;
	cities: City[];
}
 
interface TaxonomyCriticalResponse {
	cities: City[];
	businessTypes: BusinessType[];
	customerSegments: CustomerSegment[];
	roomTypes: RoomType[];
	projectScales: ProjectScale[];
}
 
interface TaxonomyExtendedResponse {
	materialTags: Record<string, MaterialTag[]>;
	brands: Record<string, Brand[]>;
	serviceCategories: (ServiceCategory & { children?: ServiceCategory[] })[];
	timelineCategories: TimelineCategory[];
}
 
const batch = new Hono<Env>();
 
// GET /all - All taxonomy data in one call (dual-layer cached)
batch.get("/all", async (c) => {
	try {
		const db = getDb(c.env.DB);
		const dualCache = createDualCache(c.env.KV_CACHE);
 
		const cached = await dualCache.get<TaxonomyAllResponse>(
			CACHE_KEYS.TAXONOMY_ALL,
		);
		if (cached) {
			return success(c, cached);
		}
 
		const [
			businessTypesData,
			customerSegmentsData,
			serviceCategoriesData,
			materialTagsData,
			brandsData,
			citiesData,
		] = await Promise.all([
			queryActiveTaxonomy<BusinessType>(db, businessTypes),
			queryActiveTaxonomy<CustomerSegment>(db, customerSegments),
			queryActiveTaxonomy<ServiceCategory>(db, serviceCategories),
			queryActiveTaxonomy<MaterialTag>(db, materialTags),
			queryActiveTaxonomy<Brand>(db, brands),
			queryActiveTaxonomy<City>(db, cities),
		]);
 
		const responseData: TaxonomyAllResponse = {
			businessTypes: businessTypesData,
			customerSegments: customerSegmentsData,
			serviceCategories: buildHierarchy(serviceCategoriesData),
			materialTags: groupByCategory(materialTagsData),
			brands: groupByCategory(brandsData),
			cities: citiesData,
		};
 
		c.executionCtx.waitUntil(
			dualCache.put(CACHE_KEYS.TAXONOMY_ALL, responseData, {
				l1Ttl: CACHE_TTL.TAXONOMY_L1,
				l2Ttl: CACHE_TTL.TAXONOMY_L2,
			}),
		);
 
		return success(c, responseData);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// GET /critical - Critical taxonomy data for app init (cached)
batch.get("/critical", async (c) => {
	try {
		const db = getDb(c.env.DB);
 
		const cached = await cache.get<TaxonomyCriticalResponse>(
			CACHE_KEYS.TAXONOMY_CRITICAL,
		);
		if (cached) {
			return success(c, cached);
		}
 
		const [
			citiesData,
			businessTypesData,
			customerSegmentsData,
			roomTypesData,
			projectScalesData,
		] = await Promise.all([
			queryActiveTaxonomy<City>(db, cities),
			queryActiveTaxonomy<BusinessType>(db, businessTypes),
			queryActiveTaxonomy<CustomerSegment>(db, customerSegments),
			queryActiveTaxonomy<RoomType>(db, roomTypes),
			queryActiveTaxonomy<ProjectScale>(db, projectScales),
		]);
 
		const responseData: TaxonomyCriticalResponse = {
			cities: citiesData,
			businessTypes: businessTypesData,
			customerSegments: customerSegmentsData,
			roomTypes: roomTypesData,
			projectScales: projectScalesData,
		};
 
		await cache.put(CACHE_KEYS.TAXONOMY_CRITICAL, responseData, {
			expirationTtl: TAXONOMY_CACHE_TTL,
		});
 
		return success(c, responseData);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// GET /extended - Extended taxonomy data, lazy-loaded (cached)
batch.get("/extended", async (c) => {
	try {
		const db = getDb(c.env.DB);
 
		const cached = await cache.get<TaxonomyExtendedResponse>(
			CACHE_KEYS.TAXONOMY_EXTENDED,
		);
		if (cached) {
			return success(c, cached);
		}
 
		const [
			materialTagsData,
			brandsData,
			serviceCategoriesData,
			timelineCategoriesData,
		] = await Promise.all([
			queryActiveTaxonomy<MaterialTag>(db, materialTags),
			queryActiveTaxonomy<Brand>(db, brands),
			queryActiveTaxonomy<ServiceCategory>(db, serviceCategories),
			queryActiveTaxonomy<TimelineCategory>(db, timelineCategories),
		]);
 
		const responseData: TaxonomyExtendedResponse = {
			materialTags: groupByCategory(materialTagsData),
			brands: groupByCategory(brandsData),
			serviceCategories: buildHierarchy(serviceCategoriesData),
			timelineCategories: timelineCategoriesData,
		};
 
		await cache.put(CACHE_KEYS.TAXONOMY_EXTENDED, responseData, {
			expirationTtl: TAXONOMY_CACHE_TTL,
		});
 
		return success(c, responseData);
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default batch;