daysbooked in timezone Europe/Berlin

This commit is contained in:
Thomas Ruoff
2020-11-09 22:51:19 +01:00
parent e9a8dfaf32
commit cd6ae123bb
4 changed files with 15 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays } from '../helpers/date'
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
import { Bill } from './bill'
import { Booker } from './booker'
import { BOOKING_STATUS } from './enums'
@@ -89,12 +89,13 @@ BookingSchema.pre('save', function (next) {
BookingSchema.static('findBookedDays', async function (): Promise<string[]> {
const model = this as BookingModel
const now = nowInTz()
const bookedDays = await model
.find(
{
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
// dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway
endDate: { $gt: dateFormatBackend(new Date()) },
endDate: { $gt: dateFormatBackend(now) },
},
'days'
)

View File

@@ -1,4 +1,5 @@
import { parse, format, addDays } from 'date-fns'
import { utcToZonedTime } from 'date-fns-tz'
const FRONTEND_FORMAT = 'dd.MM.yyyy'
const BACKEND_FORMAT = 'yyyy-MM-dd'
@@ -66,3 +67,8 @@ function dateParse(input: string, formatString: string) {
export function dateParseFrontend(input: string) {
return dateParse(input, FRONTEND_FORMAT)
}
export function nowInTz(timezone = 'Europe/Berlin') {
const now = new Date()
return utcToZonedTime(now, timezone)
}

5
package-lock.json generated
View File

@@ -4478,6 +4478,11 @@
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.16.1.tgz",
"integrity": "sha512-sAJVKx/FqrLYHAQeN7VpJrPhagZc9R4ImZIWYRFZaaohR3KzmuK88touwsSwSVT8Qcbd4zoDsnGfX4GFB4imyQ=="
},
"date-fns-tz": {
"version": "1.0.12",
"resolved": "https://registry.npmjs.org/date-fns-tz/-/date-fns-tz-1.0.12.tgz",
"integrity": "sha512-Ca+9pjGkU90XDHnclfSjz9o7g/ZqyYyYI0aCYmbf65P75oy8gktuaRslO3UPXl3ADgAnF9/KCykQkpU3/xvtWQ=="
},
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",

View File

@@ -11,6 +11,7 @@
},
"dependencies": {
"date-fns": "^2.16.1",
"date-fns-tz": "^1.0.12",
"ics": "^2.26.1",
"mongoose": "^5.10.13",
"next": "^10.0.1",