Refactor event retrieval logic: Implement server-side filtering for past events based on user preference, enhancing performance and simplifying client-side code. Update API call in EventManagement component to include past events parameter.
All checks were successful
Deploy miriamgemeinde / deploy (push) Successful in 7s

This commit is contained in:
Torsten Schulz (local)
2026-04-29 15:23:37 +02:00
parent f7e8b31fdb
commit d412331f8e
2 changed files with 33 additions and 22 deletions

View File

@@ -4,14 +4,39 @@ const { startOfDay } = require('date-fns');
const getAllEvents = async (req, res) => {
try {
const includePast = String(req.query?.includePast || '').toLowerCase();
const wantsPast = includePast === '1' || includePast === 'true' || includePast === 'yes';
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 } },
],
};
const events = await Event.findAll({
where,
include: [
{ model: Institution, as: 'institution' },
{ model: EventPlace, as: 'eventPlace' },
{ model: EventType, as: 'eventType' },
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
],
order: ['name', 'date', 'time']
order: [
['date', 'ASC'],
['time', 'ASC'],
['name', 'ASC'],
],
});
res.json(events);
} catch (error) {