fix: arreglar feeds ICS; usar prepare(); auto-rotar; corregir toasts

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
webui
brobert 2 weeks ago
parent 4f984e5a2f
commit 2e523b1f0b

@ -53,9 +53,10 @@ export async function findActiveToken(
ORDER BY id DESC ORDER BY id DESC
LIMIT 1 LIMIT 1
`; `;
const stmt = db.prepare(sql);
const row = groupId const row = groupId
? (await db.query(sql).get(type, userId, groupId)) ? stmt.get(type, userId, groupId)
: (await db.query(sql).get(type, userId)); : stmt.get(type, userId);
return (row as any) || null; return (row as any) || null;
} }

@ -1,6 +1,6 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { getDb } from '$lib/server/db'; 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) => { export const GET: RequestHandler = async (event) => {
// Requiere sesión // Requiere sesión
@ -27,22 +27,32 @@ export const GET: RequestHandler = async (event) => {
// Personal // Personal
const personalExisting = await findActiveToken('personal', userId, null); const personalExisting = await findActiveToken('personal', userId, null);
const personal = const personal = await (async () => {
personalExisting if (personalExisting) {
? { url: personalExisting.token_plain ? buildCalendarIcsUrl('personal', personalExisting.token_plain) : null } if (personalExisting.token_plain) {
: await (async () => { 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); const created = await createCalendarTokenUrl('personal', userId, null);
return { url: created.url }; return { url: created.url };
}
})(); })();
// Aggregate (multigrupo) // Aggregate (multigrupo)
const aggregateExisting = await findActiveToken('aggregate', userId, null); const aggregateExisting = await findActiveToken('aggregate', userId, null);
const aggregate = const aggregate = await (async () => {
aggregateExisting if (aggregateExisting) {
? { url: aggregateExisting.token_plain ? buildCalendarIcsUrl('aggregate', aggregateExisting.token_plain) : null } if (aggregateExisting.token_plain) {
: await (async () => { 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); const created = await createCalendarTokenUrl('aggregate', userId, null);
return { url: created.url }; return { url: created.url };
}
})(); })();
// Por grupo (B): autogenerar si falta // Por grupo (B): autogenerar si falta
@ -50,7 +60,13 @@ export const GET: RequestHandler = async (event) => {
for (const g of groups) { for (const g of groups) {
const ex = await findActiveToken('group', userId, g.id); const ex = await findActiveToken('group', userId, g.id);
if (ex) { 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 }); groupFeeds.push({ groupId: g.id, groupName: g.name ?? null, url });
} else { } else {
const created = await createCalendarTokenUrl('group', userId, g.id); const created = await createCalendarTokenUrl('group', userId, g.id);

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import FeedCard from '$lib/ui/data/FeedCard.svelte'; import FeedCard from '$lib/ui/data/FeedCard.svelte';
import EmptyState from '$lib/ui/feedback/EmptyState.svelte'; import EmptyState from '$lib/ui/feedback/EmptyState.svelte';
import { toasts } from '$lib/stores/toasts'; import { success, error } from '$lib/stores/toasts';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
export let data: { export let data: {
@ -29,7 +29,7 @@
}); });
if (!res.ok) { if (!res.ok) {
const msg = await res.text(); const msg = await res.text();
toasts.error(`No se pudo rotar: ${msg || res.status}`); error(`No se pudo rotar: ${msg || res.status}`);
return; return;
} }
const body = await res.json(); const body = await res.json();
@ -41,9 +41,9 @@
const idx = groups.findIndex(g => g.groupId === groupId); const idx = groups.findIndex(g => g.groupId === groupId);
if (idx >= 0) groups[idx].url = body.url || null; if (idx >= 0) groups[idx].url = body.url || null;
} }
toasts.success('Token rotado correctamente'); success('Token rotado correctamente');
} catch (e) { } catch (e) {
toasts.error('Error al rotar el token'); error('Error al rotar el token');
} finally { } finally {
if (type === 'group' && groupId) rotating[groupId] = false; if (type === 'group' && groupId) rotating[groupId] = false;
if (type === 'personal') rotating['personal'] = false; if (type === 'personal') rotating['personal'] = false;

Loading…
Cancel
Save