mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
fix(feeds): keep a staged booking out of the public feed and the shared trip
The ICS feed is public with a token that never expires and prints the confirmation number into the DESCRIPTION, and the shared-trip payload does a SELECT * over reservations with share_bookings on by default. Neither filters anything, so whatever lands on a trip is published the moment it is written. That is fine while a person types every booking in, and it stops being fine as soon as something writes on their behalf. reservations.ingest_state is that gate. It defaults to live, SQLite backfills every existing row on the ALTER, and no current writer names the column, so every booking visible today stays visible. Nothing writes staged yet. The predicate takes its table alias because share.service.ts queried reservations without one. A stay is withheld only when bookings point at it and all of them are staged: a hand-added hotel has no booking at all and has to keep showing up.
This commit is contained in:
@@ -4136,6 +4136,24 @@ function runMigrations(db: Database.Database): void {
|
||||
console.warn('[migrations] Non-fatal migration step failed:', err);
|
||||
}
|
||||
},
|
||||
|
||||
// Reservations an automated ingest parked for review are 'staged' and stay
|
||||
// out of the two anonymous exports (ICS feed, shared trip) until a person
|
||||
// confirms them. Nothing writes 'staged' yet; this is the gate the mail
|
||||
// ingest writes through.
|
||||
//
|
||||
// 'live' as the default is what keeps this from being a breaking change:
|
||||
// SQLite fills every existing row on the ALTER, and no current writer names
|
||||
// the column, so every booking that is visible today stays visible.
|
||||
// Appended LAST, the array is index-addressed against schema_version.
|
||||
() => {
|
||||
const hasColumn = db
|
||||
.prepare("SELECT 1 FROM pragma_table_info('reservations') WHERE name = 'ingest_state'")
|
||||
.get();
|
||||
if (!hasColumn) {
|
||||
db.exec("ALTER TABLE reservations ADD COLUMN ingest_state TEXT NOT NULL DEFAULT 'live'");
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
if (currentVersion < migrations.length) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { ReservationsService } from '../reservations/reservations.service';
|
||||
import { publicReservationSql, publicStaySql } from '../reservations/reservation-visibility';
|
||||
import { addDays } from '../days/days.service';
|
||||
import { resolveTimeZone } from '../common/timezoneService';
|
||||
import { NotFoundError } from '../common/domain-errors';
|
||||
@@ -163,7 +164,7 @@ export class CalendarService {
|
||||
LEFT JOIN days ed ON a.end_day_id = ed.id
|
||||
LEFT JOIN days rd ON r.day_id = rd.id
|
||||
LEFT JOIN days red ON r.end_day_id = red.id
|
||||
WHERE r.trip_id = ?`,
|
||||
WHERE r.trip_id = ? AND ${publicReservationSql('r')}`,
|
||||
)
|
||||
.all(tripId) as any[];
|
||||
|
||||
@@ -569,13 +570,13 @@ export class CalendarService {
|
||||
sd.date AS start_date, ed.date AS end_date,
|
||||
p.name AS place_name, p.address AS place_address, p.lat AS place_lat, p.lng AS place_lng,
|
||||
(SELECT r.title FROM reservations r
|
||||
WHERE r.accommodation_id = a.id
|
||||
WHERE r.accommodation_id = a.id AND ${publicReservationSql('r')}
|
||||
ORDER BY r.id ASC LIMIT 1) AS reservation_title
|
||||
FROM day_accommodations a
|
||||
LEFT JOIN days sd ON a.start_day_id = sd.id
|
||||
LEFT JOIN days ed ON a.end_day_id = ed.id
|
||||
LEFT JOIN places p ON a.place_id = p.id
|
||||
WHERE a.trip_id = ?
|
||||
WHERE a.trip_id = ? AND ${publicStaySql('a')}
|
||||
ORDER BY a.id ASC
|
||||
`).all(tripId) as any[];
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Which reservation rows a viewer without a TREK session may see.
|
||||
*
|
||||
* `ingest_state` is 'live' for everything a person put on a trip, and 'staged'
|
||||
* for a row an automated ingest parked for review. Only the two anonymous
|
||||
* exports filter on it. The authenticated lists deliberately do not: the
|
||||
* staging inbox has to see its own rows, or nobody can confirm them.
|
||||
*
|
||||
* These are SQL fragments rather than a service. calendar/ and share/ are
|
||||
* separate modules and neither should have to import the other for a WHERE
|
||||
* clause. They take the table alias because share.service.ts queries
|
||||
* `reservations` without one while calendar.service.ts uses `r`.
|
||||
*
|
||||
* The COALESCE guards a NULL in the column, not a missing column: a table
|
||||
* without `ingest_state` fails the query outright rather than returning NULL.
|
||||
* The migration adds the column NOT NULL DEFAULT 'live', so a NULL should not
|
||||
* occur; the COALESCE costs nothing and keeps a row that predates a botched
|
||||
* ALTER visible rather than silently dropping it out of a live calendar feed.
|
||||
*/
|
||||
export const publicReservationSql = (alias = 'r'): string => `COALESCE(${alias}.ingest_state, 'live') <> 'staged'`;
|
||||
|
||||
/**
|
||||
* A stay is public when nothing points at it (somebody added it by hand), or
|
||||
* when a live booking does. Both halves matter: a plain EXISTS would drop every
|
||||
* hand-added hotel out of trips that are shared today.
|
||||
*
|
||||
* `accommodation_id` is a TEXT column that reads back as "14.0", hence the
|
||||
* CAST. Same coercion as reservations.service.ts.
|
||||
*/
|
||||
export const publicStaySql = (alias = 'a'): string => `(
|
||||
NOT EXISTS (SELECT 1 FROM reservations vr WHERE CAST(vr.accommodation_id AS INTEGER) = ${alias}.id)
|
||||
OR EXISTS (SELECT 1 FROM reservations vr WHERE CAST(vr.accommodation_id AS INTEGER) = ${alias}.id
|
||||
AND ${publicReservationSql('vr')})
|
||||
)`;
|
||||
@@ -5,6 +5,7 @@ import type { TripAccess } from '../database/database.service';
|
||||
import { PermissionsService } from '../permissions/permissions.service';
|
||||
import { QueryHelpersService } from '../query-helpers/query-helpers.service';
|
||||
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
|
||||
import { publicReservationSql, publicStaySql } from '../reservations/reservation-visibility';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
import type { User } from '../../types';
|
||||
|
||||
@@ -241,13 +242,18 @@ export class ShareService {
|
||||
if (!posMap.has(dp.reservation_id)) posMap.set(dp.reservation_id, {});
|
||||
posMap.get(dp.reservation_id)![dp.day_id] = dp.position;
|
||||
}
|
||||
reservations = this.dbs.all<any>('SELECT * FROM reservations WHERE trip_id = ? ORDER BY reservation_time ASC', tripId)
|
||||
// The alias is not cosmetic: the visibility predicate qualifies its column,
|
||||
// and this query had no alias to qualify against.
|
||||
reservations = this.dbs.all<any>(
|
||||
`SELECT r.* FROM reservations r
|
||||
WHERE r.trip_id = ? AND ${publicReservationSql('r')}
|
||||
ORDER BY r.reservation_time ASC`, tripId)
|
||||
.map((r) => ({ ...r, day_positions: posMap.get(r.id) ?? null }));
|
||||
|
||||
accommodations = this.dbs.all(`
|
||||
SELECT a.*, p.name as place_name, p.address as place_address, p.lat as place_lat, p.lng as place_lng
|
||||
FROM day_accommodations a JOIN places p ON a.place_id = p.id
|
||||
WHERE a.trip_id = ?
|
||||
WHERE a.trip_id = ? AND ${publicStaySql('a')}
|
||||
`, tripId);
|
||||
}
|
||||
|
||||
|
||||
@@ -625,8 +625,8 @@ export class TripsService {
|
||||
const reservationMap = new Map<number, number | bigint>();
|
||||
const insertReservation = this.db.prepare(`
|
||||
INSERT INTO reservations (trip_id, day_id, end_day_id, place_id, assignment_id, accommodation_id, title, reservation_time, reservation_end_time,
|
||||
location, confirmation_number, notes, url, status, type, metadata, day_plan_position, needs_review)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
location, confirmation_number, notes, url, status, type, metadata, day_plan_position, needs_review, ingest_state)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
for (const r of oldReservations) {
|
||||
const rr = insertReservation.run(newTripId,
|
||||
@@ -641,7 +641,10 @@ export class TripsService {
|
||||
r.accommodation_id != null ? (accomMap.get(Number(r.accommodation_id)) ?? null) : null,
|
||||
r.title, r.reservation_time, r.reservation_end_time,
|
||||
r.location, r.confirmation_number, r.notes, r.url, r.status, r.type,
|
||||
r.metadata, r.day_plan_position, r.needs_review ?? 0);
|
||||
// ingest_state travels with the copy: a staged booking must not turn
|
||||
// 'live' just because the trip was duplicated, or it lands in the
|
||||
// duplicate's public feed.
|
||||
r.metadata, r.day_plan_position, r.needs_review ?? 0, r.ingest_state ?? 'live');
|
||||
reservationMap.set(r.id, rr.lastInsertRowid);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user