Files
travel-planner/backend/apps/api/src/trips/trip-preference-overrides.controller.ts

60 lines
1.8 KiB
TypeScript

import {
Body,
Controller,
Delete,
ForbiddenException,
Get,
Param,
Put,
UseGuards,
} from '@nestjs/common';
import { OidcAuthGuard } from '../../../../libs/auth/src';
import type { AuthenticatedUser } from '../../../../libs/auth/src';
import {
TripMembershipGuard,
TripPreferenceOverridesService,
} from '../../../../libs/trips/src';
import type {
TripMemberRole,
TripPreferenceOverride,
UpsertPreferenceOverrideDto,
} from '../../../../libs/trips/src';
import { CurrentUser } from '../auth/current-user.decorator';
import { CurrentTripRole } from './current-trip-role.decorator';
@Controller('trips/:tripId/preference-overrides')
@UseGuards(OidcAuthGuard, TripMembershipGuard)
export class TripPreferenceOverridesController {
constructor(private readonly service: TripPreferenceOverridesService) {}
@Get()
list(@Param('tripId') tripId: string): Promise<TripPreferenceOverride[]> {
return this.service.listOverrides(tripId);
}
@Put()
async upsert(
@Param('tripId') tripId: string,
@CurrentUser() currentUser: AuthenticatedUser,
@CurrentTripRole() role: TripMemberRole,
@Body() dto: UpsertPreferenceOverrideDto,
): Promise<TripPreferenceOverride> {
const isOwnUserOverride =
dto.subject.type === 'USER' && dto.subject.userId === currentUser.id;
if (role !== 'OWNER' && !isOwnUserOverride) {
throw new ForbiddenException(
'Only the trip owner may set preference overrides for other members or travelers',
);
}
return this.service.upsertOverride(tripId, dto.subject, dto.overrides);
}
@Delete(':overrideId')
remove(
@Param('tripId') tripId: string,
@Param('overrideId') overrideId: string,
): Promise<void> {
return this.service.removeOverride(tripId, overrideId);
}
}