diff --git a/controllers/eventController.js b/controllers/eventController.js index 7c696b2..78ba934 100644 --- a/controllers/eventController.js +++ b/controllers/eventController.js @@ -2,6 +2,18 @@ const { Event, Institution, EventPlace, ContactPerson, EventType, EventContactPe const { Op } = require('sequelize'); const { startOfDay } = require('date-fns'); +function buildUpcomingWhere() { + const today = startOfDay(new Date()); + return { + [Op.or]: [ + // Fixed-date events: only today or future. + { date: { [Op.gte]: today } }, + // Recurring/undated events: date is null (dayOfWeek may be set). + { date: { [Op.eq]: null } }, + ], + }; +} + const getAllEvents = async (req, res) => { try { const includePast = String(req.query?.includePast || '').toLowerCase(); @@ -9,20 +21,7 @@ const getAllEvents = async (req, res) => { const where = wantsPast ? undefined - : { - [Op.or]: [ - { - date: { - [Op.or]: [ - { [Op.gte]: startOfDay(new Date()) }, - { [Op.eq]: null }, - ], - }, - }, - // Recurring events without a fixed date - { dayOfWeek: { [Op.gte]: 0 } }, - ], - }; + : buildUpcomingWhere(); const events = await Event.findAll({ where, @@ -48,19 +47,7 @@ const getAllEvents = async (req, res) => { const filterEvents = async (req, res) => { try { const request = req.body; - const where = { - [Op.or]: [ - { - date: { - [Op.or]: [ - { [Op.gte]: startOfDay(new Date()) }, - { [Op.eq]: null } - ] - } - }, - { dayOfWeek: { [Op.gte]: 0 } } - ] - }; + const where = buildUpcomingWhere(); const order = [ ['date', 'ASC'], ['time', 'ASC']