fix: Instagram data collection pipeline — handle normalization + DB persistence

- Add normalizeInstagramHandle() utility (Edge + browser) to strip URLs, @ prefixes
- generate-report: normalize handles before saving, persist socialHandles in report JSONB
- enrich-channels: normalize Instagram handle before Apify call (defense in depth)
- useReport: recover socialHandles + channelEnrichment from DB on direct URL access
- ReportPage: skip redundant enrichment when data already exists in DB

Fixes: Instagram enrichment failing due to URL-format handles passed to Apify

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Haewon KamandClaude Opus 4.6 committed 2026-04-02 13:34:54 +09:00
1 parent 2d6e95c414
commit bd7bc45192
6 files changed
+156 -23

No files matched your search

@@ -0,0 +1,48 @@
/**
* Normalize an Instagram handle from various input formats to a pure username.
*
* Handles these formats:
* - "https://www.instagram.com/banobagi_ps/" → "banobagi_ps"
* - "https://instagram.com/banobagi_ps?hl=en" → "banobagi_ps"
* - "http://instagram.com/banobagi_ps" → "banobagi_ps"
* - "instagram.com/banobagi_ps" → "banobagi_ps"
* - "@banobagi_ps" → "banobagi_ps"
* - "banobagi_ps" → "banobagi_ps"
* - null / undefined / "" → null
*/
export function normalizeInstagramHandle(
raw: string | null | undefined,
): string | null {
if (!raw || typeof raw !== "string") return null;
let handle = raw.trim();
if (!handle) return null;
// If it contains "instagram.com", extract the first path segment
if (handle.includes("instagram.com")) {
try {
// Add protocol if missing so URL constructor works
const urlStr = handle.startsWith("http")
? handle
: `https://${handle}`;
const url = new URL(urlStr);
// pathname is like "/banobagi_ps/" or "/banobagi_ps"
const segments = url.pathname.split("/").filter(Boolean);
handle = segments[0] || "";
} catch {
// URL parsing failed — try regex fallback
const match = handle.match(/instagram\.com\/([^/?#]+)/);
handle = match?.[1] || "";
}
}
// Strip leading @
if (handle.startsWith("@")) {
handle = handle.slice(1);
}
// Strip trailing slash
handle = handle.replace(/\/+$/, "");
return handle || null;
}