From 9bc9271f1148a14c097e6d770b416cdbec012ba9 Mon Sep 17 00:00:00 2001 From: xCyanGrizzly Date: Mon, 23 Mar 2026 23:29:52 +0100 Subject: [PATCH] fix: auto-create READER links when enabling a source channel toggleChannelActive only flipped isActive but never created the AccountChannelMap READER link needed by the worker. Channels enabled via the toggle (rather than the channel picker) were invisible to the scanner. Now auto-creates READER links for all active authenticated accounts when a SOURCE channel is enabled. Also ran a one-time DB fix to backfill READER links for the 14 active channels that were missing them. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/app/(app)/telegram/actions.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/app/(app)/telegram/actions.ts b/src/app/(app)/telegram/actions.ts index 4dd8042..6d100e5 100644 --- a/src/app/(app)/telegram/actions.ts +++ b/src/app/(app)/telegram/actions.ts @@ -231,11 +231,35 @@ export async function toggleChannelActive(id: string): Promise { const existing = await prisma.telegramChannel.findUnique({ where: { id } }); if (!existing) return { success: false, error: "Channel not found" }; + const newActive = !existing.isActive; + try { await prisma.telegramChannel.update({ where: { id }, - data: { isActive: !existing.isActive }, + data: { isActive: newActive }, }); + + // When enabling a SOURCE channel, auto-create READER links for all + // active authenticated accounts so the worker can scan it. + // Without this, toggling a channel active without going through the + // channel picker leaves it with no AccountChannelMap READER link. + if (newActive && existing.type === "SOURCE") { + const accounts = await prisma.telegramAccount.findMany({ + where: { isActive: true, authState: "AUTHENTICATED" }, + select: { id: true }, + }); + + for (const account of accounts) { + try { + await prisma.accountChannelMap.create({ + data: { accountId: account.id, channelId: id, role: "READER" }, + }); + } catch { + // Already linked — ignore unique constraint violation + } + } + } + revalidatePath(REVALIDATE_PATH); return { success: true, data: undefined }; } catch {