Refactor upcoming event filtering: Update buildUpcomingWhere function to compare dates at the date-only level, addressing timezone issues. This change enhances the accuracy of event retrieval and improves code clarity.
All checks were successful
Deploy miriamgemeinde / deploy (push) Successful in 6s
All checks were successful
Deploy miriamgemeinde / deploy (push) Successful in 6s
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
const { Event, Institution, EventPlace, ContactPerson, EventType, EventContactPerson, sequelize } = require('../models');
|
const { Event, Institution, EventPlace, ContactPerson, EventType, EventContactPerson, sequelize } = require('../models');
|
||||||
const { Op } = require('sequelize');
|
const { Op, fn, col, where: sequelizeWhere } = require('sequelize');
|
||||||
const { startOfDay } = require('date-fns');
|
const { format, startOfDay } = require('date-fns');
|
||||||
|
|
||||||
function buildUpcomingWhere() {
|
function buildUpcomingWhere() {
|
||||||
const today = startOfDay(new Date());
|
// Compare on date-only level to avoid timezone/DATETIME offset issues.
|
||||||
|
const todayDate = format(startOfDay(new Date()), 'yyyy-MM-dd');
|
||||||
return {
|
return {
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
// Fixed-date events: only today or future.
|
// Fixed-date events: only today or future.
|
||||||
{ date: { [Op.gte]: today } },
|
sequelizeWhere(fn('DATE', col('date')), { [Op.gte]: todayDate }),
|
||||||
// Recurring/undated events: date is null (dayOfWeek may be set).
|
// Recurring/undated events: date is null (dayOfWeek may be set).
|
||||||
{ date: { [Op.eq]: null } },
|
{ date: { [Op.eq]: null } },
|
||||||
],
|
],
|
||||||
@@ -72,7 +73,7 @@ const filterEvents = async (req, res) => {
|
|||||||
const events = await Event.findAll({
|
const events = await Event.findAll({
|
||||||
where: {
|
where: {
|
||||||
alsoOnHomepage: 1,
|
alsoOnHomepage: 1,
|
||||||
date: { [Op.gte]: startOfDay(new Date()) }
|
...buildUpcomingWhere(),
|
||||||
},
|
},
|
||||||
include: [
|
include: [
|
||||||
{ model: Institution, as: 'institution' },
|
{ model: Institution, as: 'institution' },
|
||||||
|
|||||||
Reference in New Issue
Block a user