mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
refactor(db): JSON columns are decoded through one declared schema per column
decodeJson/decodeJsonResult/encodeJson read and write a JSON-in-TEXT column against the schema and fallback declared for it in db/json-columns.ts, and log a row that does not decode. reservations.metadata, the OAuth scope and redirect lists, consents, API token scopes, MFA backup codes, journey entry tags and pros/cons and poll options go through it. Readers that used to throw on a broken row (a hotel update, the ICS export, the price mirror, the OAuth token and session reads, a consent, a journey entry, a poll) now take the tolerant fallback the other readers of the same column already used.
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { RESERVATION_METADATA } from '../../db/json-columns';
|
||||
import { decodeJsonResult, logJsonFailure } from '../../utils/json-column';
|
||||
|
||||
/**
|
||||
* Metadata keys on a reservation that the booking forms do not own.
|
||||
*
|
||||
@@ -14,15 +17,10 @@
|
||||
|
||||
/** Read a stored metadata column into an object, or null if it is not one. */
|
||||
function parseStored(stored: string | null | undefined): Record<string, unknown> | null {
|
||||
if (!stored) return null;
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(stored);
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as Record<string, unknown>)
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const decoded = decodeJsonResult(RESERVATION_METADATA, stored);
|
||||
if (decoded.ok) return decoded.value;
|
||||
if (decoded.reason !== 'empty') logJsonFailure(RESERVATION_METADATA, decoded.reason);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,6 +15,8 @@ import { findByIata } from '../airports/airports.data';
|
||||
import type { EndpointInput } from './reservations.service';
|
||||
import { AssignmentsService } from '../assignments/assignments.service';
|
||||
import { transportLegInputSchema, reservationUrlSchema, type TransportLegInput } from '@trek/shared';
|
||||
import { RESERVATION_METADATA } from '../../db/json-columns';
|
||||
import { decodeJson } from '../../utils/json-column';
|
||||
|
||||
// What counts as a transport booking, for the update_transport gate. Every value
|
||||
// ReservationsPanel renders with a transport icon, so a stored `transit` row is
|
||||
@@ -138,14 +140,7 @@ type MetaRecord = Record<string, unknown>;
|
||||
* still has to accept a legs update.
|
||||
*/
|
||||
function parseStoredMetadata(raw: unknown): MetaRecord {
|
||||
if (typeof raw !== 'string' || !raw) return {};
|
||||
try {
|
||||
let parsed: unknown = JSON.parse(raw);
|
||||
if (typeof parsed === 'string') parsed = JSON.parse(parsed);
|
||||
return typeof parsed === 'object' && parsed !== null ? parsed as MetaRecord : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
return typeof raw === 'string' ? decodeJson(RESERVATION_METADATA, raw) : {};
|
||||
}
|
||||
|
||||
interface LegPlan {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import type { TripAccess } from '../../db/repositories/Trips.repository';
|
||||
import type { TripAccess, TripsRepository } from '../../db/repositories/Trips.repository';
|
||||
import { UnitOfWork } from '../database/unit-of-work';
|
||||
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
|
||||
import { RealtimeService } from '../realtime/realtime.service';
|
||||
@@ -36,8 +36,9 @@ import type { UsersRepository } from '../../db/repositories/Users.repository';
|
||||
import { Trips } from '../../db/entities/Trips.entity';
|
||||
import { BudgetItems } from '../../db/entities/BudgetItems.entity';
|
||||
import type { BudgetItemsRepository } from '../../db/repositories/BudgetItems.repository';
|
||||
import type { TripsRepository } from '../../db/repositories/Trips.repository';
|
||||
import { appClock } from '../common/timezoneService';
|
||||
import { RESERVATION_METADATA } from '../../db/json-columns';
|
||||
import { decodeJson } from '../../utils/json-column';
|
||||
|
||||
type Trip = TripAccess;
|
||||
type BudgetEntry = { total_price?: number; category?: string; currency?: string | null; exchange_rate?: number } | undefined;
|
||||
@@ -787,7 +788,7 @@ export class ReservationsService {
|
||||
// whose accommodation was just auto-created above never received its
|
||||
// metadata check-in/out times or confirmation.
|
||||
if (resolvedAccommodationId && metadata) {
|
||||
const meta = (typeof metadata === 'string' ? JSON.parse(metadata) : metadata) as AccommodationTimesMeta;
|
||||
const meta = decodeJson(RESERVATION_METADATA, metadata) as AccommodationTimesMeta;
|
||||
if (meta.check_in_time || meta.check_in_end_time || meta.check_out_time) {
|
||||
await this.dayAccommodationsRepo.patchTimes(
|
||||
resolvedAccommodationId, meta.check_in_time || null, meta.check_in_end_time || null, meta.check_out_time || null,
|
||||
@@ -1036,9 +1037,9 @@ export class ReservationsService {
|
||||
}
|
||||
|
||||
// Sync check-in/out to accommodation if linked
|
||||
const resolvedMeta = nextMetadata !== undefined ? nextMetadata : (current.metadata ? JSON.parse(current.metadata as string) : null);
|
||||
const resolvedMeta = nextMetadata !== undefined ? nextMetadata : (current.metadata ? decodeJson(RESERVATION_METADATA, current.metadata, `reservation ${idNum}`) : null);
|
||||
if (resolvedAccId && resolvedMeta) {
|
||||
const meta = (typeof resolvedMeta === 'string' ? JSON.parse(resolvedMeta) : resolvedMeta) as AccommodationTimesMeta;
|
||||
const meta = decodeJson(RESERVATION_METADATA, resolvedMeta) as AccommodationTimesMeta;
|
||||
const resolvedAccIdNum = accIdForRead(resolvedAccId)!;
|
||||
if (meta.check_in_time || meta.check_in_end_time || meta.check_out_time) {
|
||||
await this.dayAccommodationsRepo.patchTimes(
|
||||
|
||||
Reference in New Issue
Block a user