feat(accommodations): put a booked night on its check-in day

A road-trip stop is a day assignment: the rail, the server-side plan and the
map all build their stops from day_assignments and only look the stay up
afterwards, to hang check-in and check-out on one. A stay written without an
assignment was therefore invisible to every routing surface, so a hotel entered
in the day planner never reached the road-trip view and had to be entered a
second time as an ordinary place. The road-trip side has always written both
halves in one go; this is the day planner catching up, in the domain that
writes the stay.

Booking a night now also assigns its place to the check-in day, inside the
transaction that already writes the stay and its partner hotel reservation, and
types the place as a hotel stop when it carries no type of its own, so it reads
exactly like a night booked in the road trip. Only the check-in day gets a
stop; the later nights ride on the stay row. A place already planned for that
day gets no second stop and the booking claims none, so cancelling it cannot
delete a stop somebody placed by hand. Moving the booking moves its own stop,
deleting it takes its own stop back, and a trip re-date carries it along with
the stay. All four write surfaces (REST, plugin RPC and both MCP tools) run the
same write and announce the same events; the answers carry the stop too,
because the broadcast skips the socket that sent the request.

New nullable day_assignments.accommodation_id marks a stop as a booking's own.
It stays NULL for every existing row on purpose: everything planned before this
was planned by hand, and no backfill rewrites a finished trip. A stay booked
earlier picks up its stop the next time somebody saves it.

The booking form keeps writing stays on its own: that surface is gated on
reservation_edit and the day stop is day_edit work.
This commit is contained in:
Maurice
2026-09-13 01:13:25 +02:00
parent 3ab5ddaacc
commit f0e1da2d81
19 changed files with 628 additions and 83 deletions
@@ -7,12 +7,15 @@ import type { User } from '../../../src/types';
const user = { id: 1, role: 'user', email: 'u@example.test' } as User;
const trip = { user_id: 1 };
const refs = { place_id: 2, start_day_id: 10, end_day_id: 11 };
/** A write that left the day plan alone, which is what most of these cases are about. */
const noMirror = { created: null, removed: [], stamped: null };
function makeService(overrides: Partial<AccommodationsService> = {}): AccommodationsService {
return {
verifyTripAccess: vi.fn().mockReturnValue(trip),
canEdit: vi.fn().mockReturnValue(true),
broadcast: vi.fn(),
announceMirror: vi.fn(),
validateRefs: vi.fn().mockReturnValue([]),
...overrides,
} as unknown as AccommodationsService;
@@ -48,13 +51,24 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
});
it('creates and emits accommodation:created + reservation:created', () => {
const create = vi.fn().mockReturnValue({ id: 9 });
const create = vi.fn().mockReturnValue({ accommodation: { id: 9 }, mirror: noMirror });
const broadcast = vi.fn();
const svc = makeService({ create, broadcast } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).create(user, '5', refs, 'sock')).toEqual({ accommodation: { id: 9 } });
expect(new AccommodationsController(svc).create(user, '5', refs, 'sock')).toEqual({ accommodation: { id: 9 }, assignment: null });
expect(broadcast).toHaveBeenCalledWith('5', 'accommodation:created', { accommodation: { id: 9 } }, 'sock');
expect(broadcast).toHaveBeenCalledWith('5', 'reservation:created', {}, 'sock');
});
it('ACC-CTL-001 hands the day stop back in the answer and announces it', () => {
// The broadcast skips the socket that sent the request, so the answer is the
// only way the session that booked the night learns about its own stop.
const stop = { id: 77, day_id: 10 };
const create = vi.fn().mockReturnValue({ accommodation: { id: 9 }, mirror: { created: stop, removed: [], stamped: null } });
const announceMirror = vi.fn();
const svc = makeService({ create, announceMirror } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).create(user, '5', refs, 'sock')).toEqual({ accommodation: { id: 9 }, assignment: stop });
expect(announceMirror).toHaveBeenCalledWith('5', { created: stop, removed: [], stamped: null }, expect.any(Function), 'sock');
});
});
describe('PUT /:id', () => {
@@ -65,12 +79,23 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
it('updates and broadcasts', () => {
const get = vi.fn().mockReturnValue({ id: 9 });
const update = vi.fn().mockReturnValue({ id: 9, notes: 'x' });
const update = vi.fn().mockReturnValue({ accommodation: { id: 9, notes: 'x' }, mirror: noMirror });
const broadcast = vi.fn();
const svc = makeService({ get, update, broadcast } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).update(user, '5', '9', refs, 'sock')).toEqual({ accommodation: { id: 9, notes: 'x' } });
expect(new AccommodationsController(svc).update(user, '5', '9', refs, 'sock')).toEqual({ accommodation: { id: 9, notes: 'x' }, assignment: null, removedAssignments: [] });
expect(broadcast).toHaveBeenCalledWith('5', 'accommodation:updated', { accommodation: { id: 9, notes: 'x' } }, 'sock');
});
it('ACC-CTL-002 a booking moved to another day reports the stop it took with it', () => {
const get = vi.fn().mockReturnValue({ id: 9 });
const mirror = { created: { id: 78, day_id: 11 }, removed: [{ id: 77, dayId: 10 }], stamped: null };
const update = vi.fn().mockReturnValue({ accommodation: { id: 9 }, mirror });
const announceMirror = vi.fn();
const svc = makeService({ get, update, announceMirror } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).update(user, '5', '9', refs, 'sock'))
.toEqual({ accommodation: { id: 9 }, assignment: mirror.created, removedAssignments: mirror.removed });
expect(announceMirror).toHaveBeenCalledWith('5', mirror, expect.any(Function), 'sock');
});
});
describe('DELETE /:id', () => {
@@ -83,11 +108,11 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
const get = vi.fn().mockReturnValue({ id: 9 });
const remove = vi.fn().mockReturnValue({
linkedReservationId: 4, deletedBudgetItemId: 7,
linkedReservationIds: [4], deletedBudgetItemIds: [7],
linkedReservationIds: [4], deletedBudgetItemIds: [7], mirror: noMirror,
});
const broadcast = vi.fn();
const svc = makeService({ get, remove, broadcast } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).remove(user, '5', '9', 'sock')).toEqual({ success: true });
expect(new AccommodationsController(svc).remove(user, '5', '9', 'sock')).toEqual({ success: true, removedAssignments: [] });
expect(broadcast).toHaveBeenCalledWith('5', 'reservation:deleted', { reservationId: 4 }, 'sock');
expect(broadcast).toHaveBeenCalledWith('5', 'budget:deleted', { itemId: 7 }, 'sock');
expect(broadcast).toHaveBeenCalledWith('5', 'accommodation:deleted', { accommodationId: 9 }, 'sock');
@@ -97,7 +122,7 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
const get = vi.fn().mockReturnValue({ id: 9 });
const remove = vi.fn().mockReturnValue({
linkedReservationId: 4, deletedBudgetItemId: null,
linkedReservationIds: [4, 5], deletedBudgetItemIds: [],
linkedReservationIds: [4, 5], deletedBudgetItemIds: [], mirror: noMirror,
});
const broadcast = vi.fn();
const svc = makeService({ get, remove, broadcast } as Partial<AccommodationsService>);
@@ -17,7 +17,9 @@ const { testDb, dbMock } = vi.hoisted(() => {
db,
closeDb: () => {},
reinitialize: () => {},
getPlaceWithTags: () => null,
// Real enough for the stop-type stamp a stay write makes: it announces the
// place it typed, and a null here would hide that the write happened at all.
getPlaceWithTags: (id: number | string) => db.prepare('SELECT * FROM places WHERE id = ?').get(id) ?? null,
canAccessTrip: (tripId: any, userId: number) =>
db.prepare(`
SELECT t.id, t.user_id FROM trips t
@@ -37,21 +39,18 @@ vi.mock('../../../src/websocket', () => ({ broadcast }));
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import { createUser, createTrip, createDay, createPlace, createDayAccommodation, addTripMember } from '../../helpers/factories';
import { createUser, createTrip, createDay, createPlace, createDayAccommodation, createDayAssignment, addTripMember } from '../../helpers/factories';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import { makeAccommodationsService } from '../../helpers/accommodations-service';
import { AccommodationsModule } from '../../../src/nest/accommodations/accommodations.module';
import { AccommodationsController } from '../../../src/nest/accommodations/accommodations.controller';
import { expectRegisteredProvider, expectRegisteredController } from '../../helpers/module-providers';
// Named `svc` so the moved cases read exactly as they did on DaysService.
const svc = new AccommodationsService(
new DatabaseService(testDb),
new PermissionsService(new DatabaseService(testDb)),
new RealtimeService(),
);
const svc = makeAccommodationsService(testDb);
beforeAll(() => {
createTables(testDb);
@@ -119,7 +118,7 @@ describe('createAccommodation', () => {
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id,
start_day_id: day.id,
end_day_id: day.id,
@@ -137,7 +136,7 @@ describe('createAccommodation', () => {
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'City Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
@@ -155,7 +154,7 @@ describe('createAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: null as unknown as number, start_day_id: day.id, end_day_id: day.id,
}) as any;
@@ -190,12 +189,12 @@ describe('updateAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const updated = svc.updateAccommodation(accom.id, existing as any, { check_in: '16:00', check_out: '12:00' }) as any;
const { accommodation: updated } = svc.updateAccommodation(accom.id, existing as any, { check_in: '16:00', check_out: '12:00' }) as any;
expect(updated).toBeDefined();
// Verify linked reservation metadata was synced
@@ -211,7 +210,7 @@ describe('updateAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
confirmation: 'ABC123',
}) as any;
@@ -234,7 +233,7 @@ describe('updateAccommodation', () => {
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const updated = svc.updateAccommodation(accom.id, existing as any, { check_in: '15:00' }) as any;
const { accommodation: updated } = svc.updateAccommodation(accom.id, existing as any, { check_in: '15:00' }) as any;
expect(updated.check_in).toBe('15:00');
expect(testDb.prepare('SELECT COUNT(*) as n FROM reservations WHERE accommodation_id = ?').get(accom.id)).toMatchObject({ n: 0 });
@@ -248,7 +247,7 @@ describe('updateAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id, check_in: '15:00',
}) as any;
@@ -268,7 +267,7 @@ describe('updateAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
testDb.prepare('UPDATE reservations SET confirmation_number = ? WHERE accommodation_id = ?').run('RES-9', accom.id);
@@ -287,7 +286,7 @@ describe('deleteAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
@@ -326,7 +325,7 @@ describe('deleteAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
@@ -342,6 +341,7 @@ describe('deleteAccommodation', () => {
deletedBudgetItemId: budgetItemId,
linkedReservationIds: [reservation.id],
deletedBudgetItemIds: [budgetItemId],
mirror: { created: null, removed: [{ id: expect.any(Number), dayId: day.id }], stamped: null },
});
expect(testDb.prepare('SELECT id FROM budget_items WHERE id = ?').get(budgetItemId)).toBeUndefined();
});
@@ -354,7 +354,7 @@ describe('deleteAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
@@ -375,7 +375,7 @@ describe('deleteAccommodation', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as any;
testDb.prepare(
@@ -412,9 +412,9 @@ describe('quirk fixes', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel' });
const accom = svc.createAccommodation(trip.id, {
const { accommodation: accom } = svc.createAccommodation(trip.id, {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
}) as { id: number };
}) as { accommodation: { id: number } };
testDb.exec("CREATE TRIGGER boom BEFORE DELETE ON day_accommodations BEGIN SELECT RAISE(ABORT, 'boom'); END");
try {
expect(() => svc.deleteAccommodation(accom.id)).toThrow();
@@ -462,7 +462,7 @@ describe('route-facing delegators', () => {
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
// Both ids arrive from the route as strings.
const created = svc.create(String(trip.id), {
const { accommodation: created } = svc.create(String(trip.id), {
place_id: place.id, start_day_id: day.id, end_day_id: day.id, confirmation: 'XY-1',
}) as any;
@@ -478,10 +478,10 @@ describe('route-facing delegators', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) as any;
const { accommodation: accom } = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) as any;
const existing = svc.get(accom.id, trip.id)!;
const updated = svc.update(String(accom.id), existing as any, { check_in: '16:00', notes: 'late arrival' }) as any;
const { accommodation: updated } = svc.update(String(accom.id), existing as any, { check_in: '16:00', notes: 'late arrival' }) as any;
expect(updated).toMatchObject({ check_in: '16:00', notes: 'late arrival' });
const reservation = testDb.prepare('SELECT metadata FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
@@ -493,7 +493,7 @@ describe('route-facing delegators', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) as any;
const { accommodation: accom } = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) as any;
const reservation = testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
// The controller broadcasts reservation:deleted off this return value.
@@ -502,6 +502,7 @@ describe('route-facing delegators', () => {
deletedBudgetItemId: null,
linkedReservationIds: [reservation.id],
deletedBudgetItemIds: [],
mirror: { created: null, removed: [{ id: expect.any(Number), dayId: day.id }], stamped: null },
});
expect(svc.get(accom.id, trip.id)).toBeUndefined();
});
@@ -580,6 +581,214 @@ describe('trip access and edit permission', () => {
});
});
/**
* Booking a night also puts its place on the check-in day, because that stop is what
* the road trip routes and the map draws. Reported from Discord: a hotel entered in
* the day planner never reached the road-trip view, so the same place had to be
* entered a second time as an ordinary stop.
*/
describe('the day stop a booking implies', () => {
const stopsOn = (dayId: number) =>
testDb.prepare('SELECT id, place_id, order_index, accommodation_id FROM day_assignments WHERE day_id = ? ORDER BY order_index').all(dayId) as
{ id: number; place_id: number; order_index: number; accommodation_id: number | null }[];
const book = (tripId: number, placeId: number | null, startDayId: number, endDayId: number) =>
svc.createAccommodation(tripId, { place_id: placeId as number, start_day_id: startDayId, end_day_id: endDayId }) as any;
it('ACC-021 the booked night puts its place on the check-in day and marks the stop as its own', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const { accommodation, mirror } = book(trip.id, place.id, day.id, day.id);
const stops = stopsOn(day.id);
expect(stops).toHaveLength(1);
expect(stops[0]).toMatchObject({ place_id: place.id, accommodation_id: accommodation.id });
expect(mirror.created).toMatchObject({ id: stops[0].id, day_id: day.id, place_id: place.id });
});
it('ACC-022 the stop lands at the end of the day, where you arrive at a hotel', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const museum = createPlace(testDb, trip.id, { name: 'Pergamon' });
const hotel = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
createDayAssignment(testDb, day.id, museum.id);
book(trip.id, hotel.id, day.id, day.id);
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([museum.id, hotel.id]);
});
it('ACC-023 the place is typed as lodging, so the rail draws it as a service stop', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const { mirror } = book(trip.id, place.id, day.id, day.id);
expect(testDb.prepare('SELECT stop_type FROM places WHERE id = ?').get(place.id)).toMatchObject({ stop_type: 'hotel' });
// Announced, or the places list keeps the stale null until a reload.
expect(mirror.stamped).toMatchObject({ id: place.id, stop_type: 'hotel' });
// And the stop carries it, so the rail does not number it.
expect(mirror.created.place.stop_type).toBe('hotel');
});
it('ACC-024 a stop type the traveller picked is never overwritten', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Camping Isar' });
testDb.prepare("UPDATE places SET stop_type = 'campsite' WHERE id = ?").run(place.id);
const { mirror } = book(trip.id, place.id, day.id, day.id);
expect(testDb.prepare('SELECT stop_type FROM places WHERE id = ?').get(place.id)).toMatchObject({ stop_type: 'campsite' });
expect(mirror.stamped).toBeNull();
});
it('ACC-025 a place already planned for that day gets no second stop, and the booking claims neither', () => {
// This is the road-trip flow: it assigns the place to the day and only then books
// the night. A second stop would draw the hotel twice, and claiming the existing
// one would let cancelling the booking delete a stop the traveller placed.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const own = createDayAssignment(testDb, day.id, place.id);
const { mirror } = book(trip.id, place.id, day.id, day.id);
expect(stopsOn(day.id)).toEqual([expect.objectContaining({ id: own.id, accommodation_id: null })]);
expect(mirror.created).toBeNull();
});
it('ACC-026 only the check-in day, not every night of a long stay', () => {
// You drive there once. The later nights ride on the stay row, which is where the
// rail reads check-out from.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const arrive = createDay(testDb, trip.id);
const middle = createDay(testDb, trip.id);
const leave = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
book(trip.id, place.id, arrive.id, leave.id);
expect(stopsOn(arrive.id)).toHaveLength(1);
expect(stopsOn(middle.id)).toHaveLength(0);
expect(stopsOn(leave.id)).toHaveLength(0);
});
it('ACC-027 a stay with no place writes no stop instead of throwing', () => {
// day_accommodations.place_id is nullable (ON DELETE SET NULL) and the booking form
// writes stays that never had one.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const { mirror } = book(trip.id, null, day.id, day.id);
expect(mirror).toEqual({ created: null, removed: [], stamped: null });
expect(stopsOn(day.id)).toHaveLength(0);
});
it('ACC-028 moving the booking to another day moves its stop with it', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day1 = createDay(testDb, trip.id);
const day2 = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const { accommodation } = book(trip.id, place.id, day1.id, day1.id);
const before = stopsOn(day1.id)[0];
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const { mirror } = svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id }) as any;
expect(stopsOn(day1.id)).toHaveLength(0);
expect(stopsOn(day2.id)).toEqual([expect.objectContaining({ place_id: place.id, accommodation_id: accommodation.id })]);
expect(mirror.removed).toEqual([{ id: before.id, dayId: day1.id }]);
expect(mirror.created).toMatchObject({ day_id: day2.id });
});
it('ACC-029 editing an unrelated field leaves the stop exactly where it is', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const { accommodation } = book(trip.id, place.id, day.id, day.id);
const before = stopsOn(day.id);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const { mirror } = svc.updateAccommodation(accommodation.id, existing, { check_in: '16:00' }) as any;
expect(stopsOn(day.id)).toEqual(before);
expect(mirror).toEqual({ created: null, removed: [], stamped: null });
});
it('ACC-030 a stay booked before any of this existed picks up its stop on the next save', () => {
// The deliberate alternative to rewriting everybody finished trips in a migration:
// the booking reaches the route the moment somebody opens it and saves.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const old = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
const existing = svc.getAccommodation(old.id, trip.id)!;
svc.updateAccommodation(old.id, existing, { check_in: '15:00' });
expect(stopsOn(day.id)).toEqual([expect.objectContaining({ place_id: place.id, accommodation_id: old.id })]);
});
it('ACC-031 cancelling a booking takes back its own stop and leaves the other one', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const hotel = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const museum = createPlace(testDb, trip.id, { name: 'Pergamon' });
const own = createDayAssignment(testDb, day.id, museum.id);
const { accommodation } = book(trip.id, hotel.id, day.id, day.id);
const mirrored = stopsOn(day.id).find(a => a.place_id === hotel.id)!;
const { mirror } = svc.deleteAccommodation(accommodation.id);
expect(stopsOn(day.id)).toEqual([expect.objectContaining({ id: own.id })]);
expect(mirror.removed).toEqual([{ id: mirrored.id, dayId: day.id }]);
});
it('ACC-032 cancelling a night booked in the road trip keeps the stop standing', () => {
// The rail turns a night back into a pause by deleting the stay alone; the stop is
// the thing being edited there and has to survive.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' });
const own = createDayAssignment(testDb, day.id, place.id);
const { accommodation } = book(trip.id, place.id, day.id, day.id);
const { mirror } = svc.deleteAccommodation(accommodation.id);
expect(stopsOn(day.id)).toEqual([expect.objectContaining({ id: own.id })]);
expect(mirror.removed).toEqual([]);
});
it('ACC-033 announceMirror sends the removal before the arrival', () => {
// Order matters on a move: the day plan would briefly hold the place twice if the
// arrival went first.
const sent: string[] = [];
svc.announceMirror(5, {
created: { id: 78, day_id: 11 } as never,
removed: [{ id: 77, dayId: 10 }],
stamped: { id: 3 } as never,
}, event => { sent.push(event); });
expect(sent).toEqual(['assignment:deleted', 'assignment:created', 'place:updated']);
});
});
describe('AccommodationsService wiring', () => {
it('ACC-001: the module carries the controller, the service and the RPC surface', () => {
expectRegisteredController(AccommodationsModule, AccommodationsController);
+2 -2
View File
@@ -62,11 +62,11 @@ const bridgeGetDay = (id: string | number, tripId: string | number) => svc.getDa
const bridgeListDays = (tripId: string | number) => svc.list(tripId);
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
import { QueryHelpersService } from '../../../src/nest/query-helpers/query-helpers.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import { makeAccommodationsService } from '../../helpers/accommodations-service';
import type { Day } from '../../../src/types';
const svc = new DaysService(new DatabaseService(testDb), new PermissionsService(new DatabaseService(testDb)), new RealtimeService(), new QueryHelpersService(new DatabaseService(testDb)));
const accommodations = new AccommodationsService(new DatabaseService(testDb), new PermissionsService(new DatabaseService(testDb)), new RealtimeService());
const accommodations = makeAccommodationsService(testDb);
beforeAll(() => {
createTables(testDb);
@@ -66,6 +66,7 @@ import { UserCleanupService } from '../../../src/nest/auth/user-cleanup.service'
import { TripMembersService } from '../../../src/nest/trip-members/trip-members.service';
import { TripReadModelService } from '../../../src/nest/trip-read-model/trip-read-model.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import { makeAccommodationsService } from '../../helpers/accommodations-service';
import { MapsService } from '../../../src/nest/maps/maps.service';
import { QueryHelpersService } from '../../../src/nest/query-helpers/query-helpers.service';
import { notificationsStub } from '../../helpers/notifications';
@@ -93,7 +94,7 @@ const placesSvc = new PlacesService(
new JourneyDomainService(dbs(), new RealtimeService(), new TrekPhotosRepository(dbs())),
makeStorageFixture('').storage,
);
const accommodationsSvc = new AccommodationsService(dbs(), new PermissionsService(dbs()), new RealtimeService());
const accommodationsSvc = makeAccommodationsService(testDb);
const membersSvc = new TripMembersService(dbs(), budgetSvc, new UserCleanupService(dbs(), budgetSvc), new PermissionsService(dbs()), new RealtimeService(), notificationsStub());
const buildReadModel = (database: DatabaseService, roster: TripMembersService = membersSvc) =>
+20 -3
View File
@@ -74,6 +74,7 @@ import { UserCleanupService } from '../../../src/nest/auth/user-cleanup.service'
import { TripMembersService } from '../../../src/nest/trip-members/trip-members.service';
import { TripReadModelService } from '../../../src/nest/trip-read-model/trip-read-model.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import { makeAccommodationsService } from '../../helpers/accommodations-service';
import { MapsService } from '../../../src/nest/maps/maps.service';
import { UnsplashService } from '../../../src/nest/unsplash/unsplash.service';
import { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
@@ -112,7 +113,7 @@ const placesSvc = new PlacesService(
new JourneyDomainService(dbs(), new RealtimeService(), new TrekPhotosRepository(dbs())),
makeStorageFixture('').storage,
);
const accommodationsSvc = new AccommodationsService(dbs(), new PermissionsService(dbs()), new RealtimeService());
const accommodationsSvc = makeAccommodationsService(testDb);
const createAccommodation = accommodationsSvc.createAccommodation.bind(accommodationsSvc);
const svc = new TripsService(
@@ -449,9 +450,9 @@ describe('resyncAccommodationDays (#1288)', () => {
const insertAccommodation = (tripId: number, startDayId: number, endDayId: number) => {
const place = createPlace(testDb, tripId, { name: 'Grand Hotel' });
const acc = createAccommodation(tripId, {
const { accommodation: acc } = createAccommodation(tripId, {
place_id: place.id, start_day_id: startDayId, end_day_id: endDayId,
}) as { id: number };
}) as { accommodation: { id: number } };
const linkedRes = testDb.prepare(
'SELECT id FROM reservations WHERE accommodation_id = ?',
).get(acc.id) as { id: number };
@@ -479,6 +480,22 @@ describe('resyncAccommodationDays (#1288)', () => {
expect(res.reservation_time?.slice(0, 10)).toBe('2025-06-11');
});
it('TRIP-SVC-059: the day stop a booking wrote follows it when the trip is re-dated', () => {
// Booking a night also puts its place on the check-in day. Re-dating the trip moves
// the stay to whichever day row now carries its date, and the stop has to go with
// it, or the route runs through a day the traveller is no longer staying on.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { start_date: '2025-06-10', end_date: '2025-06-14' });
const { accId } = insertAccommodation(trip.id, dayFor(trip.id, '2025-06-11'), dayFor(trip.id, '2025-06-13'));
const stopOf = () => testDb.prepare('SELECT day_id FROM day_assignments WHERE accommodation_id = ?').get(accId) as { day_id: number };
expect(stopOf().day_id).toBe(dayFor(trip.id, '2025-06-11'));
svc.updateTrip(trip.id, user.id, { start_date: '2025-06-09', end_date: '2025-06-14' }, 'user');
expect(stopOf().day_id).toBe(getAcc(accId).start_day_id);
expect(stopOf().day_id).toBe(dayFor(trip.id, '2025-06-11'));
});
it('TRIP-SVC-036: moving the whole trip out of the old range keeps the accommodation glued to its days', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-05' });
@@ -45,19 +45,25 @@ function build(opts: { canEdit?: boolean; cascade?: boolean; seenActions?: strin
syncBudgetOnUpdate: vi.fn(),
notifyBookingChange: vi.fn(),
} as unknown as ReservationsService & Record<string, ReturnType<typeof vi.fn>>;
/** A stay write that left the day plan alone. */
const noMirror = () => ({ created: null, removed: [], stamped: null });
// The lodging blocks live in AccommodationsService; AccommodationsRpc still calls its
// injected copy `days`, which is why the fixture keeps that name.
const days = {
validateAccommodationRefs: vi.fn((_t: number, placeId?: number) => (placeId === 404 ? [{ message: 'place 404 is not on this trip' }] : [])),
createAccommodation: vi.fn(() => ({ id: 60 })),
createAccommodation: vi.fn(() => ({ accommodation: { id: 60 }, mirror: noMirror() })),
getAccommodation: vi.fn((id: number) => (id === 11 ? { id: 11 } : undefined)),
updateAccommodation: vi.fn(() => ({ id: 11 })),
updateAccommodation: vi.fn(() => ({ accommodation: { id: 11 }, mirror: noMirror() })),
deleteAccommodation: vi.fn(() => ({
linkedReservationId: opts.cascade ? 40 : null,
deletedBudgetItemId: opts.cascade ? 7 : null,
linkedReservationIds: opts.cascade ? [40] : [],
deletedBudgetItemIds: opts.cascade ? [7] : [],
mirror: noMirror(),
})),
// The real one broadcasts the day stop a booking writes; these cases are about
// the booking events, so the fixture keeps it inert and BOOK-RPC-016 covers it.
announceMirror: vi.fn(),
} as unknown as AccommodationsService & Record<string, ReturnType<typeof vi.fn>>;
const guards = new PluginGuards(
{
@@ -243,6 +249,14 @@ describe('AccommodationsRpc', () => {
expect(f.days.updateAccommodation).not.toHaveBeenCalled();
});
it('BOOK-RPC-017 the day stop a booking writes is announced through the plugin surface too', async () => {
const f = build();
await f.host().dispatch(req('accommodations.create', { tripId: 1, input: { place_id: 7, start_day_id: 3, end_day_id: 4 } }), 42);
// Same call the REST route makes, so a plugin booking a night cannot leave the
// other sessions without the stop that puts it on the route.
expect(f.days.announceMirror).toHaveBeenCalledWith(1, { created: null, removed: [], stamped: null }, expect.any(Function));
});
it('BOOK-RPC-015d deleting a block without a partner cascades only its own event', async () => {
const f = build();
await f.host().dispatch(req('accommodations.delete', { tripId: 1, accommodationId: 11 }), 42);