mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
fix(roadtrip): keep a booked night out of the day list, and off the traveller's stops
Three things the first pass got wrong. A stop a booking puts on the day is for road trip mode, which needs the hotel in the driving chain. The day planner already shows that booking as its own overnight block, so the row there would be the same hotel twice. It is hidden in Days whatever the service-stop switch says, and the switch goes back to meaning what it says: the petrol stations the traveller added. Cancelling a night from the road trip popup took the stop with it. That control means "this is a pause now, not a night", and the stop sits mid-drive where re-adding it would land it at the end of the day. The booking now hands the stop over instead of deleting it. Moving a booking whose stop belongs to the traveller put a second one on the new day and left the first behind, which is the duplicate this change exists to remove. Only a booking's own stop moves now. Stays booked before this shipped get theirs from the migration, not from the next edit they happen to receive.
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Query,
|
||||
Get,
|
||||
Headers,
|
||||
HttpException,
|
||||
@@ -117,11 +118,15 @@ export class AccommodationsController {
|
||||
@Param('tripId') tripId: string,
|
||||
@Param('id') id: string,
|
||||
@Headers('x-socket-id') socketId?: string,
|
||||
@Query('keepStop') keepStop?: string,
|
||||
) {
|
||||
if (!this.accommodations.get(id, tripId)) {
|
||||
throw new HttpException({ error: 'Accommodation not found' }, 404);
|
||||
}
|
||||
const { linkedReservationIds, deletedBudgetItemIds, mirror } = this.accommodations.remove(id);
|
||||
// Turning a night back into a pause in road trip mode: the booking goes, the
|
||||
// stop stays and becomes the traveller's. Everywhere else a cancelled booking
|
||||
// takes the stop it brought with it.
|
||||
const { linkedReservationIds, deletedBudgetItemIds, mirror } = this.accommodations.remove(id, { keepStop: keepStop === 'true' });
|
||||
this.accommodations.announceMirror(tripId, mirror, (event, payload) => this.accommodations.broadcast(tripId, event, payload, socketId), socketId);
|
||||
for (const reservationId of linkedReservationIds) {
|
||||
this.accommodations.broadcast(tripId, 'reservation:deleted', { reservationId }, socketId);
|
||||
|
||||
@@ -123,8 +123,8 @@ export class AccommodationsService {
|
||||
return this.updateAccommodation(id, existing, fields);
|
||||
}
|
||||
|
||||
remove(id: string | number) {
|
||||
return this.deleteAccommodation(id);
|
||||
remove(id: string | number, opts: { keepStop?: boolean } = {}) {
|
||||
return this.deleteAccommodation(id, opts);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,15 +239,18 @@ export class AccommodationsService {
|
||||
/**
|
||||
* Carry the mirrored stop over to wherever the booking now is.
|
||||
*
|
||||
* A booking that owns no stop gets one, which is how a stay made before any of
|
||||
* this existed picks one up: re-save it once and it appears. The cost is that a
|
||||
* mirrored stop somebody deleted by hand comes back the next time they touch the
|
||||
* booking, which is the lesser of the two surprises.
|
||||
* Only its own stop moves. A booking that owns none is one whose stop belongs to
|
||||
* the traveller: booked in road trip mode, where the place was put on the day
|
||||
* first, or booked for a place they had already planned there. Moving that is not
|
||||
* ours to do, and putting a second one on the new day next to it is exactly the
|
||||
* duplicate this whole change is meant to remove. Stays booked before any of this
|
||||
* existed get their stop from the migration, not from the next edit.
|
||||
*
|
||||
* Runs inside the caller's transaction.
|
||||
*/
|
||||
private remirrorStay(accommodationId: number, placeId: number | null, dayId: number): AccommodationMirror {
|
||||
const own = this.ownStops(accommodationId);
|
||||
if (own.length === 0) return noMirror();
|
||||
if (own.length === 1 && own[0].day_id === dayId && own[0].place_id === placeId) return noMirror();
|
||||
|
||||
const mirror = noMirror();
|
||||
@@ -350,7 +353,7 @@ export class AccommodationsService {
|
||||
* that no longer exists. `linkedReservationId` / `deletedBudgetItemId` stay on the
|
||||
* result as the first of each, because the RPC, MCP and REST callers read them.
|
||||
*/
|
||||
deleteAccommodation(id: string | number): {
|
||||
deleteAccommodation(id: string | number, opts: { keepStop?: boolean } = {}): {
|
||||
linkedReservationId: number | null;
|
||||
deletedBudgetItemId: number | null;
|
||||
linkedReservationIds: number[];
|
||||
@@ -372,8 +375,17 @@ export class AccommodationsService {
|
||||
// Only the stops this booking put there itself. A stop the traveller placed
|
||||
// and then booked a night at keeps standing, which is how cancelling a night
|
||||
// in the road trip has always behaved.
|
||||
//
|
||||
// keepStop hands it to the traveller instead of taking it away. That is the
|
||||
// road trip popup turning a night back into a pause: they asked to drop the
|
||||
// booking, not the place, and the stop is mid-drive where re-adding it would
|
||||
// land it at the end of the day.
|
||||
const mirror = noMirror();
|
||||
for (const stop of this.ownStops(Number(id))) {
|
||||
if (opts.keepStop) {
|
||||
this.db.run('UPDATE day_assignments SET accommodation_id = NULL WHERE id = ?', stop.id);
|
||||
continue;
|
||||
}
|
||||
this.db.run('DELETE FROM day_assignments WHERE id = ?', stop.id);
|
||||
mirror.removed.push({ id: stop.id, dayId: stop.day_id });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user