feat: add featured event with automatic fallback

- Add featured_event_id to site_settings (schema + migration)
- Backend: featured event logic in /events/next/upcoming with auto-unset when event ends
- Site settings: PUT supports featuredEventId, add PUT /featured-event for admin
- Admin events: Set as featured checkbox in editor, star toggle in list, featured badge
- Admin settings: Featured Event section with current event and remove/change links
- API: siteSettingsApi.setFeaturedEvent(), Event.isFeatured, SiteSettings.featuredEventId
- Homepage/linktree unchanged: still use getNextUpcoming (now returns featured or fallback)
This commit is contained in:
Michilis
2026-02-03 19:24:00 +00:00
parent 0fd8172e04
commit 0c142884c7
9 changed files with 421 additions and 78 deletions

View File

@@ -1,8 +1,8 @@
import { Hono } from 'hono';
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod';
import { db, dbGet, siteSettings } from '../db/index.js';
import { eq } from 'drizzle-orm';
import { db, dbGet, siteSettings, events } from '../db/index.js';
import { eq, and, gte } from 'drizzle-orm';
import { requireAuth } from '../lib/auth.js';
import { generateId, getNow, toDbBool } from '../lib/utils.js';
@@ -27,6 +27,7 @@ const updateSiteSettingsSchema = z.object({
instagramUrl: z.string().url().optional().nullable().or(z.literal('')),
twitterUrl: z.string().url().optional().nullable().or(z.literal('')),
linkedinUrl: z.string().url().optional().nullable().or(z.literal('')),
featuredEventId: z.string().optional().nullable(),
maintenanceMode: z.boolean().optional(),
maintenanceMessage: z.string().optional().nullable(),
maintenanceMessageEs: z.string().optional().nullable(),
@@ -52,6 +53,7 @@ siteSettingsRouter.get('/', async (c) => {
instagramUrl: null,
twitterUrl: null,
linkedinUrl: null,
featuredEventId: null,
maintenanceMode: false,
maintenanceMessage: null,
maintenanceMessageEs: null,
@@ -104,6 +106,17 @@ siteSettingsRouter.put('/', requireAuth(['admin']), zValidator('json', updateSit
if (!existing) {
// Create new settings record
const id = generateId();
// Validate featured event if provided
if (data.featuredEventId) {
const featuredEvent = await dbGet<any>(
(db as any).select().from(events).where(eq((events as any).id, data.featuredEventId))
);
if (!featuredEvent || featuredEvent.status !== 'published') {
return c.json({ error: 'Featured event must exist and be published' }, 400);
}
}
const newSettings = {
id,
timezone: data.timezone || 'America/Asuncion',
@@ -116,6 +129,7 @@ siteSettingsRouter.put('/', requireAuth(['admin']), zValidator('json', updateSit
instagramUrl: data.instagramUrl || null,
twitterUrl: data.twitterUrl || null,
linkedinUrl: data.linkedinUrl || null,
featuredEventId: data.featuredEventId || null,
maintenanceMode: toDbBool(data.maintenanceMode || false),
maintenanceMessage: data.maintenanceMessage || null,
maintenanceMessageEs: data.maintenanceMessageEs || null,
@@ -128,6 +142,16 @@ siteSettingsRouter.put('/', requireAuth(['admin']), zValidator('json', updateSit
return c.json({ settings: newSettings, message: 'Settings created successfully' }, 201);
}
// Validate featured event if provided
if (data.featuredEventId) {
const featuredEvent = await dbGet<any>(
(db as any).select().from(events).where(eq((events as any).id, data.featuredEventId))
);
if (!featuredEvent || featuredEvent.status !== 'published') {
return c.json({ error: 'Featured event must exist and be published' }, 400);
}
}
// Update existing settings
const updateData: Record<string, any> = {
...data,
@@ -151,4 +175,61 @@ siteSettingsRouter.put('/', requireAuth(['admin']), zValidator('json', updateSit
return c.json({ settings: updated, message: 'Settings updated successfully' });
});
// Set featured event (admin only) - convenience endpoint for event editor
siteSettingsRouter.put('/featured-event', requireAuth(['admin']), zValidator('json', z.object({
eventId: z.string().nullable(),
})), async (c) => {
const { eventId } = c.req.valid('json');
const user = c.get('user');
const now = getNow();
// Validate event if provided
if (eventId) {
const event = await dbGet<any>(
(db as any).select().from(events).where(eq((events as any).id, eventId))
);
if (!event) {
return c.json({ error: 'Event not found' }, 404);
}
if (event.status !== 'published') {
return c.json({ error: 'Event must be published to be featured' }, 400);
}
}
// Get or create settings
const existing = await dbGet<any>(
(db as any).select().from(siteSettings).limit(1)
);
if (!existing) {
// Create new settings record with featured event
const id = generateId();
const newSettings = {
id,
timezone: 'America/Asuncion',
siteName: 'Spanglish',
featuredEventId: eventId,
maintenanceMode: 0,
updatedAt: now,
updatedBy: user.id,
};
await (db as any).insert(siteSettings).values(newSettings);
return c.json({ featuredEventId: eventId, message: eventId ? 'Event set as featured' : 'Featured event removed' });
}
// Update existing settings
await (db as any)
.update(siteSettings)
.set({
featuredEventId: eventId,
updatedAt: now,
updatedBy: user.id,
})
.where(eq((siteSettings as any).id, existing.id));
return c.json({ featuredEventId: eventId, message: eventId ? 'Event set as featured' : 'Featured event removed' });
});
export default siteSettingsRouter;