API staff pour foyer mono-parent : compte actif, liens + enfants, mail création MDP. Mini-specs front/back dans docs/tmp. Co-authored-by: Cursor <cursoragent@cursor.com>
242 lines
11 KiB
TypeScript
242 lines
11 KiB
TypeScript
import {
|
||
Body,
|
||
Controller,
|
||
Delete,
|
||
Get,
|
||
HttpCode,
|
||
HttpStatus,
|
||
Param,
|
||
Patch,
|
||
Post,
|
||
UseGuards,
|
||
} from '@nestjs/common';
|
||
import { ParentsService } from './parents.service';
|
||
import { UserService } from '../user/user.service';
|
||
import { AuthService } from '../auth/auth.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 {
|
||
ApiBearerAuth,
|
||
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 { StaffCreateParentDossierDto } from './dto/staff-create-parent-dossier.dto';
|
||
import { StaffCreateParentDossierResponseDto } from './dto/staff-create-parent-dossier-response.dto';
|
||
import { StaffAddCoParentDto } from './dto/staff-add-co-parent.dto';
|
||
import { StaffAddCoParentResponseDto } from './dto/staff-add-co-parent-response.dto';
|
||
import { RegisterParentCompletDto } from '../auth/dto/register-parent-complet.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')
|
||
@ApiBearerAuth('access-token')
|
||
@Controller('parents')
|
||
@UseGuards(AuthGuard, RolesGuard)
|
||
export class ParentsController {
|
||
constructor(
|
||
private readonly parentsService: ParentsService,
|
||
private readonly userService: UserService,
|
||
private readonly authService: AuthService,
|
||
) {}
|
||
|
||
@Roles(RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE)
|
||
@Post('dossier')
|
||
@HttpCode(HttpStatus.CREATED)
|
||
@ApiOperation({
|
||
summary: 'Créer un dossier famille/parent complet (staff) — ticket #129',
|
||
description:
|
||
'Crée parent (+ co-parent optionnel) + enfants + n° dossier avec statut actif, ' +
|
||
'et envoie l’e-mail de création de mot de passe. ' +
|
||
'Ne pas utiliser POST /auth/register/parent depuis le dashboard.',
|
||
})
|
||
@ApiBody({ type: StaffCreateParentDossierDto })
|
||
@ApiResponse({ status: 201, type: StaffCreateParentDossierResponseDto })
|
||
@ApiResponse({ status: 400, description: 'Validation DTO / métier' })
|
||
@ApiResponse({ status: 403, description: 'Rôle non autorisé' })
|
||
@ApiResponse({ status: 409, description: 'Email pivot et/ou co-parent déjà pris' })
|
||
async createDossier(
|
||
@Body() dto: StaffCreateParentDossierDto,
|
||
): Promise<StaffCreateParentDossierResponseDto> {
|
||
const registerDto = {
|
||
...dto,
|
||
acceptation_cgu: true,
|
||
acceptation_privacy: true,
|
||
} as RegisterParentCompletDto;
|
||
const result = await this.authService.createParentDossierStaff(registerDto);
|
||
return {
|
||
message: result.message,
|
||
numero_dossier: result.numero_dossier,
|
||
parent_user_id: result.parent_user_id,
|
||
co_parent_user_id: result.co_parent_user_id ?? null,
|
||
statut: result.statut,
|
||
enfant_ids: result.enfant_ids,
|
||
};
|
||
}
|
||
|
||
@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.ADMINISTRATEUR, RoleType.GESTIONNAIRE)
|
||
@Post(':id/co-parent')
|
||
@HttpCode(HttpStatus.CREATED)
|
||
@ApiOperation({
|
||
summary: 'Ajouter un co-parent à un foyer existant (staff) — ticket #135',
|
||
description:
|
||
'Foyer mono-parent uniquement. Crée le co-parent actif, liens foyer + enfants, ' +
|
||
'e-mail de création de mot de passe. Ne pas utiliser POST /auth/register/parent.',
|
||
})
|
||
@ApiParam({ name: 'id', description: 'UUID utilisateur du parent pivot' })
|
||
@ApiBody({ type: StaffAddCoParentDto })
|
||
@ApiResponse({ status: 201, type: StaffAddCoParentResponseDto })
|
||
@ApiResponse({ status: 400, description: 'Foyer déjà à 2 parents / validation' })
|
||
@ApiResponse({ status: 404, description: 'Parent introuvable' })
|
||
@ApiResponse({ status: 409, description: 'Email déjà pris' })
|
||
async addCoParent(
|
||
@Param('id') id: string,
|
||
@Body() dto: StaffAddCoParentDto,
|
||
): Promise<StaffAddCoParentResponseDto> {
|
||
return this.authService.addCoParentStaff(id, dto);
|
||
}
|
||
|
||
@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);
|
||
}
|
||
}
|