|
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + Body, |
| 4 | + ClassSerializerInterceptor, |
| 5 | + Controller, |
| 6 | + Get, |
| 7 | + Param, |
| 8 | + ParseIntPipe, |
| 9 | + Patch, |
| 10 | + UseGuards, |
| 11 | + UseInterceptors |
| 12 | +} from "@nestjs/common"; |
| 13 | +import { ApiTags } from "@nestjs/swagger"; |
| 14 | +import { UserRole } from "@prisma/client"; |
| 15 | + |
| 16 | +import { JwtAuthGuard } from "../auth/guards/jwt.guard"; |
| 17 | +import { Roles } from "../decorators/roles.decorator"; |
| 18 | +import { RolesGuard } from "../guards/roles.guard"; |
| 19 | +import { getErrorMessage } from "../utils/get-error-message"; |
| 20 | +import { ScheduleEntity, UpdateScheduleDto } from "./schedule.dto"; |
| 21 | +import { ScheduleService } from "./schedule.service"; |
| 22 | + |
| 23 | +@ApiTags("Schedule") |
| 24 | +@Controller("schedule") |
| 25 | +@UseInterceptors(ClassSerializerInterceptor) |
| 26 | +export class ScheduleController { |
| 27 | + constructor(private readonly scheduleService: ScheduleService) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * Get a schedule for a service with the specified id |
| 31 | + */ |
| 32 | + @Get() |
| 33 | + async findAll(@Param("serviceId", ParseIntPipe) serviceId: number) { |
| 34 | + const schedule = await this.scheduleService.findSchedule({ serviceId }); |
| 35 | + return schedule.map((s) => new ScheduleEntity(s)); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Update a schedule with the specified id |
| 40 | + */ |
| 41 | + @Patch(":id") |
| 42 | + @UseGuards(JwtAuthGuard, RolesGuard) |
| 43 | + @Roles(UserRole.ADMIN, UserRole.MANAGER) |
| 44 | + async update(@Param("id", ParseIntPipe) id: number, @Body() data: UpdateScheduleDto) { |
| 45 | + try { |
| 46 | + return new ScheduleEntity(await this.scheduleService.updateSchedule(id, data)); |
| 47 | + } catch (e) { |
| 48 | + console.error(getErrorMessage(e)); |
| 49 | + throw new BadRequestException("Failed to update schedule."); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments