mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
fix(accommodations): keep booking confirmations in sync
Distinguish omitted references from explicit clears on stays and their linked bookings. Reserve automatic confirmation for a new reference with a booking code format. Announce linked changes after booking edits. Closes #3301
This commit is contained in:
@@ -121,13 +121,8 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
.execute('run');
|
||||
}
|
||||
|
||||
/** RS30/RS43 — `UPDATE day_accommodations SET confirmation = COALESCE(?, confirmation) WHERE id = ?`. */
|
||||
async patchConfirmation(id: number, confirmation: string | null): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.qb()
|
||||
.update({ confirmation: coalesceOverride(platform, confirmation, 'confirmation') })
|
||||
.where({ id })
|
||||
.execute('run');
|
||||
await this.nativeUpdate({ id }, { confirmation });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,14 +154,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
return row?.check_in;
|
||||
}
|
||||
|
||||
/**
|
||||
* RS39 — `UPDATE day_accommodations SET place_id = ?, start_day_id = ?,
|
||||
* end_day_id = ?, check_in = ?, check_out = ?, confirmation = ? WHERE id =
|
||||
* ?`. Six columns (no `check_in_end`/`notes`, unlike `AccommodationsService
|
||||
* .moveStay`'s own write) — the caller's already-decided values, written
|
||||
* verbatim (the `PlacesRepository.updatePlace`/PL11 precedent: no SQL-side
|
||||
* COALESCE here, the legacy statement itself has none for these columns).
|
||||
*/
|
||||
/** An omitted confirmation leaves the stay's existing reference in place. */
|
||||
async updateFromBooking(
|
||||
id: number,
|
||||
write: {
|
||||
@@ -175,7 +163,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
end_day_id: number;
|
||||
check_in: string | null;
|
||||
check_out: string | null;
|
||||
confirmation: string | null;
|
||||
confirmation?: string | null;
|
||||
},
|
||||
): Promise<void> {
|
||||
await this.nativeUpdate(
|
||||
@@ -186,7 +174,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
endDay: write.end_day_id,
|
||||
check_in: write.check_in,
|
||||
check_out: write.check_out,
|
||||
confirmation: write.confirmation,
|
||||
...(write.confirmation !== undefined ? { confirmation: write.confirmation } : {}),
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -343,7 +331,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
check_in: string | null;
|
||||
check_in_end: string | null;
|
||||
check_out: string | null;
|
||||
confirmation: string | null;
|
||||
confirmation?: string | null;
|
||||
notes: string | null;
|
||||
},
|
||||
): Promise<void> {
|
||||
@@ -356,7 +344,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
|
||||
check_in: write.check_in,
|
||||
check_in_end: write.check_in_end,
|
||||
check_out: write.check_out,
|
||||
confirmation: write.confirmation,
|
||||
...(write.confirmation !== undefined ? { confirmation: write.confirmation } : {}),
|
||||
notes: write.notes,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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, isCalendarDate } from '../dialect/sql-functions';
|
||||
import { 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';
|
||||
@@ -663,24 +663,14 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
// Reservations repositories where a read belongs there").
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* AC33 (`AccommodationsService.createAccommodation`) — `INSERT INTO
|
||||
* reservations (trip_id, day_id, title, reservation_time, location,
|
||||
* confirmation_number, notes, status, type, accommodation_id, metadata)
|
||||
* VALUES (?, ?, ?, ?, ?, ?, ?, 'confirmed', 'hotel', ?, ?)` — the
|
||||
* auto-created partner hotel booking. `status`/`type` literals, `location`
|
||||
* always `null` (matching the legacy statement exactly — it never binds a
|
||||
* value there). `accommodation_id: string` — R2: the caller stringifies
|
||||
* the new stay's numeric id (`String(newId)`) the same way `insertReservation`'s
|
||||
* own `accommodation_id` caller already does, so SQLite's TEXT-affinity
|
||||
* storage is identical to the legacy raw bind of the bare number.
|
||||
*/
|
||||
/** The partner hotel booking created with a stay. The caller decides its initial status. */
|
||||
async insertHotelPartner(input: {
|
||||
trip_id: number | string;
|
||||
day_id: number;
|
||||
title: string;
|
||||
reservation_time: string | null;
|
||||
confirmation_number: string | null;
|
||||
status: 'pending' | 'confirmed';
|
||||
notes: string | null;
|
||||
accommodation_id: string;
|
||||
metadata: string | null;
|
||||
@@ -693,8 +683,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
location: null,
|
||||
confirmation_number: input.confirmation_number,
|
||||
notes: input.notes,
|
||||
// A stay without a confirmation number is not a confirmed booking yet.
|
||||
status: input.confirmation_number ? 'confirmed' : 'pending',
|
||||
status: input.status,
|
||||
type: 'hotel',
|
||||
accommodation_id: input.accommodation_id,
|
||||
metadata: input.metadata,
|
||||
@@ -719,24 +708,23 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
return rows as { id: number; metadata: string | null }[];
|
||||
}
|
||||
|
||||
/**
|
||||
* AC38 (`AccommodationsService.updateAccommodation`) — `UPDATE
|
||||
* reservations SET metadata = ?, confirmation_number = COALESCE(?,
|
||||
* confirmation_number) WHERE id = ?`. Runs AFTER `updateAccommodation`'s
|
||||
* own transaction commits (§18.6 — R5 class, flagged not fixed).
|
||||
* `coalesceOverride` for the value-side COALESCE — the bound `?` FIRST,
|
||||
* the column the fallback, so the new value wins
|
||||
* (`DayAccommodationsRepository.patchConfirmation`'s precedent).
|
||||
*/
|
||||
async setMetadataAndConfirmation(id: number, metadata: string, confirmation: string | null): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.qb()
|
||||
.update({
|
||||
/** Clearing a reference reopens a confirmed booking that actually held a reference. */
|
||||
async setMetadataAndConfirmation(id: number, metadata: string, confirmation?: string | null): Promise<void> {
|
||||
await this.kysely<Pick<DB, 'reservations'>>()
|
||||
.updateTable('reservations')
|
||||
.set((eb) => ({
|
||||
metadata,
|
||||
confirmation_number: coalesceOverride(platform, confirmation, 'confirmation_number'),
|
||||
})
|
||||
.where({ id })
|
||||
.execute('run');
|
||||
confirmation_number: confirmation,
|
||||
status: confirmation === null
|
||||
? eb.case()
|
||||
.when(eb.and([eb('status', '=', 'confirmed'), eb('confirmation_number', '!=', '')]))
|
||||
.then('pending')
|
||||
.else(eb.ref('status'))
|
||||
.end()
|
||||
: undefined,
|
||||
}))
|
||||
.where('id', '=', id)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** A pending hotel booking turns confirmed once its stay gets a confirmation number; other statuses stay. */
|
||||
|
||||
Reference in New Issue
Block a user