mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-27 01:25:38 -05:00
Initial commit of calendar service
This commit is contained in:
27
Calendar/Service/src/app.ts
Normal file
27
Calendar/Service/src/app.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'dotenv/config';
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import { eventsRouter } from './events/events.routes';
|
||||
|
||||
if (!process.env.PORT) {
|
||||
console.log('No port value specified');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
process.env.TZ = 'Etc/UTC';
|
||||
|
||||
const PORT = parseInt(process.env.PORT as string, 10);
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(cors());
|
||||
app.use(helmet());
|
||||
|
||||
app.use('/events/', eventsRouter);
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is listening on port ${PORT}`);
|
||||
});
|
||||
29
Calendar/Service/src/events/event.ts
Normal file
29
Calendar/Service/src/events/event.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { HolidaysTypes } from 'date-holidays';
|
||||
import { DateTime, Interval } from 'luxon';
|
||||
|
||||
export class Event {
|
||||
name: string;
|
||||
date: DateTime;
|
||||
type: HolidaysTypes.HolidayType;
|
||||
durationUntil: {
|
||||
days: number;
|
||||
hours: number;
|
||||
minutes: number;
|
||||
seconds: number;
|
||||
} | null;
|
||||
|
||||
constructor(holiday: HolidaysTypes.Holiday, timezone: string) {
|
||||
this.name = holiday.name;
|
||||
this.date = DateTime.fromFormat(holiday.date, 'yyyy-MM-dd HH:mm:ss', { zone: timezone });
|
||||
this.type = holiday.type;
|
||||
|
||||
const duration = Interval.fromDateTimes(DateTime.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)
|
||||
};
|
||||
}
|
||||
}
|
||||
78
Calendar/Service/src/events/events.routes.ts
Normal file
78
Calendar/Service/src/events/events.routes.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { StatusCodes } from 'http-status-codes';
|
||||
import * as DateHolidays from 'date-holidays';
|
||||
import { Event } from './event';
|
||||
|
||||
export const eventsRouter = express.Router();
|
||||
|
||||
function getHolidays(req: Request): DateHolidays.HolidaysTypes.Holiday[] {
|
||||
const country = req.query.country as string;
|
||||
const state = req.query.state as string;
|
||||
const year = parseInt(req.query.year as string, 10);
|
||||
const timezone = 'Etc/UTC';
|
||||
|
||||
const dateHolidays = new DateHolidays.default();
|
||||
|
||||
dateHolidays.init(country, state, {
|
||||
timezone: timezone
|
||||
});
|
||||
|
||||
const holidays = dateHolidays.getHolidays(year);
|
||||
|
||||
return holidays;
|
||||
}
|
||||
|
||||
eventsRouter.get('/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const timezone = req.query.timezone as string;
|
||||
const holidays = getHolidays(req);
|
||||
|
||||
const calendarEvents = holidays.map(holiday => new Event(holiday, timezone));
|
||||
|
||||
return res.status(StatusCodes.OK).json(calendarEvents);
|
||||
} 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 nextHoliday = holidays.find(holiday => {
|
||||
const holidayDate = new Date(holiday.date);
|
||||
|
||||
return holidayDate.getTime() > Date.now();
|
||||
});
|
||||
|
||||
if (nextHoliday == null) {
|
||||
return res.status(StatusCodes.OK).json(null);
|
||||
}
|
||||
|
||||
const calendarEvent = new Event(nextHoliday, timezone);
|
||||
|
||||
return res.status(StatusCodes.OK).json(calendarEvent);
|
||||
} catch (error) {
|
||||
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
||||
}
|
||||
});
|
||||
|
||||
eventsRouter.get('/future', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const timezone = req.query.timezone as string;
|
||||
const holidays = getHolidays(req);
|
||||
|
||||
const futureHolidays = holidays.filter(holiday => {
|
||||
const holidayDate = new Date(holiday.date);
|
||||
|
||||
return holidayDate.getTime() > Date.now();
|
||||
});
|
||||
|
||||
const calendarEvents = futureHolidays.map(holiday => new Event(holiday, timezone));
|
||||
|
||||
return res.status(StatusCodes.OK).json(calendarEvents);
|
||||
} catch (error) {
|
||||
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user