diff --git a/Calendar/.vscode/launch.json b/Calendar/.vscode/launch.json new file mode 100644 index 0000000..6f09abe --- /dev/null +++ b/Calendar/.vscode/launch.json @@ -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": [ + "/**" + ], + "program": "${workspaceFolder}/Service/src/app.ts", + "env": { + "PORT": "8080" + }, + "preLaunchTask": "npm: build - Service", + "outFiles": [ + "${workspaceFolder}/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/Calendar/.vscode/tasks.json b/Calendar/.vscode/tasks.json new file mode 100644 index 0000000..f00cd7f --- /dev/null +++ b/Calendar/.vscode/tasks.json @@ -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 ./" + } + ] +} \ No newline at end of file diff --git a/Calendar/Service/.prettierrc b/Calendar/Service/.prettierrc new file mode 100644 index 0000000..97313f1 --- /dev/null +++ b/Calendar/Service/.prettierrc @@ -0,0 +1,5 @@ +{ + "tabWidth": 4, + "useTabs": false, + "singleQuote": true +} diff --git a/Calendar/Service/src/events/event.ts b/Calendar/Service/src/events/event.ts index 8092d43..4eb04bc 100644 --- a/Calendar/Service/src/events/event.ts +++ b/Calendar/Service/src/events/event.ts @@ -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), + }; } -} \ No newline at end of file +} diff --git a/Calendar/Service/src/events/events.routes.ts b/Calendar/Service/src/events/events.routes.ts index 5605493..5482609 100644 --- a/Calendar/Service/src/events/events.routes.ts +++ b/Calendar/Service/src/events/events.routes.ts @@ -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 }); } -}); \ No newline at end of file +}); diff --git a/Calendar/Service/tsconfig.json b/Calendar/Service/tsconfig.json index 7d3d903..6fe93d8 100644 --- a/Calendar/Service/tsconfig.json +++ b/Calendar/Service/tsconfig.json @@ -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. */