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
All checks were successful
Deploy miriamgemeinde / deploy (push) Successful in 7s
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user