feat: DB V3 dual-write — clinics + analysis_runs + channel_snapshots

Phase 2-4 of SaaS schema migration. All Edge Functions now write to
BOTH legacy marketing_reports AND new V3 tables:

discover-channels:
  - UPSERT clinics (url-based dedup)
  - INSERT analysis_runs (status: discovering)

collect-channel-data:
  - INSERT channel_snapshots (one per channel — time-series!)
  - INSERT screenshots (evidence rows)
  - UPDATE analysis_runs (raw_channel_data, vision_analysis)

generate-report:
  - UPDATE analysis_runs (report, status: complete)
  - UPDATE clinics (last_analyzed_at, established_year)

Frontend passes clinicId + runId through all 3 phases.
Legacy marketing_reports still written for backward compatibility.

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-05 00:51:11 +09:00
1 parent e011ef7357
commit 7fe3ff82c9
5 files changed
+184 -8

No files matched your search

@@ -486,6 +486,7 @@ Deno.serve(async (req) => {
onlinePresenceResearch: perplexityResearch,
};
// ─── Legacy: marketing_reports (backward compat) ───
const { data: saved, error: saveError } = await supabase
.from("marketing_reports")
.insert({
@@ -501,9 +502,62 @@ Deno.serve(async (req) => {
if (saveError) throw new Error(`DB save failed: ${saveError.message}`);
// ─── V3: clinics + analysis_runs (dual-write) ───
let clinicId: string | null = null;
let runId: string | null = null;
try {
// UPSERT clinic (url 기준 — 같은 URL이면 기존 행 업데이트)
const { data: clinicRow } = await supabase
.from("clinics")
.upsert({
url,
name: resolvedName,
name_en: clinic.clinicNameEn || null,
domain: new URL(url).hostname.replace('www.', ''),
address: clinic.address || null,
phone: clinic.phone || null,
services: clinic.services || [],
branding: brandData.data?.json || {},
social_handles: {
instagram: verified.instagram?.map((v: Record<string, unknown>) => v.handle) || [],
youtube: (verified.youtube as Record<string, unknown>)?.handle || null,
facebook: (verified.facebook as Record<string, unknown>)?.handle || null,
naverBlog: (verified.naverBlog as Record<string, unknown>)?.handle || null,
},
verified_channels: verified,
last_analyzed_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}, { onConflict: 'url' })
.select("id")
.single();
clinicId = clinicRow?.id || null;
// INSERT analysis_run
if (clinicId) {
const { data: runRow } = await supabase
.from("analysis_runs")
.insert({
clinic_id: clinicId,
status: "discovering",
scrape_data: scrapeDataFull,
discovered_channels: verified,
trigger: "manual",
pipeline_started_at: new Date().toISOString(),
})
.select("id")
.single();
runId = runRow?.id || null;
}
} catch (e) {
// V3 write failure should not block the pipeline
console.error("V3 dual-write error:", e);
}
return new Response(
JSON.stringify({
success: true, reportId: saved.id,
clinicId, runId, // V3 IDs for downstream phases
clinicName: resolvedName,
verifiedChannels: verified,
address: clinic.address || "",