mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Support the day of the event
This commit is contained in:
@@ -5,6 +5,7 @@ export class Event {
|
|||||||
name: string;
|
name: string;
|
||||||
date: DateTime;
|
date: DateTime;
|
||||||
type: HolidaysTypes.HolidayType;
|
type: HolidaysTypes.HolidayType;
|
||||||
|
isToday: boolean;
|
||||||
durationUntil: {
|
durationUntil: {
|
||||||
days: number;
|
days: number;
|
||||||
hours: number;
|
hours: number;
|
||||||
@@ -16,6 +17,7 @@ export class Event {
|
|||||||
this.name = holiday.name;
|
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 });
|
||||||
this.type = holiday.type;
|
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']);
|
const duration = Interval.fromDateTimes(DateTime.now(), this.date).toDuration(['days', 'hours', 'minutes', 'seconds']);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import express, { Request, Response } from 'express';
|
|||||||
import { StatusCodes } from 'http-status-codes';
|
import { StatusCodes } from 'http-status-codes';
|
||||||
import * as DateHolidays from 'date-holidays';
|
import * as DateHolidays from 'date-holidays';
|
||||||
import { Event } from './event';
|
import { Event } from './event';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
export const eventsRouter = express.Router();
|
export const eventsRouter = express.Router();
|
||||||
|
|
||||||
@@ -25,11 +26,12 @@ function getHolidays(req: Request): DateHolidays.HolidaysTypes.Holiday[] {
|
|||||||
eventsRouter.get('/all', async (req: Request, res: Response) => {
|
eventsRouter.get('/all', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const timezone = req.query.timezone as string;
|
const timezone = req.query.timezone as string;
|
||||||
|
|
||||||
const holidays = getHolidays(req);
|
const holidays = getHolidays(req);
|
||||||
|
|
||||||
const calendarEvents = holidays.map(holiday => new Event(holiday, timezone));
|
const events = holidays.map(holiday => new Event(holiday, timezone));
|
||||||
|
|
||||||
return res.status(StatusCodes.OK).json(calendarEvents);
|
return res.status(StatusCodes.OK).json(events);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
||||||
}
|
}
|
||||||
@@ -38,21 +40,20 @@ eventsRouter.get('/all', async (req: Request, res: Response) => {
|
|||||||
eventsRouter.get('/next', async (req: Request, res: Response) => {
|
eventsRouter.get('/next', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const timezone = req.query.timezone as string;
|
const timezone = req.query.timezone as string;
|
||||||
|
|
||||||
const holidays = getHolidays(req);
|
const holidays = getHolidays(req);
|
||||||
|
|
||||||
const nextHoliday = holidays.find(holiday => {
|
const events = holidays.map(holiday => new Event(holiday, timezone));
|
||||||
const holidayDate = new Date(holiday.date);
|
|
||||||
|
|
||||||
return holidayDate.getTime() > Date.now();
|
const now = DateTime.now();
|
||||||
});
|
|
||||||
|
|
||||||
if (nextHoliday == null) {
|
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(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendarEvent = new Event(nextHoliday, timezone);
|
return res.status(StatusCodes.OK).json(nextEvent);
|
||||||
|
|
||||||
return res.status(StatusCodes.OK).json(calendarEvent);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
||||||
}
|
}
|
||||||
@@ -61,17 +62,16 @@ eventsRouter.get('/next', async (req: Request, res: Response) => {
|
|||||||
eventsRouter.get('/future', async (req: Request, res: Response) => {
|
eventsRouter.get('/future', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const timezone = req.query.timezone as string;
|
const timezone = req.query.timezone as string;
|
||||||
|
|
||||||
const holidays = getHolidays(req);
|
const holidays = getHolidays(req);
|
||||||
|
|
||||||
const futureHolidays = holidays.filter(holiday => {
|
const events = holidays.map(holiday => new Event(holiday, timezone));
|
||||||
const holidayDate = new Date(holiday.date);
|
|
||||||
|
|
||||||
return holidayDate.getTime() > Date.now();
|
const now = DateTime.now();
|
||||||
});
|
|
||||||
|
|
||||||
const calendarEvents = futureHolidays.map(holiday => new Event(holiday, timezone));
|
const futureEvents = events.filter(event => event.date > now || event.isToday);
|
||||||
|
|
||||||
return res.status(StatusCodes.OK).json(calendarEvents);
|
return res.status(StatusCodes.OK).json(futureEvents);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user