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

+4 -4
View File
@@ -108,13 +108,13 @@ export async function discoverChannels(url: string, clinicName?: string) {
* Phase 2: Collect all channel data using verified handles.
* Reads verified_channels from DB, runs parallel API calls.
*/
export async function collectChannelData(reportId: string) {
export async function collectChannelData(reportId: string, clinicId?: string, runId?: string) {
const response = await fetch(
`${supabaseUrl}/functions/v1/collect-channel-data`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ reportId }),
body: JSON.stringify({ reportId, clinicId, runId }),
}
);
@@ -128,13 +128,13 @@ export async function collectChannelData(reportId: string) {
/**
* Phase 3: Generate AI report using real collected data from DB.
*/
export async function generateReportV2(reportId: string) {
export async function generateReportV2(reportId: string, clinicId?: string, runId?: string) {
const response = await fetch(
`${supabaseUrl}/functions/v1/generate-report`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ reportId }),
body: JSON.stringify({ reportId, clinicId, runId }),
}
);
+4 -2
View File
@@ -39,15 +39,17 @@ export default function AnalysisLoadingPage() {
const discovery = await discoverChannels(url);
if (!discovery.success) throw new Error(discovery.error || 'Channel discovery failed');
const reportId = discovery.reportId;
const clinicId = discovery.clinicId; // V3
const runId = discovery.runId; // V3
// Phase 2: Collect Channel Data
setPhase('collecting');
const collection = await collectChannelData(reportId);
const collection = await collectChannelData(reportId, clinicId, runId);
if (!collection.success) throw new Error(collection.error || 'Data collection failed');
// Phase 3: Generate Report
setPhase('generating');
const result = await generateReportV2(reportId);
const result = await generateReportV2(reportId, clinicId, runId);
if (!result.success) throw new Error(result.error || 'Report generation failed');
// Complete — navigate to report