Files
backend/src/profile-fields/profile-sections.controller.ts

137 lines
4.5 KiB
TypeScript

import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
UseGuards,
ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { ProfileSectionsService } from './profile-sections.service';
import { CreateSectionDto, UpdateSectionDto, AssignSectionDto, UpdateAssignmentDto } from './dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole } from '@prisma/client';
@ApiTags('Profile Sections')
@Controller('profile-sections')
export class ProfileSectionsController {
constructor(private readonly sectionsService: ProfileSectionsService) {}
@Post()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Create a new profile section (Admin only)' })
create(@Body() createSectionDto: CreateSectionDto) {
return this.sectionsService.create(createSectionDto);
}
@Get()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all profile sections (Admin only)' })
@ApiQuery({ name: 'includeInactive', required: false, type: Boolean })
findAll(@Query('includeInactive') includeInactive?: string) {
return this.sectionsService.findAll(includeInactive === 'true');
}
@Get(':id')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get a profile section by ID with fields (Admin only)' })
findOne(@Param('id', ParseUUIDPipe) id: string) {
return this.sectionsService.findOne(id);
}
@Patch(':id')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a profile section (Admin only)' })
update(
@Param('id', ParseUUIDPipe) id: string,
@Body() updateSectionDto: UpdateSectionDto,
) {
return this.sectionsService.update(id, updateSectionDto);
}
@Delete(':id')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete a profile section (Admin only)' })
remove(@Param('id', ParseUUIDPipe) id: string) {
return this.sectionsService.remove(id);
}
@Post(':id/assign')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Assign section to an agent type (Admin only)' })
assignToAgentType(
@Param('id', ParseUUIDPipe) id: string,
@Body() assignDto: AssignSectionDto,
) {
return this.sectionsService.assignToAgentType(id, assignDto);
}
@Patch(':id/assignment/:agentTypeId')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update section assignment (Admin only)' })
updateAssignment(
@Param('id', ParseUUIDPipe) id: string,
@Param('agentTypeId', ParseUUIDPipe) agentTypeId: string,
@Body() updateDto: UpdateAssignmentDto,
) {
return this.sectionsService.updateAssignment(id, agentTypeId, updateDto);
}
@Delete(':id/assignment/:agentTypeId')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Remove section from an agent type (Admin only)' })
removeFromAgentType(
@Param('id', ParseUUIDPipe) id: string,
@Param('agentTypeId', ParseUUIDPipe) agentTypeId: string,
) {
return this.sectionsService.removeFromAgentType(id, agentTypeId);
}
@Get('agent-type/:agentTypeId')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN, UserRole.AGENT)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all sections for an agent type with ordering' })
@ApiQuery({ name: 'includeFields', required: false, type: Boolean })
getSectionsForAgentType(
@Param('agentTypeId', ParseUUIDPipe) agentTypeId: string,
@Query('includeFields') includeFields?: string,
) {
return this.sectionsService.getSectionsForAgentType(agentTypeId, includeFields !== 'false');
}
@Patch('agent-type/:agentTypeId/order')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update section ordering for an agent type (Admin only)' })
updateSectionOrderForAgentType(
@Param('agentTypeId', ParseUUIDPipe) agentTypeId: string,
@Body() body: { sectionOrders: { sectionId: string; sortOrder: number; isRequired?: boolean }[] },
) {
return this.sectionsService.updateSectionOrderForAgentType(agentTypeId, body.sectionOrders);
}
}