fix(reservations): keep a mirrored booking price without keying it on the expense

The carry-over was conditional on a budget_items row still being linked to
the booking, which drops a real price in two shipped cases. The booking
importer writes metadata.price unconditionally but only creates the linked
cost when the Costs addon is on and the price is above zero, so an instance
with that addon off has bookings carrying a price and no budget row at all;
an MCP booking created with a price of 0 is in the same position. Both lost
the price on the first edit.

The condition is gone: a payload that does not name the key keeps what is
stored, and naming it, null included, is taken at face value so a price can
still be removed on purpose.

What stops a price from outliving its expense is now the other end.
deleteBudgetItem is the single chokepoint for all four delete paths, and it
clears the mirror there, inside the same transaction as the delete. That
also removes an ordering hazard the linked-row check had: the controller
runs update() before it syncs the budget, so on an explicit clear the row
still existed and the price would have come back a moment before it went.

The predicate moves into reservation-metadata.ts so it can be read and
tested on its own, and the update_transport tool description no longer
promises that metadata is replaced wholesale.
This commit is contained in:
Maurice
2026-09-04 19:07:00 +02:00
committed by Maurice
parent 1aafab83a5
commit 3e1c06a0ee
6 changed files with 156 additions and 49 deletions
@@ -680,6 +680,37 @@ describe('negative amounts persist end-to-end (#2176)', () => {
});
});
describe('deleting an expense takes its price off the booking (#2233)', () => {
it('BUDGET-SVC-DB-040: the mirrored price and currency are cleared, the rest of the metadata stays', () => {
// The reservation update path keeps metadata.price across booking edits, so
// the expense side has to remove it when the expense itself goes away.
// Without this the card would show a price with nothing behind it, for good.
const { user } = createUser(testDb, { username: 'owner' });
const trip = createTrip(testDb, user.id, { title: 'Trip' });
const res = testDb.prepare(
"INSERT INTO reservations (trip_id, title, type, metadata) VALUES (?, 'Flight', 'flight', ?)",
).run(trip.id, JSON.stringify({ airline: 'CZ', seat: '12A', price: '2040', priceCurrency: 'CNY' }));
const reservationId = Number(res.lastInsertRowid);
const item = budget.createBudgetItem(trip.id, { name: 'Flight', total_price: 2040 });
testDb.prepare('UPDATE budget_items SET reservation_id = ? WHERE id = ?').run(reservationId, item.id);
expect(budget.deleteBudgetItem(item.id, trip.id)).toBe(true);
const after = testDb.prepare('SELECT metadata FROM reservations WHERE id = ?').get(reservationId) as { metadata: string };
expect(JSON.parse(after.metadata)).toEqual({ airline: 'CZ', seat: '12A' });
});
it('BUDGET-SVC-DB-041: an expense that was never linked to a booking deletes as before', () => {
const { user } = createUser(testDb, { username: 'owner' });
const trip = createTrip(testDb, user.id, { title: 'Trip' });
const item = budget.createBudgetItem(trip.id, { name: 'Coffee', total_price: 4 });
expect(budget.deleteBudgetItem(item.id, trip.id)).toBe(true);
expect(testDb.prepare('SELECT id FROM budget_items WHERE id = ?').get(item.id)).toBeUndefined();
});
});
describe('an expense nobody paid stays out of the ledger (#2225)', () => {
it('BUDGET-SVC-DB-037: an unpaid expense moves neither the balances nor the offered flows', () => {
const { user: alice } = createUser(testDb, { username: 'alice' });
@@ -213,21 +213,37 @@ describe('ReservationsService (DI-native, real SQL)', () => {
expect(JSON.parse(reservation.metadata as string)).toEqual({ airline: 'CZ', seat: '12A', price: '2040', priceCurrency: 'CNY' });
});
it('RESV-SVC-034: a payload that sets `price` itself, or a booking with no linked expense, is stored as sent', () => {
it('RESV-SVC-034: a payload that names `price` is taken at face value, set or cleared', () => {
const { trip } = ownerTrip();
const linkedRes = createReservation(testDb, trip.id, { title: 'Linked', type: 'flight' });
const res = createReservation(testDb, trip.id, { title: 'Linked', type: 'flight' });
const item = createBudgetItem(testDb, trip.id, { name: 'Linked', total_price: 100 });
testDb.prepare('UPDATE budget_items SET reservation_id = ? WHERE id = ?').run(linkedRes.id, item.id);
testDb.prepare('UPDATE reservations SET metadata = ? WHERE id = ?').run(JSON.stringify({ price: '100' }), linkedRes.id);
let current = svc.getReservation(String(linkedRes.id), String(trip.id))!;
let { reservation } = svc.update(String(linkedRes.id), String(trip.id), { metadata: { price: '150' } }, current);
testDb.prepare('UPDATE budget_items SET reservation_id = ? WHERE id = ?').run(res.id, item.id);
testDb.prepare('UPDATE reservations SET metadata = ? WHERE id = ?').run(JSON.stringify({ price: '100' }), res.id);
let current = svc.getReservation(String(res.id), String(trip.id))!;
let { reservation } = svc.update(String(res.id), String(trip.id), { metadata: { price: '150' } }, current);
expect(JSON.parse(reservation.metadata as string)).toEqual({ price: '150' });
const orphanRes = createReservation(testDb, trip.id, { title: 'Orphan', type: 'flight' });
testDb.prepare('UPDATE reservations SET metadata = ? WHERE id = ?').run(JSON.stringify({ price: '999' }), orphanRes.id);
current = svc.getReservation(String(orphanRes.id), String(trip.id))!;
({ reservation } = svc.update(String(orphanRes.id), String(trip.id), { metadata: { seat: '1A' } }, current));
expect(JSON.parse(reservation.metadata as string)).toEqual({ seat: '1A' });
// Naming the key with a null is how a caller removes the price on purpose;
// it must not be read as "the form did not mention it".
current = svc.getReservation(String(res.id), String(trip.id))!;
({ reservation } = svc.update(String(res.id), String(trip.id), { metadata: { price: null, seat: '1A' } }, current));
expect(JSON.parse(reservation.metadata as string)).toEqual({ price: null, seat: '1A' });
});
it('RESV-SVC-036: a price with no linked expense is kept too, because the importer stamps one either way', () => {
// booking-import writes metadata.price unconditionally but only creates the
// linked cost when the Costs addon is on and the price is above zero, so an
// instance with that addon off has bookings carrying a price and no budget
// row at all. Keying the carry-over on a linked item would drop those.
const { trip } = ownerTrip();
const res = createReservation(testDb, trip.id, { title: 'Imported', type: 'flight' });
testDb.prepare('UPDATE reservations SET metadata = ? WHERE id = ?')
.run(JSON.stringify({ price: '999', priceCurrency: 'EUR' }), res.id);
const current = svc.getReservation(String(res.id), String(trip.id))!;
const { reservation } = svc.update(String(res.id), String(trip.id), { metadata: { seat: '1A' } }, current);
expect(JSON.parse(reservation.metadata as string)).toEqual({ seat: '1A', price: '999', priceCurrency: 'EUR' });
});
it('RESV-SVC-035: metadata null still clears the stored metadata, linked expense or not', () => {