diff --git a/backend/src/routes/parents/parents.controller.ts b/backend/src/routes/parents/parents.controller.ts index a6ed455..edadf2b 100644 --- a/backend/src/routes/parents/parents.controller.ts +++ b/backend/src/routes/parents/parents.controller.ts @@ -1,7 +1,6 @@ import { Body, Controller, - Delete, Get, Param, Patch, @@ -9,21 +8,27 @@ import { 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 } from 'src/entities/users.entity'; -import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; +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) {} + constructor( + private readonly parentsService: ParentsService, + private readonly userService: UserService, + ) {} @Get('pending-families') @Roles(RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE) @@ -34,6 +39,29 @@ export class ParentsController { 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 { + 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' }) diff --git a/backend/src/routes/parents/parents.module.ts b/backend/src/routes/parents/parents.module.ts index dc57fe6..6cb557b 100644 --- a/backend/src/routes/parents/parents.module.ts +++ b/backend/src/routes/parents/parents.module.ts @@ -1,12 +1,16 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Parents } from 'src/entities/parents.entity'; import { ParentsController } from './parents.controller'; import { ParentsService } from './parents.service'; import { Users } from 'src/entities/users.entity'; +import { UserModule } from '../user/user.module'; @Module({ - imports: [TypeOrmModule.forFeature([Parents, Users])], + imports: [ + TypeOrmModule.forFeature([Parents, Users]), + forwardRef(() => UserModule), + ], controllers: [ParentsController], providers: [ParentsService], exports: [ParentsService, diff --git a/backend/src/routes/parents/parents.service.ts b/backend/src/routes/parents/parents.service.ts index e2c50e6..3aa174d 100644 --- a/backend/src/routes/parents/parents.service.ts +++ b/backend/src/routes/parents/parents.service.ts @@ -119,4 +119,49 @@ export class ParentsService { numero_dossier: r.numero_dossier ?? null, })); } + + /** + * Retourne les user_id de tous les parents de la même famille (co_parent ou enfants partagés). + * @throws NotFoundException si parentId n'est pas un parent + */ + async getFamilyUserIds(parentId: string): Promise { + const raw = await this.parentsRepository.query( + ` + WITH RECURSIVE + links AS ( + SELECT p.id_utilisateur AS p1, p.id_co_parent AS p2 FROM parents p WHERE p.id_co_parent IS NOT NULL + UNION ALL + SELECT p.id_co_parent AS p1, p.id_utilisateur AS p2 FROM parents p WHERE p.id_co_parent IS NOT NULL + UNION ALL + SELECT ep1.id_parent AS p1, ep2.id_parent AS p2 + FROM enfants_parents ep1 + JOIN enfants_parents ep2 ON ep2.id_enfant = ep1.id_enfant AND ep1.id_parent < ep2.id_parent + UNION ALL + SELECT ep2.id_parent AS p1, ep1.id_parent AS p2 + FROM enfants_parents ep1 + JOIN enfants_parents ep2 ON ep2.id_enfant = ep1.id_enfant AND ep1.id_parent < ep2.id_parent + ), + rec AS ( + SELECT id_utilisateur AS id, id_utilisateur AS rep FROM parents + UNION + SELECT l.p2 AS id, LEAST(rec_alias.rep, l.p2) AS rep FROM links l JOIN rec rec_alias ON rec_alias.id = l.p1 + ), + family_rep AS ( + SELECT id, (MIN(rep::text))::uuid AS rep FROM rec GROUP BY id + ), + input_rep AS ( + SELECT rep FROM family_rep WHERE id = $1::uuid LIMIT 1 + ) + SELECT fr.id::text AS id + FROM family_rep fr + CROSS JOIN input_rep ir + WHERE fr.rep = ir.rep + `, + [parentId], + ); + if (!raw || raw.length === 0) { + throw new NotFoundException('Parent introuvable ou pas encore enregistré en base.'); + } + return raw.map((r: { id: string }) => r.id); + } }