mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-13 17:22:54 -05:00
Calendar updates
This commit is contained in:
24
Calendar/.vscode/launch.json
vendored
Normal file
24
Calendar/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "${workspaceFolder}/Service/src/app.ts",
|
||||
"env": {
|
||||
"PORT": "8080"
|
||||
},
|
||||
"preLaunchTask": "npm: build - Service",
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/**/*.js"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
14
Calendar/.vscode/tasks.json
vendored
Normal file
14
Calendar/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "build",
|
||||
"path": "Service",
|
||||
"group": "build",
|
||||
"problemMatcher": [],
|
||||
"label": "npm: build - Service",
|
||||
"detail": "tsc -p ./"
|
||||
}
|
||||
]
|
||||
}
|
||||
5
Calendar/Service/.prettierrc
Normal file
5
Calendar/Service/.prettierrc
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"tabWidth": 4,
|
||||
"useTabs": false,
|
||||
"singleQuote": true
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./dist/", /* Specify an output folder for all emitted files. */
|
||||
|
||||
Reference in New Issue
Block a user