crm-system/src/repair-orders/repair-orders.controller.ts

108 lines
3.3 KiB
TypeScript

import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
ParseUUIDPipe,
NotFoundException,
ConflictException,
UseGuards,
} from '@nestjs/common';
import { RepairOrdersService, type CreateRepairOrderDto, type UpdateRepairOrderDto } from './repair-orders.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RbacGuard } from '../auth/rbac.guard';
import { Roles } from '../auth/rbac.decorator';
import { CurrentUser } from '../auth/get-user.decorator';
import { Role } from '@prisma/client';
@Controller('repair-orders')
@UseGuards(JwtAuthGuard, RbacGuard)
export class RepairOrdersController {
constructor(private readonly repairOrdersService: RepairOrdersService) {}
@Post()
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC, Role.WORKER)
async create(@Body() createRepairOrderDto: CreateRepairOrderDto, @CurrentUser() user: { role: Role }) {
return this.repairOrdersService.create(createRepairOrderDto, user.role);
}
@Get()
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC, Role.WORKER)
findAll() {
return this.repairOrdersService.findAll();
}
@Get(':id')
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC, Role.WORKER)
findOne(@Param('id', ParseUUIDPipe) id: string) {
try {
return this.repairOrdersService.findOne(id);
} catch (e) {
if (e instanceof NotFoundException) throw e;
throw new NotFoundException('RepairOrder not found');
}
}
@Patch(':id')
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC)
update(
@Param('id', ParseUUIDPipe) id: string,
@Body() updateRepairOrderDto: UpdateRepairOrderDto,
@CurrentUser() user: { role: Role },
) {
try {
return this.repairOrdersService.update(id, updateRepairOrderDto, user.role);
} catch (e) {
if (e instanceof NotFoundException || e instanceof ConflictException) throw e;
throw new NotFoundException('RepairOrder not found');
}
}
@Delete(':id')
@Roles(Role.ADMIN)
remove(@Param('id', ParseUUIDPipe) id: string) {
try {
return this.repairOrdersService.remove(id);
} catch (e) {
if (e instanceof NotFoundException) throw e;
throw new NotFoundException('RepairOrder not found');
}
}
// Assignees
@Post(':id/assignees/:userId')
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC)
async addAssignee(
@Param('id', ParseUUIDPipe) id: string,
@Param('userId', ParseUUIDPipe) userId: string,
@CurrentUser() user: { role: Role },
) {
try {
await this.repairOrdersService.addAssignee(id, userId, user.role);
return { success: true };
} catch (e) {
if (e instanceof NotFoundException || e instanceof ConflictException) throw e;
throw new NotFoundException('RepairOrder or User not found');
}
}
@Delete(':id/assignees/:userId')
@Roles(Role.ADMIN, Role.DIRECTOR, Role.MECHANIC)
async removeAssignee(
@Param('id', ParseUUIDPipe) id: string,
@Param('userId', ParseUUIDPipe) userId: string,
@CurrentUser() user: { role: Role },
) {
try {
await this.repairOrdersService.removeAssignee(id, userId, user.role);
return { success: true };
} catch (e) {
if (e instanceof NotFoundException || e instanceof ConflictException) throw e;
throw new NotFoundException('RepairOrder or User not found');
}
}
}