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:
1 parent
e011ef7357
commit
7fe3ff82c9
5 files changed
+184
-8
No files matched your search
@@ -13,6 +13,8 @@ const APIFY_BASE = "https://api.apify.com/v2";
|
||||
|
||||
interface CollectRequest {
|
||||
reportId: string;
|
||||
clinicId?: string; // V3: clinic UUID
|
||||
runId?: string; // V3: analysis_run UUID
|
||||
}
|
||||
|
||||
async function runApifyActor(actorId: string, input: Record<string, unknown>, token: string): Promise<unknown[]> {
|
||||
@@ -40,7 +42,7 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const { reportId } = (await req.json()) as CollectRequest;
|
||||
const { reportId, clinicId: inputClinicId, runId: inputRunId } = (await req.json()) as CollectRequest;
|
||||
if (!reportId) throw new Error("reportId is required");
|
||||
|
||||
// Read Phase 1 results from DB
|
||||
@@ -390,7 +392,7 @@ Deno.serve(async (req) => {
|
||||
// ─── Execute all tasks ───
|
||||
await Promise.allSettled(tasks);
|
||||
|
||||
// ─── Save to DB ───
|
||||
// ─── Legacy: Save to marketing_reports ───
|
||||
await supabase.from("marketing_reports").update({
|
||||
channel_data: channelData,
|
||||
analysis_data: { clinicName, services, address, analysis: analysisData, analyzedAt: new Date().toISOString() },
|
||||
@@ -398,6 +400,98 @@ Deno.serve(async (req) => {
|
||||
updated_at: new Date().toISOString(),
|
||||
}).eq("id", reportId);
|
||||
|
||||
// ─── V3: channel_snapshots + screenshots + analysis_runs ───
|
||||
const clinicId = inputClinicId || null;
|
||||
const runId = inputRunId || null;
|
||||
|
||||
if (clinicId && runId) {
|
||||
try {
|
||||
// Channel snapshots — INSERT one row per channel (time-series!)
|
||||
const snapshotInserts: Record<string, unknown>[] = [];
|
||||
|
||||
const igData = channelData.instagram as Record<string, unknown> | undefined;
|
||||
if (igData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'instagram',
|
||||
handle: igData.username, followers: igData.followers, posts: igData.posts,
|
||||
details: igData,
|
||||
});
|
||||
}
|
||||
|
||||
const ytData = channelData.youtube as Record<string, unknown> | undefined;
|
||||
if (ytData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'youtube',
|
||||
handle: ytData.handle || ytData.channelName, followers: ytData.subscribers,
|
||||
posts: ytData.totalVideos, total_views: ytData.totalViews,
|
||||
details: ytData,
|
||||
});
|
||||
}
|
||||
|
||||
const fbData = channelData.facebook as Record<string, unknown> | undefined;
|
||||
if (fbData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'facebook',
|
||||
handle: fbData.pageName, followers: fbData.followers,
|
||||
details: fbData,
|
||||
});
|
||||
}
|
||||
|
||||
const guData = channelData.gangnamUnni as Record<string, unknown> | undefined;
|
||||
if (guData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'gangnamUnni',
|
||||
handle: guData.name, rating: guData.rating, rating_scale: 10,
|
||||
reviews: guData.totalReviews, details: guData,
|
||||
});
|
||||
}
|
||||
|
||||
const gmData = channelData.googleMaps as Record<string, unknown> | undefined;
|
||||
if (gmData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'googleMaps',
|
||||
handle: gmData.name, rating: gmData.rating, rating_scale: 5,
|
||||
reviews: gmData.reviewCount, details: gmData,
|
||||
});
|
||||
}
|
||||
|
||||
const nbData = channelData.naverBlog as Record<string, unknown> | undefined;
|
||||
if (nbData) {
|
||||
snapshotInserts.push({
|
||||
clinic_id: clinicId, run_id: runId, channel: 'naverBlog',
|
||||
handle: nbData.officialBlogHandle, details: nbData,
|
||||
});
|
||||
}
|
||||
|
||||
if (snapshotInserts.length > 0) {
|
||||
await supabase.from("channel_snapshots").insert(snapshotInserts);
|
||||
}
|
||||
|
||||
// Screenshots — INSERT evidence rows
|
||||
const screenshotList = (channelData.screenshots || []) as Record<string, unknown>[];
|
||||
if (screenshotList.length > 0) {
|
||||
await supabase.from("screenshots").insert(
|
||||
screenshotList.map(ss => ({
|
||||
clinic_id: clinicId, run_id: runId,
|
||||
channel: ss.channel, page_type: (ss.id as string || '').split('-')[1] || 'main',
|
||||
url: ss.url, source_url: ss.sourceUrl, caption: ss.caption,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
// Update analysis_run
|
||||
await supabase.from("analysis_runs").update({
|
||||
raw_channel_data: channelData,
|
||||
analysis_data: { clinicName, services, address, analysis: analysisData },
|
||||
vision_analysis: channelData.visionAnalysis || {},
|
||||
status: "collecting",
|
||||
}).eq("id", runId);
|
||||
|
||||
} catch (e) {
|
||||
console.error("V3 dual-write error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ success: true, channelData, analysisData, collectedAt: new Date().toISOString() }),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } },
|
||||
|
||||
Reference in new issue
Block a user