Calendar updates

This commit is contained in:
2024-02-16 14:01:18 -05:00
parent 122d417026
commit be77845c84
6 changed files with 79 additions and 36 deletions

View File

@@ -14,18 +14,28 @@ export class Event {
} | null;
constructor(holiday: HolidaysTypes.Holiday, timezone: string) {
const now = DateTime.now().setZone(timezone);
this.name = holiday.name;
this.date = DateTime.fromFormat(holiday.date, 'yyyy-MM-dd HH:mm:ss', { zone: timezone });
this.date = DateTime.fromFormat(holiday.date, 'yyyy-MM-dd HH:mm:ss', {
zone: timezone,
}).startOf('day');
this.type = holiday.type;
this.isToday = this.date.hasSame(DateTime.now(), 'day');
const duration = Interval.fromDateTimes(DateTime.now(), this.date).toDuration(['days', 'hours', 'minutes', 'seconds']);
this.isToday = this.date.hasSame(now, 'day');
this.durationUntil = !duration.isValid ? null : {
days: duration.days,
hours: duration.hours,
minutes: duration.minutes,
seconds: Math.round(duration.seconds)
};
const duration = Interval.fromDateTimes(now, this.date).toDuration([
'days',
'hours',
'minutes',
'seconds',
]);
this.durationUntil = !duration.isValid
? null
: {
days: duration.days,
hours: duration.hours,
minutes: duration.minutes,
seconds: Math.round(duration.seconds),
};
}
}
}

View File

@@ -15,7 +15,7 @@ function getHolidays(req: Request): DateHolidays.HolidaysTypes.Holiday[] {
const dateHolidays = new DateHolidays.default();
dateHolidays.init(country, state, {
timezone: timezone
timezone: timezone,
});
const holidays = dateHolidays.getHolidays(year);
@@ -23,37 +23,25 @@ function getHolidays(req: Request): DateHolidays.HolidaysTypes.Holiday[] {
return holidays;
}
eventsRouter.get('/all', async (req: Request, res: Response) => {
try {
const timezone = req.query.timezone as string;
const holidays = getHolidays(req);
const events = holidays.map(holiday => new Event(holiday, timezone));
return res.status(StatusCodes.OK).json(events);
} catch (error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
}
});
eventsRouter.get('/next', async (req: Request, res: Response) => {
try {
const timezone = req.query.timezone as string;
const holidays = getHolidays(req);
const events = holidays.map(holiday => new Event(holiday, timezone));
const events = holidays.map((holiday) => new Event(holiday, timezone));
const now = DateTime.now();
const now = DateTime.now().setZone(timezone);
const nextEvent = events.find(event => event.date > now || event.isToday);
const nextEvent = events.find(
(event) => event.date > now || event.isToday
);
if (!nextEvent) {
return res.status(StatusCodes.OK).json(null);
}
return res.status(StatusCodes.OK).json(nextEvent);
return res.status(StatusCodes.OK).json({ responseTime: now, event: nextEvent });
} catch (error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
}
@@ -65,14 +53,16 @@ eventsRouter.get('/future', async (req: Request, res: Response) => {
const holidays = getHolidays(req);
const events = holidays.map(holiday => new Event(holiday, timezone));
const events = holidays.map((holiday) => new Event(holiday, timezone));
const now = DateTime.now();
const now = DateTime.now().setZone(timezone);
const futureEvents = events.filter(event => event.date > now || event.isToday);
const futureEvents = events.filter(
(event) => event.date > now || event.isToday
);
return res.status(StatusCodes.OK).json(futureEvents);
return res.status(StatusCodes.OK).json({ responseTime: now, events: futureEvents });
} catch (error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
}
});
});