101 lines
4.3 KiB
TypeScript
101 lines
4.3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { ParentsService } from './parents.service';
|
|
import { UserService } from '../user/user.service';
|
|
import { Parents } from 'src/entities/parents.entity';
|
|
import { Users } from 'src/entities/users.entity';
|
|
import { Roles } from 'src/common/decorators/roles.decorator';
|
|
import { RoleType, StatutUtilisateurType } from 'src/entities/users.entity';
|
|
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
import { CreateParentDto } from '../user/dto/create_parent.dto';
|
|
import { UpdateParentsDto } from '../user/dto/update_parent.dto';
|
|
import { AuthGuard } from 'src/common/guards/auth.guard';
|
|
import { RolesGuard } from 'src/common/guards/roles.guard';
|
|
import { User } from 'src/common/decorators/user.decorator';
|
|
import { PendingFamilyDto } from './dto/pending-family.dto';
|
|
|
|
@ApiTags('Parents')
|
|
@Controller('parents')
|
|
@UseGuards(AuthGuard, RolesGuard)
|
|
export class ParentsController {
|
|
constructor(
|
|
private readonly parentsService: ParentsService,
|
|
private readonly userService: UserService,
|
|
) {}
|
|
|
|
@Get('pending-families')
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE)
|
|
@ApiOperation({ summary: 'Liste des familles en attente (une entrée par famille)' })
|
|
@ApiResponse({ status: 200, description: 'Liste des familles (libellé, parentIds, numero_dossier)', type: [PendingFamilyDto] })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé' })
|
|
getPendingFamilies(): Promise<PendingFamilyDto[]> {
|
|
return this.parentsService.getPendingFamilies();
|
|
}
|
|
|
|
@Post(':parentId/valider-dossier')
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE)
|
|
@ApiOperation({ summary: 'Valider tout le dossier famille (les 2 parents en une fois)' })
|
|
@ApiParam({ name: 'parentId', description: "UUID d'un des parents (user_id)" })
|
|
@ApiResponse({ status: 200, description: 'Utilisateurs validés (famille)' })
|
|
@ApiResponse({ status: 404, description: 'Parent introuvable' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé' })
|
|
async validerDossierFamille(
|
|
@Param('parentId') parentId: string,
|
|
@User() currentUser: Users,
|
|
@Body('comment') comment?: string,
|
|
): Promise<Users[]> {
|
|
const familyIds = await this.parentsService.getFamilyUserIds(parentId);
|
|
const validated: Users[] = [];
|
|
for (const userId of familyIds) {
|
|
const user = await this.userService.findOne(userId);
|
|
if (user.statut !== StatutUtilisateurType.EN_ATTENTE && user.statut !== StatutUtilisateurType.REFUSE) continue;
|
|
const saved = await this.userService.validateUser(userId, currentUser, comment);
|
|
validated.push(saved);
|
|
}
|
|
return validated;
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@Get()
|
|
@ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
|
getAll(): Promise<Parents[]> {
|
|
return this.parentsService.findAll();
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
|
@Get(':id')
|
|
@ApiResponse({ status: 200, type: Parents, description: 'Détails du parent par ID utilisateur' })
|
|
@ApiResponse({ status: 404, description: 'Parent non trouvé' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
|
getOne(@Param('id') user_id: string): Promise<Parents> {
|
|
return this.parentsService.findOne(user_id);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
|
@Post()
|
|
@ApiBody({ type: CreateParentDto })
|
|
@ApiResponse({ status: 201, type: Parents, description: 'Parent créé avec succès' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
|
create(@Body() dto: CreateParentDto): Promise<Parents> {
|
|
return this.parentsService.create(dto);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
|
@Patch(':id')
|
|
@ApiBody({ type: UpdateParentsDto })
|
|
@ApiResponse({ status: 200, type: Parents, description: 'Parent mis à jour avec succès' })
|
|
@ApiResponse({ status: 404, description: 'Parent introuvable' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
|
update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> {
|
|
return this.parentsService.update(id, dto);
|
|
}
|
|
}
|