All files / src/lib/seo interior-designers-redirect.ts

100% Statements 22/22
96.29% Branches 26/27
100% Functions 3/3
100% Lines 18/18

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                  1x                                                         15x 15x   15x         15x   15x 12x   11x   8x 1x     19x 15x 1x       6x   5x 15x 4x     2x    
/**
 * Resolves legacy `/interior-designers/*` paths to their live `/interiors/*`
 * equivalents. The URL prefix was renamed pre-launch; these 301s catch any
 * external link that still uses the old pattern.
 *
 * A city that is not live (or unknown) falls back to the Hyderabad hub so we
 * never 301 to a 404. Once a city is live, its own hub is the target.
 */
 
const HUB = "/interiors/hyderabad";
 
/**
 * Minimal structural view of the `getSeoCity` result — only the slug lists this
 * resolver needs to classify a path segment.
 */
export interface SeoCityClassification {
	localities: { slug: string | null; zoneSlug: string | null }[];
	zones: { slug: string | null }[];
	services: { slug: string }[];
	bhkTypes: { slug: string }[];
}
 
export interface LiveCityRedirect {
	id: string;
	slug: string;
}
 
/**
 * @param segments path parts after `/interior-designers/`, already lowercased
 * @param cityData SEO classification for the city in the path, or null
 * @param liveCity set when that city is live. Hyderabad is assumed live when
 *   omitted, which keeps the historical redirects.
 */
export function resolveInteriorDesignersRedirect(
	segments: string[],
	cityData: SeoCityClassification | null,
	liveCity?: LiveCityRedirect | null,
): string {
	const [city, seg, locality] = segments;
	const hyd = !city || city === "hyderabad";
	const own =
		!hyd &&
		liveCity &&
		(city === liveCity.id || city === liveCity.slug)
			? `/interiors/${liveCity.slug}`
			: null;
	const hub = own ?? HUB;
 
	if (!hyd && !own) return HUB;
	if (!cityData) return hub;
 
	if (!seg) return hub;
 
	if (locality) {
		return `${hub}/${seg}/${locality}`;
	}
 
	const matchedLocality = cityData.localities?.find((l) => l.slug === seg);
	if (matchedLocality?.zoneSlug) {
		return `${hub}/${matchedLocality.zoneSlug}/${seg}`;
	}
 
	const isSingleSegmentRoute =
		cityData.zones?.some((z) => z.slug === seg) ||
		cityData.services?.some((s) => s.slug === seg) ||
		cityData.bhkTypes?.some((b) => b.slug === seg);
	if (isSingleSegmentRoute) {
		return `${hub}/${seg}`;
	}
 
	return hub;
}