Replace calendar service with new .NET version

This commit is contained in:
2024-12-18 11:59:05 -05:00
parent 4dbedee94b
commit 40ebbf38cb
25 changed files with 767 additions and 3504 deletions

25
Calendar/Calendar.sln Normal file
View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Service", "Service\Service.csproj", "{AAF49637-5EAB-4D0F-A3EB-4421456DF709}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AAF49637-5EAB-4D0F-A3EB-4421456DF709}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAF49637-5EAB-4D0F-A3EB-4421456DF709}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAF49637-5EAB-4D0F-A3EB-4421456DF709}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAF49637-5EAB-4D0F-A3EB-4421456DF709}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF8B4FE8-1A8C-4189-8A16-BFD216D13047}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kaczor/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -1 +0,0 @@
PORT=8080

View File

@@ -1,36 +0,0 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

View File

@@ -1,2 +1,484 @@
dist/
node_modules/
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from `dotnet new gitignore`
# dotenv files
.env
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
.idea/
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# Vim temporary swap files
*.swp

View File

@@ -1,5 +0,0 @@
{
"tabWidth": 4,
"useTabs": false,
"singleQuote": true
}

View File

@@ -0,0 +1,21 @@
@Calendar_HostAddress = http://localhost:5060
GET {{Calendar_HostAddress}}/calendar/upcoming
Accept: application/json
###
GET {{Calendar_HostAddress}}/calendar/upcoming?days=7
Accept: application/json
###
GET {{Calendar_HostAddress}}/events/next
Accept: application/json
###
GET {{Calendar_HostAddress}}/events/next?timezone=America/New_York
Accept: application/json
###

View File

@@ -0,0 +1,27 @@
using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
using Microsoft.AspNetCore.Mvc;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
[Route("calendar")]
[ApiController]
public class CalendarController(IConfiguration configuration, HttpClient httpClient) : ControllerBase
{
[HttpGet("upcoming")]
public async Task<ActionResult<IEnumerable<CalendarEntry>>> GetUpcoming([FromQuery] int? days)
{
var data = await httpClient.GetStringAsync(configuration["Calendar:PersonalUrl"]);
var calendar = Ical.Net.Calendar.Load(data);
var start = DateTimeOffset.Now.Date;
var end = start.AddDays(days ?? 1);
var calendarEntries = calendar
.GetOccurrences(start, end)
.Select(o => new CalendarEntry(o))
.OrderBy(ce => ce.Start);
return Ok(calendarEntries);
}
}

View File

@@ -0,0 +1,35 @@
using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
using Microsoft.AspNetCore.Mvc;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
[Route("events")]
[ApiController]
public class HolidayController(IConfiguration configuration, HttpClient httpClient) : ControllerBase
{
[HttpGet("next")]
public async Task<ActionResult<IEnumerable<CalendarEntry>>> GetNext([FromQuery] string timezone = "Etc/UTC")
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
var data = await httpClient.GetStringAsync(configuration["Calendar:HolidayUrl"]);
var calendar = Ical.Net.Calendar.Load(data);
var start = DateTimeOffset.Now.Date;
var end = start.AddYears(1);
var calendarEntries = calendar
.GetOccurrences(start, end)
.Select(o => new CalendarEntry(o))
.OrderBy(ce => ce.Start);
var nextCalendarEntry = calendarEntries.First();
var holidayEntry = new HolidayEntry(nextCalendarEntry, timeZoneInfo);
var holidayResponse = new HolidayResponse(holidayEntry, timeZoneInfo);
return Ok(holidayResponse);
}
}

View File

@@ -1,15 +1,18 @@
FROM node:20-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
WORKDIR /app
EXPOSE 8080
RUN npm run build
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["./Service.csproj", "./"]
RUN dotnet restore "Service.csproj"
COPY . .
WORKDIR "/src"
RUN dotnet publish "Service.csproj" -c Release -o /app
CMD ["npm", "run", "dist"]
FROM base AS final
WORKDIR /app
COPY --from=build /app .
RUN apk add --no-cache tzdata icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENTRYPOINT ["dotnet", "ChrisKaczor.HomeMonitor.Calendar.Service.dll"]

View File

@@ -0,0 +1,13 @@
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models;
[PublicAPI]
public class CalendarEntry(Occurrence occurrence)
{
public string Summary { get; set; } = ((CalendarEvent)occurrence.Source).Summary;
public DateTimeOffset Start { get; set; } = occurrence.Period.StartTime.AsUtc;
public DateTimeOffset End { get; set; } = occurrence.Period.EndTime.AsUtc;
}

View File

@@ -0,0 +1,19 @@
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models;
public class Duration
{
public int Days { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public Duration(DateTimeOffset date)
{
var now = DateTimeOffset.Now;
var duration = date - now;
Days = duration.Days;
Hours = duration.Hours;
Minutes = duration.Minutes;
Seconds = duration.Seconds;
}
}

View File

@@ -0,0 +1,17 @@
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models;
public class HolidayEntry
{
public string Name { get; set; }
public DateTimeOffset Date { get; set; }
public bool IsToday { get; set; }
public Duration DurationUntil { get; set; }
public HolidayEntry(CalendarEntry calendarEntry, TimeZoneInfo timeZoneInfo)
{
Name = calendarEntry.Summary;
Date = TimeZoneInfo.ConvertTime(calendarEntry.Start, timeZoneInfo).Subtract(timeZoneInfo.GetUtcOffset(calendarEntry.Start));
IsToday = Date.Date == DateTimeOffset.UtcNow.Date;
DurationUntil = new Duration(Date);
}
}

View File

@@ -0,0 +1,10 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models;
[PublicAPI]
public class HolidayResponse(HolidayEntry holidayEntry, TimeZoneInfo timeZoneInfo)
{
public DateTimeOffset ResponseTime { get; set; } = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, timeZoneInfo);
public HolidayEntry? Event { get; set; } = holidayEntry;
}

View File

@@ -0,0 +1,34 @@
using ChrisKaczor.Common.OpenTelemetry;
using System.Reflection;
namespace ChrisKaczor.HomeMonitor.Calendar.Service;
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddCommonOpenTelemetry(Assembly.GetExecutingAssembly().GetName().Name, builder.Configuration["Telemetry:Endpoint"]);
builder.Services.AddSingleton<HttpClient>();
builder.Services.AddControllers();
// -- --
var app = builder.Build();
app.UseCors("CorsPolicy");
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -0,0 +1,11 @@
{
"profiles": {
"Calendar": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5060"
}
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>false</InvariantGlobalization>
<RootNamespace>ChrisKaczor.HomeMonitor.Calendar.Service</RootNamespace>
<AssemblyName>ChrisKaczor.HomeMonitor.Calendar.Service</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ChrisKaczor.Common.OpenTelemetry" Version="1.0.2" />
<PackageReference Include="Ical.Net" Version="4.3.1" />
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="System.Formats.Asn1" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,16 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Information"
}
},
"AllowedHosts": "*",
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
},
"Calendar": {
"PersonalUrl": "",
"HolidayUrl": ""
}
}

View File

@@ -24,6 +24,17 @@ spec:
imagePullPolicy: Always
securityContext:
privileged: true
env:
- name: Calendar__PersonalUrl
valueFrom:
secretKeyRef:
name: calendar-config
key: PERSONAL_URL
- name: Calendar__HolidayUrl
valueFrom:
secretKeyRef:
name: calendar-config
key: HOLIDAYS_URL
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst

File diff suppressed because it is too large Load Diff

View File

@@ -1,37 +0,0 @@
{
"name": "calendar",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --pretty --respawn ./src/app.ts",
"build": "tsc -p ./",
"dist": "node ./dist/app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"date-holidays": "^3.23.3",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"helmet": "^7.1.0",
"http-status-codes": "^2.3.0",
"luxon": "^3.4.4",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.6",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/luxon": "^3.3.8",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.3.3"
}
}

View File

@@ -1,27 +0,0 @@
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}`);
});

View File

@@ -1,41 +0,0 @@
import { HolidaysTypes } from 'date-holidays';
import { DateTime, Interval } from 'luxon';
export class Event {
name: string;
date: DateTime;
type: HolidaysTypes.HolidayType;
isToday: boolean;
durationUntil: {
days: number;
hours: number;
minutes: number;
seconds: number;
} | 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,
}).startOf('day');
this.type = holiday.type;
this.isToday = this.date.hasSame(now, 'day');
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

@@ -1,68 +0,0 @@
import express, { Request, Response } from 'express';
import { StatusCodes } from 'http-status-codes';
import * as DateHolidays from 'date-holidays';
import { Event } from './event';
import { DateTime } from 'luxon';
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('/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 now = DateTime.now().setZone(timezone);
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({ responseTime: now, event: nextEvent });
} 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 events = holidays.map((holiday) => new Event(holiday, timezone));
const now = DateTime.now().setZone(timezone);
const futureEvents = events.filter(
(event) => event.date > now || event.isToday
);
return res.status(StatusCodes.OK).json({ responseTime: now, events: futureEvents });
} catch (error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error });
}
});

View File

@@ -1,109 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "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. */
// "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. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}