fix(trips): deleting a guest keeps the costs adding up instead of dropping their payments

A guest's payer rows and recorded payments hung on the users row by ON DELETE
CASCADE, so deleting the guest wiped money that had really moved: expenses
they paid were left without a payer, settled payments reopened, and a custom
or receipt split lost the guest's share so the balances no longer summed to
zero.

Deleting a guest who paid for an expense or is part of a recorded payment is
now refused with a 409 (REST and the delete_trip_guest MCP tool, same text),
and the share dialog says to change the payer or delete the payment first.
Otherwise the guest's share is handed on: an equal split re-splits as before,
a custom amount goes to the others by largest remainder in the currency's
smallest step (members without an amount get one, so the expense stays a
custom split), and a receipt drops the guest from its lines and is split
again like the cost editor does. A legacy paid_by_user_id is cleared as on
account deletion. Account deletion goes through the same re-split. The
confirmation text in all languages and the wiki say what really happens.

Refs #2604
This commit is contained in:
Maurice
2026-10-12 16:39:17 +02:00
parent 2149fd5315
commit 1b58acc78f
44 changed files with 663 additions and 44 deletions
+17
View File
@@ -89,3 +89,20 @@ export const COLLAB_POLL_OPTIONS: JsonColumn<unknown[]> = {
schema: z.array(z.unknown()),
fallback: () => [],
};
/**
* An expense's itemized receipt (#1658): each line's price as typed and the user
* ids in on it. The client writes it; the server only reads it to take a departed
* guest off the lines, so every key it does not know is kept as it is.
*/
export const BUDGET_ITEM_TICKET: JsonColumn<{ items: { price?: string | number; parts?: number[] }[] } | null> = {
column: 'budget_items.ticket_json',
schema: z
.object({
items: z.array(
z.looseObject({ price: z.union([z.string(), z.number()]).optional(), parts: z.array(z.number()).optional() }),
),
})
.loose(),
fallback: () => null,
};
@@ -196,6 +196,16 @@ export class BudgetItemMembersRepository extends TrekRepository<BudgetItemMember
.execute();
}
/** `UPDATE budget_item_members SET amount = ? WHERE budget_item_id = ? AND user_id = ?`. */
async setAmount(budget_item_id: number, user_id: number, amount: number | null): Promise<void> {
await this.kysely<BudgetItemMembersKyselyDB>()
.updateTable('budget_item_members')
.set({ amount })
.where('budget_item_id', '=', budget_item_id)
.where('user_id', '=', user_id)
.execute();
}
/** BG35/BG38/BG59 — `DELETE FROM budget_item_members WHERE budget_item_id = ?` (replace-all before a re-insert). */
async deleteForItem(budget_item_id: number): Promise<void> {
await this.kysely<BudgetItemMembersKyselyDB>()
@@ -94,6 +94,17 @@ export class BudgetItemPayersRepository extends TrekRepository<BudgetItemPayers>
.execute();
}
/** How many expenses a user paid any part of, a refund's negative share included (#2176): `SELECT COUNT(*) FROM budget_item_payers WHERE user_id = ? AND amount <> 0`. */
async countNonZeroForUser(user_id: number): Promise<number> {
const row = await this.kysely<BudgetItemPayersKyselyDB>()
.selectFrom('budget_item_payers')
.select((eb) => eb.fn.countAll<number>().as('count'))
.where('user_id', '=', user_id)
.where('amount', '<>', 0)
.executeTakeFirst();
return Number(row?.count ?? 0);
}
/** BG4 — `DELETE FROM budget_item_payers WHERE budget_item_id = ?` (replace-all before a re-insert). */
async deleteForItem(budget_item_id: number): Promise<void> {
await this.kysely<BudgetItemPayersKyselyDB>()
@@ -306,6 +306,11 @@ export class BudgetItemsRepository extends TrekRepository<BudgetItems> {
.execute();
}
/** `UPDATE budget_items SET ticket_json = ? WHERE id = ?`. */
async setTicketJson(id: number, ticket_json: string | null): Promise<void> {
await this.kysely<BudgetKyselyDB>().updateTable('budget_items').set({ ticket_json }).where('id', '=', id).execute();
}
/** BG83 (`reorderItems`, looped) — `UPDATE budget_items SET sort_order = ? WHERE id = ? AND trip_id = ?`. */
async setSortOrder(id: number | string, trip_id: number | string, sort_order: number): Promise<void> {
await this.kysely<BudgetKyselyDB>()
@@ -179,6 +179,16 @@ export class BudgetSettlementsRepository extends TrekRepository<BudgetSettlement
await this.kysely<BudgetSettlementsKyselyDB>().deleteFrom('budget_settlements').where('id', '=', id).execute();
}
/** How many recorded payments a user sent or received: `SELECT COUNT(*) FROM budget_settlements WHERE from_user_id = ? OR to_user_id = ?`. */
async countForUser(user_id: number): Promise<number> {
const row = await this.kysely<BudgetSettlementsKyselyDB>()
.selectFrom('budget_settlements')
.select((eb) => eb.fn.countAll<number>().as('count'))
.where((eb) => eb.or([eb('from_user_id', '=', user_id), eb('to_user_id', '=', user_id)]))
.executeTakeFirst();
return Number(row?.count ?? 0);
}
/**
* `UPDATE budget_settlements SET created_by_user_id = NULL WHERE
* created_by_user_id = ?`. The column has no ON DELETE action, so deleting a