diff --git a/server/scripts/size-baseline.json b/server/scripts/size-baseline.json index 81a14c753..13870ed38 100644 --- a/server/scripts/size-baseline.json +++ b/server/scripts/size-baseline.json @@ -17,7 +17,7 @@ "src/nest/places/places.service.ts": 2020, "src/nest/plugins/plugin-runtime.service.ts": 1463, "src/nest/plugins/runtime/plugin-sdk.ts": 1094, - "src/nest/reservations/reservations.service.ts": 1247, + "src/nest/reservations/reservations.service.ts": 1240, "src/nest/trips/trips.service.ts": 1013, "src/nest/vacay/vacay.service.ts": 1440 } diff --git a/server/src/nest/reservations/reservations.service.ts b/server/src/nest/reservations/reservations.service.ts index 7cd214eb8..82042a4a5 100644 --- a/server/src/nest/reservations/reservations.service.ts +++ b/server/src/nest/reservations/reservations.service.ts @@ -1133,24 +1133,21 @@ export class ReservationsService { return { ...rest, currency, ...(priced.exchange_rate != null ? { exchange_rate: priced.exchange_rate } : {}) }; } - /** POST side effect: auto-create a linked budget item when a price is provided. `collect` holds the broadcast for a caller inside its transaction. */ + /** POST side effect: link a budget item when a price is provided. `collect` holds the broadcast for the caller's transaction. */ async syncBudgetOnCreate(tripId: string, reservationId: number, title: string, type: string | undefined, entry: BudgetEntry, socketId: string | undefined, collect?: CostEvent[]): Promise { if (!entry || !(Number(entry.total_price) > 0)) return; - try { - const item = await this.budget.linkBudgetItemToReservation(tripId, reservationId, { - name: title, - category: entry.category || type || 'Other', - total_price: entry.total_price!, - ...(entry.currency ? { currency: entry.currency } : {}), - ...(entry.exchange_rate != null ? { exchange_rate: entry.exchange_rate } : {}), - }); - this.sendCost(tripId, { event: 'budget:created', payload: { item } }, socketId, collect); - } catch (err) { - console.error('[reservations] Failed to create budget entry:', err); - } + // A failing cost write is not caught: it rolls the booking back with it in createWithCost. + const item = await this.budget.linkBudgetItemToReservation(tripId, reservationId, { + name: title, + category: entry.category || type || 'Other', + total_price: entry.total_price!, + ...(entry.currency ? { currency: entry.currency } : {}), + ...(entry.exchange_rate != null ? { exchange_rate: entry.exchange_rate } : {}), + }); + this.sendCost(tripId, { event: 'budget:created', payload: { item } }, socketId, collect); } - /** PUT side effect: drop the linked budget item when the price is cleared, else create/update it. `collect` as on create. */ + /** PUT side effect: drop the linked budget item when the price is cleared, else create/update it. `collect` and failures as on create. */ async syncBudgetOnUpdate(tripId: string, id: string, title: string, type: string | undefined, currentTitle: string, currentType: string | undefined, entry: BudgetEntry, socketId: string | undefined, collect?: CostEvent[]): Promise { // When the booking type changes, keep a linked expense's category in sync — // but only if it still carries the auto-derived category (so a manual pick in @@ -1184,20 +1181,16 @@ export class ReservationsService { return; } - try { - const itemName = title || currentTitle; - const category = entry.category || type || currentType || 'Other'; - const existing = await this.budgetItemsRepo.findIdByReservationInTrip(tripId, id); - if (existing) { - const updated = await this.budget.updateBudgetItem(existing.id, tripId, { name: itemName, category, total_price: entry.total_price }); - this.sendCost(tripId, { event: 'budget:updated', payload: { item: updated } }, socketId, collect); - } else { - // The link travels on the insert, so a cost is never written without its booking. - const item = await this.budget.linkBudgetItemToReservation(tripId, Number(id), { name: itemName, category, total_price: entry.total_price! }); - this.sendCost(tripId, { event: 'budget:created', payload: { item } }, socketId, collect); - } - } catch (err) { - console.error('[reservations] Failed to create/update budget entry:', err); + const itemName = title || currentTitle; + const category = entry.category || type || currentType || 'Other'; + const existing = await this.budgetItemsRepo.findIdByReservationInTrip(tripId, id); + if (existing) { + const updated = await this.budget.updateBudgetItem(existing.id, tripId, { name: itemName, category, total_price: entry.total_price }); + this.sendCost(tripId, { event: 'budget:updated', payload: { item: updated } }, socketId, collect); + } else { + // The link travels on the insert, so a cost is never written without its booking. + const item = await this.budget.linkBudgetItemToReservation(tripId, Number(id), { name: itemName, category, total_price: entry.total_price! }); + this.sendCost(tripId, { event: 'budget:created', payload: { item } }, socketId, collect); } } } diff --git a/server/tests/unit/nest/reservations.service.test.ts b/server/tests/unit/nest/reservations.service.test.ts index ace4a4687..2017c1ab4 100644 --- a/server/tests/unit/nest/reservations.service.test.ts +++ b/server/tests/unit/nest/reservations.service.test.ts @@ -555,9 +555,12 @@ describe('ReservationsService (DI-native, real SQL)', () => { expect(broadcast).toHaveBeenCalledWith('5', 'budget:created', { item: { id: 7 } }, 'sock'); }); - it('falls back to type then "Other" for the category and swallows errors', async () => { + it('falls back to "Other" for the category and lets a failing cost write reach the caller', async () => { budget.linkBudgetItemToReservation.mockImplementation(() => { throw new Error('boom'); }); - expect(() => svc.syncBudgetOnCreate('5', 9, 'Hotel', undefined, { total_price: 50 }, 'sock')).not.toThrow(); + // createWithCost relies on the throw to roll the booking back with its cost. + await expect(svc.syncBudgetOnCreate('5', 9, 'Hotel', undefined, { total_price: 50 }, 'sock')).rejects.toThrow('boom'); + expect(budget.linkBudgetItemToReservation).toHaveBeenCalledWith('5', 9, { name: 'Hotel', category: 'Other', total_price: 50 }); + expect(broadcast).not.toHaveBeenCalled(); }); // #2525: an imported booking quoted in dollars became a cost of that many euros. @@ -721,6 +724,21 @@ describe('ReservationsService (DI-native, real SQL)', () => { expect(broadcast).not.toHaveBeenCalled(); }); + it('RESV-TX-006: updateWithCost rolls the booking edit back when the new price cannot be written', async () => { + const { trip, res, item } = linkedItem({ total_price: 100 }); + testDb.prepare("UPDATE reservations SET title = 'Old' WHERE id = ?").run(res.id); + const current = (await svc.getReservation(String(res.id), String(trip.id)))!; + budget.updateBudgetItem.mockRejectedValueOnce(new Error('boom')); + + // Same type, so only the price path writes, and that write throws. + await expect(svc.updateWithCost(String(res.id), String(trip.id), { title: 'New' } as never, current, { total_price: 250 })) + .rejects.toThrow('boom'); + + expect(testDb.prepare('SELECT title FROM reservations WHERE id = ?').get(res.id)).toEqual({ title: 'Old' }); + expect(testDb.prepare('SELECT total_price FROM budget_items WHERE id = ?').get(item.id)).toEqual({ total_price: 100 }); + expect(broadcast).not.toHaveBeenCalled(); + }); + it('RESV-TX-002: updateWithCost hands the cost events back instead of sending them inside the write', async () => { const { trip, res, item } = linkedItem(); const current = (await svc.getReservation(String(res.id), String(trip.id)))!; @@ -747,14 +765,15 @@ describe('ReservationsService (DI-native, real SQL)', () => { expect(broadcast).not.toHaveBeenCalledWith(String(trip.id), 'budget:created', expect.anything(), expect.anything()); }); - it('RESV-TX-004: a failing cost keeps the booking, as the REST route always has', async () => { + it('RESV-TX-004: a failing cost rolls the new booking back with it', async () => { const { trip } = ownerTrip(); budget.linkBudgetItemToReservation.mockRejectedValueOnce(new Error('boom')); - const out = await svc.createWithCost(String(trip.id), { title: 'Hotel', type: 'other' } as never, { total_price: 200 }); + await expect(svc.createWithCost(String(trip.id), { title: 'Hotel', type: 'other' } as never, { total_price: 200 })) + .rejects.toThrow('boom'); - expect(out.costEvents).toEqual([]); - expect(testDb.prepare('SELECT title FROM reservations WHERE id = ?').get(out.reservation.id)).toEqual({ title: 'Hotel' }); + expect(testDb.prepare('SELECT COUNT(*) AS n FROM reservations WHERE trip_id = ?').get(trip.id)).toEqual({ n: 0 }); + expect(broadcast).not.toHaveBeenCalled(); }); it('RESV-TX-005: without a price there is no cost and no event', async () => {