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

@@ -12,6 +12,9 @@ const corsHeaders = {
interface ReportRequest {
// V2: reportId-based (Phase 3 — uses data already in DB)
reportId?: string;
// V3: clinic + run IDs for new schema
clinicId?: string;
runId?: string;
// V1 compat: url-based (legacy single-call flow)
url?: string;
clinicName?: string;
@@ -153,6 +156,7 @@ ${JSON.stringify(scrapeData.branding || {}, null, 2).slice(0, 1000)}
facebook: verified.facebook?.verified ? verified.facebook.handle : null,
};
// Legacy: marketing_reports
await supabase.from("marketing_reports").update({
report,
status: "complete",
@@ -160,6 +164,28 @@ ${JSON.stringify(scrapeData.branding || {}, null, 2).slice(0, 1000)}
updated_at: new Date().toISOString(),
}).eq("id", body.reportId);
// V3: analysis_runs + clinics
const v3RunId = body.runId || null;
const v3ClinicId = body.clinicId || null;
if (v3RunId) {
try {
await supabase.from("analysis_runs").update({
report,
status: "complete",
pipeline_completed_at: new Date().toISOString(),
}).eq("id", v3RunId);
} catch (e) { console.error("V3 run update error:", e); }
}
if (v3ClinicId) {
try {
await supabase.from("clinics").update({
last_analyzed_at: new Date().toISOString(),
established_year: report.clinicInfo?.established ? parseInt(report.clinicInfo.established) || null : null,
updated_at: new Date().toISOString(),
}).eq("id", v3ClinicId);
} catch (e) { console.error("V3 clinic update error:", e); }
}
return new Response(
JSON.stringify({
success: true,