feat(trips): transfer trip ownership to a member (#973)

Add POST /api/trips/:id/transfer so the owner can hand a trip to one of its
existing members. The swap runs in a transaction: the new owner takes
trips.user_id and the former owner is kept on as a regular member, so nobody
loses access. The endpoint is owner-only, writes a trip.transfer_ownership
audit entry and broadcasts the refreshed trip. The members modal gains a
"Make owner" action, shown only to the current owner.
This commit is contained in:
Maurice
2026-06-30 13:55:17 +02:00
committed by Maurice
parent fac2393388
commit 641711322e
32 changed files with 312 additions and 3 deletions
+35
View File
@@ -279,6 +279,41 @@ export class TripsController {
return { success: true };
}
@Post(':id/transfer')
transferOwnership(
@CurrentUser() user: User,
@Param('id') id: string,
@Body('newOwnerId') newOwnerId: unknown,
@Req() req: Request,
@Headers('x-socket-id') socketId?: string,
) {
const access = this.trips.canAccessTrip(id, user.id);
if (!access) {
throw new HttpException({ error: 'Trip not found' }, 404);
}
// Owner-only: handing over a trip is reserved for its actual owner, not just
// anyone who can manage members.
if (access.user_id !== user.id) {
throw new HttpException({ error: 'Only the owner can transfer ownership' }, 403);
}
if (typeof newOwnerId !== 'number') {
throw new HttpException({ error: 'newOwnerId is required' }, 400);
}
try {
const result = this.trips.transferOwnership(id, newOwnerId, user.id);
writeAudit({ userId: user.id, action: 'trip.transfer_ownership', ip: getClientIp(req), details: { tripId: Number(id), trip: result.tripTitle, from: result.fromEmail, to: result.toEmail } });
// Nudge everyone viewing the trip to re-read it so the new ownership and the
// recomputed permissions take effect live.
const updatedTrip = this.trips.get(id, user.id);
this.trips.broadcast(id, 'trip:updated', { trip: updatedTrip }, socketId);
return { success: true };
} catch (e: unknown) {
if (e instanceof NotFoundError) throw new HttpException({ error: e.message }, 404);
if (e instanceof ValidationError) throw new HttpException({ error: e.message }, 400);
throw e;
}
}
@Get(':id/bundle')
bundle(@CurrentUser() user: User, @Param('id') id: string) {
const trip = this.trips.get(id, user.id) as { user_id: number } | undefined;
+4
View File
@@ -95,6 +95,10 @@ export class TripsService {
tripSvc.removeMember(tripId, targetId);
}
transferOwnership(tripId: string, newOwnerId: number, currentOwnerId: number) {
return tripSvc.transferOwnership(tripId, newOwnerId, currentOwnerId);
}
exportICS(tripId: string) {
return tripSvc.exportICS(tripId);
}
+42
View File
@@ -403,6 +403,48 @@ export function removeMember(tripId: string | number, targetUserId: number) {
db.prepare('DELETE FROM trip_members WHERE trip_id = ? AND user_id = ?').run(tripId, targetUserId);
}
export interface TransferOwnershipResult {
tripTitle: string;
fromEmail: string;
toEmail: string;
}
/**
* Hand a trip over to one of its existing members (#973). The new owner must
* already be a member; afterwards they hold `trips.user_id` and the former owner
* becomes a regular member, so nobody loses access. Runs in a transaction so the
* owner pointer and the membership rows never diverge.
*/
export function transferOwnership(
tripId: string | number,
newOwnerId: number,
currentOwnerId: number,
): TransferOwnershipResult {
const trip = db.prepare('SELECT id, title, user_id FROM trips WHERE id = ?').get(tripId) as { id: number; title: string; user_id: number } | undefined;
if (!trip) throw new NotFoundError('Trip not found');
if (trip.user_id !== currentOwnerId) throw new ValidationError('Only the owner can transfer ownership');
if (newOwnerId === currentOwnerId) throw new ValidationError('You already own this trip');
const newOwner = db.prepare('SELECT id, email FROM users WHERE id = ?').get(newOwnerId) as { id: number; email: string } | undefined;
if (!newOwner) throw new NotFoundError('User not found');
const isMember = db.prepare('SELECT id FROM trip_members WHERE trip_id = ? AND user_id = ?').get(tripId, newOwnerId);
if (!isMember) throw new ValidationError('New owner must be a trip member');
const fromEmail = (db.prepare('SELECT email FROM users WHERE id = ?').get(currentOwnerId) as { email: string } | undefined)?.email || '';
const run = db.transaction(() => {
db.prepare('UPDATE trips SET user_id = ? WHERE id = ?').run(newOwnerId, tripId);
// The new owner is no longer a plain member…
db.prepare('DELETE FROM trip_members WHERE trip_id = ? AND user_id = ?').run(tripId, newOwnerId);
// …and the former owner keeps access as a member.
db.prepare('INSERT OR IGNORE INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(tripId, currentOwnerId, newOwnerId);
});
run();
return { tripTitle: trip.title, fromEmail, toEmail: newOwner.email };
}
// ── ICS export ────────────────────────────────────────────────────────────
// RFC 5545 §3.1: content lines longer than 75 octets must be folded with a CRLF