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 { Op } = require('sequelize');
|
||||||
const { startOfDay } = require('date-fns');
|
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) => {
|
const getAllEvents = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const includePast = String(req.query?.includePast || '').toLowerCase();
|
const includePast = String(req.query?.includePast || '').toLowerCase();
|
||||||
@@ -9,20 +21,7 @@ const getAllEvents = async (req, res) => {
|
|||||||
|
|
||||||
const where = wantsPast
|
const where = wantsPast
|
||||||
? undefined
|
? undefined
|
||||||
: {
|
: buildUpcomingWhere();
|
||||||
[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({
|
const events = await Event.findAll({
|
||||||
where,
|
where,
|
||||||
@@ -48,19 +47,7 @@ const getAllEvents = async (req, res) => {
|
|||||||
const filterEvents = async (req, res) => {
|
const filterEvents = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const request = req.body;
|
const request = req.body;
|
||||||
const where = {
|
const where = buildUpcomingWhere();
|
||||||
[Op.or]: [
|
|
||||||
{
|
|
||||||
date: {
|
|
||||||
[Op.or]: [
|
|
||||||
{ [Op.gte]: startOfDay(new Date()) },
|
|
||||||
{ [Op.eq]: null }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ dayOfWeek: { [Op.gte]: 0 } }
|
|
||||||
]
|
|
||||||
};
|
|
||||||
const order = [
|
const order = [
|
||||||
['date', 'ASC'],
|
['date', 'ASC'],
|
||||||
['time', 'ASC']
|
['time', 'ASC']
|
||||||
|
|||||||
Reference in New Issue
Block a user