From 59aae59b623ea8dbf79b87c78f7147a96389b9aa Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 29 Apr 2026 15:27:42 +0200 Subject: [PATCH] Refactor event retrieval logic: Extract upcoming event filtering into a separate function for improved readability and maintainability. This change simplifies the conditions for fetching events, ensuring clarity in the codebase. --- controllers/eventController.js | 41 ++++++++++++---------------------- 1 file changed, 14 insertions(+), 27 deletions(-) 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']