169 lines
7.7 KiB
TypeScript
169 lines
7.7 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
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 { UpdateParentFicheAdminDto } from './dto/update-parent-fiche-admin.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';
|
|
import { DossierFamilleCompletDto } from './dto/dossier-famille-complet.dto';
|
|
import { mapParentForApi, mapParentsForApi } from './parents.mapper';
|
|
|
|
@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, date_soumission, nombre_enfants, emails, parents)',
|
|
type: [PendingFamilyDto],
|
|
})
|
|
@ApiResponse({ status: 403, description: 'Accès refusé' })
|
|
getPendingFamilies(): Promise<PendingFamilyDto[]> {
|
|
return this.parentsService.getPendingFamilies();
|
|
}
|
|
|
|
@Get('dossier-famille/:numeroDossier')
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE)
|
|
@ApiOperation({ summary: 'Dossier famille complet par numéro de dossier (Ticket #119)' })
|
|
@ApiParam({ name: 'numeroDossier', description: 'Numéro de dossier (ex: 2026-000001)' })
|
|
@ApiResponse({ status: 200, description: 'Dossier famille (numero_dossier, parents, enfants, presentation)', type: DossierFamilleCompletDto })
|
|
@ApiResponse({ status: 404, description: 'Aucun dossier pour ce numéro' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé' })
|
|
getDossierFamille(@Param('numeroDossier') numeroDossier: string): Promise<DossierFamilleCompletDto> {
|
|
return this.parentsService.getDossierFamilleByNumero(numeroDossier);
|
|
}
|
|
|
|
@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;
|
|
}
|
|
|
|
@Get()
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@ApiOperation({ summary: 'Liste des parents (user, co_parent, parentChildren) — ticket #131' })
|
|
@ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
|
|
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
|
async getAll(): Promise<Parents[]> {
|
|
const parents = await this.parentsService.findAll();
|
|
return mapParentsForApi(parents);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@Get(':id')
|
|
@ApiOperation({ summary: 'Détail parent par user_id (inclut co_parent si id_co_parent renseigné) — ticket #131' })
|
|
@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é !' })
|
|
async getOne(@Param('id') user_id: string): Promise<Parents> {
|
|
const parent = await this.parentsService.findOne(user_id);
|
|
return mapParentForApi(parent);
|
|
}
|
|
|
|
@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é !' })
|
|
async create(@Body() dto: CreateParentDto): Promise<Parents> {
|
|
const parent = await this.parentsService.create(dto);
|
|
return mapParentForApi(parent);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@Patch(':id/fiche')
|
|
@ApiOperation({ summary: 'Mettre à jour la fiche parent (admin/gestionnaire) — ticket #131' })
|
|
@ApiParam({ name: 'id', description: "UUID utilisateur du parent" })
|
|
@ApiBody({ type: UpdateParentFicheAdminDto })
|
|
@ApiResponse({ status: 200, type: Parents, description: 'Fiche parent mise à jour' })
|
|
async updateFicheAdmin(
|
|
@Param('id') id: string,
|
|
@Body() dto: UpdateParentFicheAdminDto,
|
|
): Promise<Parents> {
|
|
const parent = await this.parentsService.updateFicheAdmin(id, dto);
|
|
return mapParentForApi(parent);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@Post(':id/enfants/:enfantId')
|
|
@ApiOperation({ summary: 'Rattacher un enfant à un parent — ticket #115' })
|
|
@ApiParam({ name: 'id', description: "UUID utilisateur du parent" })
|
|
@ApiParam({ name: 'enfantId', description: "UUID de l'enfant" })
|
|
@ApiResponse({ status: 200, type: Parents, description: 'Parent avec enfants mis à jour' })
|
|
async attachEnfant(
|
|
@Param('id') id: string,
|
|
@Param('enfantId') enfantId: string,
|
|
): Promise<Parents> {
|
|
const parent = await this.parentsService.attachEnfant(id, enfantId);
|
|
return mapParentForApi(parent);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
|
@Delete(':id/enfants/:enfantId')
|
|
@ApiOperation({ summary: "Détacher un enfant d'un parent — ticket #115" })
|
|
@ApiParam({ name: 'id', description: "UUID utilisateur du parent" })
|
|
@ApiParam({ name: 'enfantId', description: "UUID de l'enfant" })
|
|
@ApiResponse({ status: 200, type: Parents, description: 'Parent avec enfants mis à jour' })
|
|
async detachEnfant(
|
|
@Param('id') id: string,
|
|
@Param('enfantId') enfantId: string,
|
|
): Promise<Parents> {
|
|
const parent = await this.parentsService.detachEnfant(id, enfantId);
|
|
return mapParentForApi(parent);
|
|
}
|
|
|
|
@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é !' })
|
|
async update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> {
|
|
const parent = await this.parentsService.update(id, dto);
|
|
return mapParentForApi(parent);
|
|
}
|
|
}
|