Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/reconcile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { SupabaseClient } from "@supabase/supabase-js";
import type { Database } from "./supabase";

type SupabaseDB = SupabaseClient<Database>;

/**
* Reconcile a storyline's plot_count and last_plot_time from the plots table.
* Uses COUNT(*) and MAX(block_timestamp) — idempotent and safe for replays.
*
* Throws on any Supabase error so callers can handle failures.
*/
export async function reconcileStorylinePlotCount(
supabase: SupabaseDB,
storylineId: number,
): Promise<void> {
const [countResult, latestResult] = await Promise.all([
supabase
.from("plots")
.select("*", { count: "exact", head: true })
.eq("storyline_id", storylineId),
supabase
.from("plots")
.select("block_timestamp")
.eq("storyline_id", storylineId)
.order("block_timestamp", { ascending: false })
.limit(1)
.single(),
]);

if (countResult.error) {
throw new Error(`Reconcile count error: ${countResult.error.message}`);
}
if (latestResult.error) {
throw new Error(`Reconcile latest plot error: ${latestResult.error.message}`);
}

if (countResult.count === null) return;

const { error: updateError } = await supabase
.from("storylines")
.update({
plot_count: countResult.count,
...(latestResult.data?.block_timestamp
? { last_plot_time: latestResult.data.block_timestamp }
: {}),
})
.eq("storyline_id", storylineId);

if (updateError) {
throw new Error(`Reconcile update error: ${updateError.message}`);
}
}
4 changes: 4 additions & 0 deletions src/app/api/cron/backfill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { storyFactoryAbi } from "../../../../../lib/contracts/abi";
import { STORY_FACTORY } from "../../../../../lib/contracts/constants";
import { hashContent } from "../../../../../lib/content";
import { detectWriterType } from "../../../../../lib/contracts/erc8004";
import { reconcileStorylinePlotCount } from "../../../../../lib/reconcile";
import type { Database } from "../../../../../lib/supabase";

const IPFS_GATEWAY = "https://ipfs.filebase.io/ipfs/";
Expand Down Expand Up @@ -285,6 +286,9 @@ async function processPlotChained(
if (plotError) {
throw new Error(`Database error (plot): ${plotError.message}`);
}

// Reconcile parent storyline plot_count and last_plot_time (idempotent)
await reconcileStorylinePlotCount(supabase, Number(storylineId));
}

async function processDonation(
Expand Down
10 changes: 10 additions & 0 deletions src/app/api/index/plot/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../../../../lib/contracts/abi";
import { STORY_FACTORY } from "../../../../../lib/contracts/constants";
import { hashContent } from "../../../../../lib/content";
import { reconcileStorylinePlotCount } from "../../../../../lib/reconcile";
import type { Database } from "../../../../../lib/supabase";

const IPFS_GATEWAY = "https://ipfs.filebase.io/ipfs/";
Expand Down Expand Up @@ -133,5 +134,14 @@ export async function POST(req: Request) {
return error(`Database error: ${dbError.message}`, 500);
}

// Reconcile parent storyline plot_count and last_plot_time (idempotent)
try {
await reconcileStorylinePlotCount(supabase, Number(storylineId));
} catch (err) {
const msg = err instanceof Error ? err.message : "Unknown reconciliation error";
console.error(`[index/plot] Reconciliation failed for storyline ${storylineId}: ${msg}`);
return error(`Reconciliation failed: ${msg}`, 500);
}

return NextResponse.json({ success: true });
}
Loading