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

+16 -8
View File
@@ -34,24 +34,32 @@ const REPORT_SECTIONS = [
export default function ReportPage() {
const { id } = useParams<{ id: string }>();
const location = useLocation();
const { data: baseData, isLoading, error } = useReport(id);
const {
data: baseData,
isLoading,
error,
isEnriched,
socialHandles: dbSocialHandles,
} = useReport(id);
// Extract enrichment params from location state (socialHandles from API) or base data
// Build enrichment params — skip if already enriched (data from DB)
const enrichmentParams = useMemo(() => {
if (!baseData) return null;
if (!baseData || isEnriched) return null;
// Priority: location.state socialHandles > DB socialHandles > transformed data
const state = location.state as Record<string, unknown> | undefined;
const metadata = state?.metadata as Record<string, unknown> | undefined;
const socialHandles = metadata?.socialHandles as Record<string, string | null> | undefined;
const stateSocialHandles = metadata?.socialHandles as Record<string, string | null> | undefined;
const handles = stateSocialHandles || dbSocialHandles;
// Priority: API socialHandles > transformed data > undefined
const igHandle =
socialHandles?.instagram ||
handles?.instagram ||
baseData.instagramAudit?.accounts?.[0]?.handle ||
undefined;
const ytHandle =
socialHandles?.youtube ||
handles?.youtube ||
baseData.youtubeAudit?.handle ||
undefined;
@@ -62,7 +70,7 @@ export default function ReportPage() {
youtubeChannelId: ytHandle || undefined,
address: baseData.clinicSnapshot.location || undefined,
};
}, [baseData, location.state]);
}, [baseData, isEnriched, dbSocialHandles, location.state]);
const { status: enrichStatus, enrichedReport } = useEnrichment(baseData, enrichmentParams);