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.
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:
@@ -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']
|
||||
|
||||
Reference in New Issue
Block a user