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 | 2x 2x 28x 28x 28x 28x 28x 28x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 1x 2x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 2x 4x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 2x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 2x 4x 4x 4x 4x 4x 4x 4x | import { Hono } from "hono";
import { eq, and, asc, inArray } from "drizzle-orm";
import { getDb } from "../../db";
import { cities, zones, localities, cityServices, cityBhkTypes, roomTypes, pageContent } from "../../db/schema";
import { success, handleError } from "../../lib/response";
import { createDualCache } from "../../lib/cache";
type Env = {
Bindings: CloudflareBindings;
};
// SEO content is admin-editable; PUT handlers invalidate these keys on write
const SEO_L1_TTL = 60; // 60s in-memory (capped by MAX_L1_TTL)
const SEO_L2_TTL = 86400; // 24h in KV
async function cachedSeoQuery<T>(
c: { env: CloudflareBindings; executionCtx: ExecutionContext },
key: string,
fetcher: () => Promise<T>,
): Promise<T> {
const cache = createDualCache(c.env.KV_CACHE);
const hit = await cache.get<T>(key);
Iif (hit) return hit;
const data = await fetcher();
c.executionCtx.waitUntil(cache.put(key, data, { l1Ttl: SEO_L1_TTL, l2Ttl: SEO_L2_TTL }));
return data;
}
const seo = new Hono<Env>();
// GET /seo/cities/:cityId — city hub page data
seo.get("/cities/:cityId", async (c) => {
try {
const cityId = c.req.param("cityId");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(c, `seo:city:${cityId}`, async () => {
const [cityRows, zoneRows, serviceRows, bhkRows] = await Promise.all([
db.select().from(cities).where(eq(cities.id, cityId)),
db.select().from(zones).where(and(eq(zones.cityId, cityId), eq(zones.isActive, true))).orderBy(asc(zones.sortOrder)),
db.select().from(cityServices).where(and(eq(cityServices.cityId, cityId), eq(cityServices.isActive, true))).orderBy(asc(cityServices.sortOrder)),
db.select().from(cityBhkTypes).where(and(eq(cityBhkTypes.cityId, cityId), eq(cityBhkTypes.isActive, true))).orderBy(asc(cityBhkTypes.sortOrder)),
]);
const city = cityRows[0];
if (!city) return null;
const zoneIds = zoneRows.map((z) => z.id);
const allLocalities =
zoneIds.length > 0
? await db
.select({ id: localities.id, slug: localities.slug, name: localities.name, zoneId: localities.zoneId })
.from(localities)
.where(and(inArray(localities.zoneId, zoneIds), eq(localities.isActive, true)))
.orderBy(asc(localities.sortOrder))
: [];
const zoneSlugById = new Map(zoneRows.map((z) => [z.id, z.slug]));
const localitiesWithZone = allLocalities.map((l) => ({
...l,
zoneSlug: zoneSlugById.get(l.zoneId) ?? "",
}));
return {
id: city.id,
name: city.name,
headline: city.headline,
intro: city.intro,
faqs: city.faqs ? JSON.parse(city.faqs) : [],
zones: zoneRows.map((z) => ({ id: z.id, name: z.name, slug: z.slug })),
services: serviceRows.map((s) => ({ id: s.id, slug: s.slug, name: s.name })),
bhkTypes: bhkRows.map((b) => ({ id: b.id, slug: b.slug, label: b.label })),
localities: localitiesWithZone,
};
});
if (!data) return c.json({ error: "City not found" }, 404);
return success(c, { city: data });
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/cities/:cityId/zones/:zoneSlug — zone page data
seo.get("/cities/:cityId/zones/:zoneSlug", async (c) => {
try {
const cityId = c.req.param("cityId");
const zoneSlug = c.req.param("zoneSlug");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(c, `seo:zone:${cityId}:${zoneSlug}`, async () => {
const zoneRows = await db
.select()
.from(zones)
.where(and(eq(zones.cityId, cityId), eq(zones.slug, zoneSlug)));
const zone = zoneRows[0];
if (!zone) return null;
const localityRows = await db
.select({ id: localities.id, slug: localities.slug, name: localities.name })
.from(localities)
.where(and(eq(localities.zoneId, zone.id), eq(localities.isActive, true)))
.orderBy(asc(localities.sortOrder));
return {
id: zone.id,
name: zone.name,
slug: zone.slug,
tier: zone.tier,
headline: zone.headline,
intro: zone.intro,
priceRange: zone.priceRange,
faqs: zone.faqs ? JSON.parse(zone.faqs) : [],
whatsappMessage: zone.whatsappMessage,
localities: localityRows,
};
});
if (!data) return c.json({ error: "Zone not found" }, 404);
return success(c, { zone: data });
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/cities/:cityId/zones/:zoneSlug/localities/:localitySlug — locality page data
seo.get("/cities/:cityId/zones/:zoneSlug/localities/:localitySlug", async (c) => {
try {
const cityId = c.req.param("cityId");
const zoneSlug = c.req.param("zoneSlug");
const localitySlug = c.req.param("localitySlug");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(
c,
`seo:locality:${cityId}:${zoneSlug}:${localitySlug}`,
async () => {
const zoneRows = await db
.select({ id: zones.id, name: zones.name, slug: zones.slug })
.from(zones)
.where(and(eq(zones.cityId, cityId), eq(zones.slug, zoneSlug)));
const zone = zoneRows[0];
if (!zone) return null;
const localityRows = await db
.select()
.from(localities)
.where(and(eq(localities.zoneId, zone.id), eq(localities.slug, localitySlug)));
const loc = localityRows[0];
if (!loc) return null;
return {
id: loc.id,
name: loc.name,
slug: loc.slug,
zoneId: loc.zoneId,
zoneSlug: zone.slug,
zoneName: zone.name,
tier: loc.tier,
intro: loc.intro,
pinCodes: loc.pinCodes ?? [],
landmarks: loc.landmarks ? JSON.parse(loc.landmarks) : [],
typicalHomeSize: loc.typicalHomeSize,
faqOverrides: loc.faqOverrides ? JSON.parse(loc.faqOverrides) : [],
};
},
);
if (!data) return c.json({ error: "Locality not found" }, 404);
return success(c, { locality: data });
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/cities/:cityId/services/:slug — service page data
seo.get("/cities/:cityId/services/:slug", async (c) => {
try {
const cityId = c.req.param("cityId");
const slug = c.req.param("slug");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(c, `seo:service:${cityId}:${slug}`, async () => {
const rows = await db
.select()
.from(cityServices)
.where(and(eq(cityServices.cityId, cityId), eq(cityServices.slug, slug)));
const svc = rows[0];
if (!svc) return null;
return {
id: svc.id,
slug: svc.slug,
name: svc.name,
headline: svc.headline,
intro: svc.intro,
priceRange: svc.priceRange,
priceNote: svc.priceNote,
included: svc.included ? JSON.parse(svc.included) : [],
excluded: svc.excluded ? JSON.parse(svc.excluded) : [],
timeline: svc.timeline,
faqs: svc.faqs ? JSON.parse(svc.faqs) : [],
whatsappMessage: svc.whatsappMessage,
};
});
if (!data) return c.json({ error: "Service not found" }, 404);
return success(c, { service: data });
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/cities/:cityId/faq-hub — FAQ hub page data (faqSections only)
seo.get("/cities/:cityId/faq-hub", async (c) => {
try {
const cityId = c.req.param("cityId");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(c, `seo:faq-hub:${cityId}`, async () => {
const rows = await db
.select({ faqSections: cities.faqSections })
.from(cities)
.where(eq(cities.id, cityId));
const city = rows[0];
if (!city) return null;
return {
faqSections: city.faqSections ? JSON.parse(city.faqSections) : [],
};
});
if (!data) return c.json({ error: "City not found" }, 404);
return success(c, data);
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/cities/:cityId/bhk/:slug — BHK type page data
seo.get("/cities/:cityId/bhk/:slug", async (c) => {
try {
const cityId = c.req.param("cityId");
const slug = c.req.param("slug");
const db = getDb(c.env.DB);
const data = await cachedSeoQuery(c, `seo:bhk:${cityId}:${slug}`, async () => {
const rows = await db
.select()
.from(cityBhkTypes)
.where(and(eq(cityBhkTypes.cityId, cityId), eq(cityBhkTypes.slug, slug)));
const bhk = rows[0];
if (!bhk) return null;
return {
id: bhk.id,
slug: bhk.slug,
label: bhk.label,
bhkCount: bhk.bhkCount,
headline: bhk.headline,
sqftRange: bhk.sqftRange,
priceRange: bhk.priceRange,
priceNote: bhk.priceNote,
timeline: bhk.timeline,
rooms: bhk.rooms ? JSON.parse(bhk.rooms) : [],
costTable: bhk.costTable ? JSON.parse(bhk.costTable) : [],
faqs: bhk.faqs ? JSON.parse(bhk.faqs) : [],
whatsappMessage: bhk.whatsappMessage,
};
});
if (!data) return c.json({ error: "BHK type not found" }, 404);
return success(c, { bhkType: data });
} catch (err) {
return handleError(c, err);
}
});
// URL category slug → DB room_type code (category aggregates multiple codes)
const CATEGORY_CODE_MAP: Record<string, string> = {
bedroom: "master_bedroom",
kids_room: "kids_bedroom",
dining: "dining_area",
office: "study",
};
// GET /seo/rooms/:category — FAQs for a room type category page
seo.get("/rooms/:category", async (c) => {
try {
const category = c.req.param("category");
const code = CATEGORY_CODE_MAP[category] ?? category;
const db = getDb(c.env.DB);
const faqs = await cachedSeoQuery(c, `seo:room:${category}`, async () => {
const rows = await db
.select({ faqs: roomTypes.faqs })
.from(roomTypes)
.where(eq(roomTypes.code, code));
return rows[0]?.faqs
? (JSON.parse(rows[0].faqs) as { question: string; answer: string }[])
: [];
});
return c.json({ faqs });
} catch (err) {
return handleError(c, err);
}
});
// GET /seo/pages/:pageKey — FAQs for static pages (e.g. how-it-works)
seo.get("/pages/:pageKey", async (c) => {
try {
const pageKey = c.req.param("pageKey");
const db = getDb(c.env.DB);
const faqs = await cachedSeoQuery(c, `seo:page:${pageKey}`, async () => {
const rows = await db
.select({ faqs: pageContent.faqs })
.from(pageContent)
.where(eq(pageContent.pageKey, pageKey));
return rows[0]?.faqs
? (JSON.parse(rows[0].faqs) as { question: string; answer: string }[])
: [];
});
return c.json({ faqs });
} catch (err) {
return handleError(c, err);
}
});
export default seo;
|