From 2e523b1f0bc60c7eb5cd37f4520a44d9b6fe0422 Mon Sep 17 00:00:00 2001 From: brobert Date: Sat, 18 Oct 2025 22:02:12 +0200 Subject: [PATCH] fix: arreglar feeds ICS; usar prepare(); auto-rotar; corregir toasts Co-authored-by: aider (openrouter/openai/gpt-5) --- apps/web/src/lib/server/calendar-tokens.ts | 5 +- .../routes/api/integrations/feeds/+server.ts | 48 ++++++++++++------- .../src/routes/app/integrations/+page.svelte | 8 ++-- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/apps/web/src/lib/server/calendar-tokens.ts b/apps/web/src/lib/server/calendar-tokens.ts index f6c272a..6a7282b 100644 --- a/apps/web/src/lib/server/calendar-tokens.ts +++ b/apps/web/src/lib/server/calendar-tokens.ts @@ -53,9 +53,10 @@ export async function findActiveToken( ORDER BY id DESC LIMIT 1 `; + const stmt = db.prepare(sql); const row = groupId - ? (await db.query(sql).get(type, userId, groupId)) - : (await db.query(sql).get(type, userId)); + ? stmt.get(type, userId, groupId) + : stmt.get(type, userId); return (row as any) || null; } diff --git a/apps/web/src/routes/api/integrations/feeds/+server.ts b/apps/web/src/routes/api/integrations/feeds/+server.ts index 5217b36..2a3f9c5 100644 --- a/apps/web/src/routes/api/integrations/feeds/+server.ts +++ b/apps/web/src/routes/api/integrations/feeds/+server.ts @@ -1,6 +1,6 @@ import type { RequestHandler } from './$types'; import { getDb } from '$lib/server/db'; -import { findActiveToken, createCalendarTokenUrl, buildCalendarIcsUrl } from '$lib/server/calendar-tokens'; +import { findActiveToken, createCalendarTokenUrl, buildCalendarIcsUrl, rotateCalendarTokenUrl } from '$lib/server/calendar-tokens'; export const GET: RequestHandler = async (event) => { // Requiere sesión @@ -27,30 +27,46 @@ export const GET: RequestHandler = async (event) => { // Personal const personalExisting = await findActiveToken('personal', userId, null); - const personal = - personalExisting - ? { url: personalExisting.token_plain ? buildCalendarIcsUrl('personal', personalExisting.token_plain) : null } - : await (async () => { - const created = await createCalendarTokenUrl('personal', userId, null); - return { url: created.url }; - })(); + const personal = await (async () => { + if (personalExisting) { + if (personalExisting.token_plain) { + return { url: buildCalendarIcsUrl('personal', personalExisting.token_plain) }; + } + const rotated = await rotateCalendarTokenUrl('personal', userId, null); + return { url: rotated.url }; + } else { + const created = await createCalendarTokenUrl('personal', userId, null); + return { url: created.url }; + } + })(); // Aggregate (multigrupo) const aggregateExisting = await findActiveToken('aggregate', userId, null); - const aggregate = - aggregateExisting - ? { url: aggregateExisting.token_plain ? buildCalendarIcsUrl('aggregate', aggregateExisting.token_plain) : null } - : await (async () => { - const created = await createCalendarTokenUrl('aggregate', userId, null); - return { url: created.url }; - })(); + const aggregate = await (async () => { + if (aggregateExisting) { + if (aggregateExisting.token_plain) { + return { url: buildCalendarIcsUrl('aggregate', aggregateExisting.token_plain) }; + } + const rotated = await rotateCalendarTokenUrl('aggregate', userId, null); + return { url: rotated.url }; + } else { + const created = await createCalendarTokenUrl('aggregate', userId, null); + return { url: created.url }; + } + })(); // Por grupo (B): autogenerar si falta const groupFeeds: Array<{ groupId: string; groupName: string | null; url: string | null }> = []; for (const g of groups) { const ex = await findActiveToken('group', userId, g.id); if (ex) { - const url = ex.token_plain ? buildCalendarIcsUrl('group', ex.token_plain) : null; + let url: string | null; + if (ex.token_plain) { + url = buildCalendarIcsUrl('group', ex.token_plain); + } else { + const rotated = await rotateCalendarTokenUrl('group', userId, g.id); + url = rotated.url; + } groupFeeds.push({ groupId: g.id, groupName: g.name ?? null, url }); } else { const created = await createCalendarTokenUrl('group', userId, g.id); diff --git a/apps/web/src/routes/app/integrations/+page.svelte b/apps/web/src/routes/app/integrations/+page.svelte index c9d26c8..489edd3 100644 --- a/apps/web/src/routes/app/integrations/+page.svelte +++ b/apps/web/src/routes/app/integrations/+page.svelte @@ -1,7 +1,7 @@