mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(db): find no nearest day for a date the calendar does not have
The nearest-day fallback of a booking's time cast the bound date to a timestamp on Postgres, which refuses 2024-02-30 with a 500, while SQLite's JULIANDAY rolled it over into March and placed the booking there. dayDistance now refuses anything but a calendar date on both engines (isCalendarDate), so a caller that forgets the check fails on SQLite too, and findNearestDayId answers no day for such a date. The size baseline of Reservations.repository.ts drops by one line.
This commit is contained in:
@@ -538,6 +538,20 @@ export function castInteger(platform: Platform, ref: string): RawQueryFragment &
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `text` is a date as `YYYY-MM-DD` that the calendar has: `2024-02-29`
|
||||
* is one, `2024-02-30` and `2023-02-29` are not. A bound value either engine
|
||||
* reads as a date goes through this first, since SQLite rolls an impossible
|
||||
* day over into the next month while Postgres refuses it.
|
||||
*/
|
||||
export function isCalendarDate(text: string): boolean {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
||||
if (!match) return false;
|
||||
const [, year, month, day] = match.map(Number);
|
||||
const date = new Date(Date.UTC(year, month - 1, day));
|
||||
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
||||
}
|
||||
|
||||
/**
|
||||
* `ABS(JULIANDAY(<col>) - JULIANDAY(?))` — the legacy shape at
|
||||
* `reservations.service.ts:307` (RS11, `resolveDayIdFromTime`'s
|
||||
@@ -557,8 +571,16 @@ export function castInteger(platform: Platform, ref: string): RawQueryFragment &
|
||||
* comment documents; ordering by the EXPRESSION itself is what works).
|
||||
* Postgres measures the same distance in days from the epoch difference of
|
||||
* the two values cast to `timestamp`.
|
||||
*
|
||||
* `isoDate` must be a real calendar date ({@link isCalendarDate}): Postgres
|
||||
* refuses to cast `2024-02-30` while SQLite's JULIANDAY rolls it over into
|
||||
* March, so the two engines would answer it differently. Any other value is
|
||||
* refused here, on both engines alike, and the caller checks first.
|
||||
*/
|
||||
export function dayDistance(platform: Platform, ref: string, isoDate: string): RawQueryFragment & symbol {
|
||||
if (!isCalendarDate(isoDate)) {
|
||||
throw new Error(`sql-functions: dayDistance needs a calendar date (YYYY-MM-DD), got ${JSON.stringify(isoDate)}`);
|
||||
}
|
||||
if (isSqlite(platform)) return raw(`ABS(JULIANDAY(${column(ref)}) - JULIANDAY(?))`, [isoDate]);
|
||||
if (isPostgres(platform)) {
|
||||
return raw(`ABS(EXTRACT(EPOCH FROM (CAST(${column(ref)} AS timestamp) - CAST(? AS timestamp))) / 86400)`, [
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Reservations } from '../entities/Reservations.entity';
|
||||
import { DayAssignments } from '../entities/DayAssignments.entity';
|
||||
import { Days } from '../entities/Days.entity';
|
||||
import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functions';
|
||||
import { coalesceOverride, columnRef, dayDistance, isCalendarDate } from '../dialect/sql-functions';
|
||||
import { concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
|
||||
import { accommodationIdMatches, reservationStayIs } from './_shared/reservation-accommodation';
|
||||
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
|
||||
@@ -640,12 +640,11 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
/**
|
||||
* RS11 (`resolveDayIdFromTime`'s nearest-day fallback) — `SELECT id FROM
|
||||
* days WHERE trip_id = ? ORDER BY ABS(JULIANDAY(date) - JULIANDAY(?)) ASC,
|
||||
* date ASC LIMIT 1`. `dayDistance`, through `DaysRepository`'s own QB (the
|
||||
* same cross-repository-read shape as `getAssignmentTripId` above —
|
||||
* `DaysRepository` stays Task 1's additive-method territory; no new
|
||||
* method is added there).
|
||||
* date ASC LIMIT 1`, through `DaysRepository`'s own QB. A date the calendar does
|
||||
* not have (`2024-02-30`) finds no day on either engine (`isCalendarDate`).
|
||||
*/
|
||||
async findNearestDayId(trip_id: number, iso_date: string): Promise<number | undefined> {
|
||||
if (!isCalendarDate(iso_date)) return undefined;
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
const days = this.getEntityManager().getRepository(Days);
|
||||
const row = await days
|
||||
|
||||
Reference in New Issue
Block a user