From a7d53ffe21c17bf5f0691f91fcb1d29dcfd1e90e Mon Sep 17 00:00:00 2001 From: Kenzo Date: Thu, 18 Dec 2025 13:16:49 +0100 Subject: [PATCH] feat: Improve banner fetching logic and integrate Banner component - Adjusted server logic in `/banners/active` to resolve timezone issues and ensure consistent date handling. - Sorted active banners by creation date in descending order for better relevance. - Integrated `Banner.astro` component into the homepage layout for displaying active banners. --- backend/src/routes/banners.ts | 9 +++++++-- src/pages/index.astro | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/src/routes/banners.ts b/backend/src/routes/banners.ts index 7cc4c35..022a418 100644 --- a/backend/src/routes/banners.ts +++ b/backend/src/routes/banners.ts @@ -1,7 +1,7 @@ import { FastifyPluginAsync } from 'fastify'; import { db } from '../config/database.js'; import { banners } from '../db/schema.js'; -import { eq, and, lte, gte } from 'drizzle-orm'; +import { eq, and, lte, gte, desc } from 'drizzle-orm'; const bannerBodyJsonSchema = { type: 'object', @@ -18,7 +18,11 @@ const bannersRoute: FastifyPluginAsync = async (fastify) => { // Get active banner (public endpoint) fastify.get('/banners/active', async (request, reply) => { - const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD + // Use local date to avoid timezone issues + const now = new Date(); + const today = new Date(now.getTime() - (now.getTimezoneOffset() * 60000)) + .toISOString() + .split('T')[0]; // YYYY-MM-DD const [activeBanner] = await db .select() @@ -30,6 +34,7 @@ const bannersRoute: FastifyPluginAsync = async (fastify) => { gte(banners.endDate, today) ) ) + .orderBy(desc(banners.createdAt)) .limit(1); if (!activeBanner) { diff --git a/src/pages/index.astro b/src/pages/index.astro index 42cfd73..7dd7e8a 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,5 +1,6 @@ --- import Layout from "../components/Layout.astro"; +import Banner from "../components/Banner.astro"; import Hero from "../components/Hero.astro"; import Welcome from "../components/Welcome.astro"; import EventsGrid from "../components/EventsGrid.astro"; @@ -75,6 +76,7 @@ const images = [ --- +