Compare commits

..

No commits in common. "master" and "back/enfants-crud-01" have entirely different histories.

18 changed files with 238 additions and 285 deletions

View File

@ -13,7 +13,6 @@ import { ParentsModule } from './routes/parents/parents.module';
import { AuthModule } from './routes/auth/auth.module';
import { SentryGlobalFilter } from '@sentry/nestjs/setup';
import { AllExceptionsFilter } from './common/filters/all_exceptions.filters';
import { EnfantsModule } from './routes/enfants/enfants.module';
@Module({
imports: [
@ -47,7 +46,6 @@ import { EnfantsModule } from './routes/enfants/enfants.module';
}),
UserModule,
ParentsModule,
EnfantsModule,
AuthModule,
],
controllers: [AppController],

View File

@ -21,11 +21,8 @@ export class AuthGuard implements CanActivate {
if (isPublic) return true;
const request = context.switchToHttp().getRequest<Request>();
if (request.path.startsWith('/api-docs')) {
return true;
}
const authHeader = request.headers['authorization'] as string | undefined;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
throw new UnauthorizedException('Token manquant ou invalide');
}
@ -33,14 +30,9 @@ export class AuthGuard implements CanActivate {
const token = authHeader.split(' ')[1];
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: this.configService.get<string>('jwt.accessSecret'),
secret: this.configService.get<string>('jwt.accessSecret'), // ✅ corrige ici
});
request.user = {
...payload,
id: payload.sub,
};
request.user = payload;
return true;
} catch (error) {
throw new UnauthorizedException('Token invalide ou expiré');

View File

@ -41,11 +41,10 @@ async function bootstrap() {
},
'access-token',
)
//.addServer('/api/v1')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api/v1/swagger', app, document);
SwaggerModule.setup('api-docs', app, document);

View File

@ -66,16 +66,4 @@ export class AssistantesMaternellesController {
update(@Param('id') id: string, @Body() dto: UpdateAssistanteDto): Promise<AssistanteMaternelle> {
return this.assistantesMaternellesService.update(id, dto);
}
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
@ApiOperation({ summary: 'Supprimer une nounou' })
@ApiResponse({ status: 200, description: 'Nounou supprimée avec succès' })
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins, gestionnaires et administrateurs' })
@ApiResponse({ status: 404, description: 'Nounou non trouvée' })
@ApiParam({ name: 'id', description: "UUID de la nounou" })
@Delete(':id')
remove(@Param('id') id: string): Promise<{ message: string }>
{
return this.assistantesMaternellesService.remove(id);
}
}

View File

@ -1,20 +1,9 @@
import { Module } from '@nestjs/common';
import { AssistantesMaternellesService } from './assistantes_maternelles.service';
import { AssistantesMaternellesController } from './assistantes_maternelles.controller';
import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Users } from 'src/entities/users.entity';
import { AuthModule } from '../auth/auth.module';
@Module({
imports: [TypeOrmModule.forFeature([AssistanteMaternelle, Users]),
AuthModule
],
controllers: [AssistantesMaternellesController],
providers: [AssistantesMaternellesService],
exports: [
AssistantesMaternellesService,
TypeOrmModule,
],
})
export class AssistantesMaternellesModule { }
export class AssistantesMaternellesModule {}

View File

@ -71,10 +71,4 @@ export class AssistantesMaternellesService {
await this.assistantesMaternelleRepository.update(id, dto);
return this.findOne(id);
}
// Suppression dune assistante maternelle
async remove(id: string): Promise<{ message: string }> {
await this.assistantesMaternelleRepository.delete(id);
return { message: 'Assistante maternelle supprimée' };
}
}

View File

@ -9,8 +9,6 @@ import type { Request } from 'express';
import { UserService } from '../user/user.service';
import { ProfileResponseDto } from './dto/profile_response.dto';
import { RefreshTokenDto } from './dto/refresh_token.dto';
import { User } from 'src/common/decorators/user.decorator';
import { Users } from 'src/entities/users.entity';
@ApiTags('Authentification')
@Controller('auth')
@ -64,12 +62,5 @@ export class AuthController {
statut: user.statut,
};
}
@UseGuards(AuthGuard)
@ApiBearerAuth('access-token')
@Post('logout')
logout(@User() currentUser: Users) {
return this.authService.logout(currentUser.id);
}
}

View File

@ -10,6 +10,7 @@ import { RegisterDto } from './dto/register.dto';
import { ConfigService } from '@nestjs/config';
import { RoleType, StatutUtilisateurType, Users } from 'src/entities/users.entity';
import { LoginDto } from './dto/login.dto';
import { DeepPartial } from 'typeorm';
@Injectable()
export class AuthService {
@ -130,8 +131,6 @@ export class AuthService {
}
async logout(userId: string) {
// Pour le moment envoyer un message clair
return { success: true, message: 'Deconnexion'}
// Pour une implémentation simple, on ne fait rien ici.
}
}

View File

@ -1,15 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsBoolean,
IsDateString,
IsEnum,
IsNotEmpty,
IsOptional,
IsString,
MaxLength,
ValidateIf,
} from 'class-validator';
import { GenreType, StatutEnfantType } from 'src/entities/children.entity';
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsDateString, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength, ValidateIf } from "class-validator";
import { GenreType, StatutEnfantType } from "src/entities/children.entity";
export class CreateEnfantsDto {
@ApiProperty({ enum: StatutEnfantType, example: StatutEnfantType.ACTIF })
@ -23,7 +14,7 @@ export class CreateEnfantsDto {
@MaxLength(100)
first_name?: string;
@ApiProperty({ example: 'Dupont', required: false })
@ApiProperty({ example: 'Lucas', required: false })
@IsOptional()
@IsString()
@MaxLength(100)
@ -40,7 +31,7 @@ export class CreateEnfantsDto {
@IsDateString()
birth_date?: string;
@ApiProperty({ example: '2025-12-15', required: false })
@ApiProperty({ example: '2024-12-24', required: false })
@ValidateIf(o => o.status === StatutEnfantType.A_NAITRE)
@IsOptional()
@IsDateString()
@ -63,4 +54,10 @@ export class CreateEnfantsDto {
@ApiProperty({ default: false })
@IsBoolean()
is_multiple: boolean;
@ApiProperty({ example: 'UUID-parent' })
@IsUUID()
@IsNotEmpty()
id_parent: string;
}

View File

@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { GenreType, StatutEnfantType } from 'src/entities/children.entity';
import { ApiProperty } from "@nestjs/swagger";
import { GenreType, StatutEnfantType } from "src/entities/children.entity";
export class EnfantResponseDto {
@ApiProperty({ example: 'UUID-enfant' })
@ -20,7 +20,7 @@ export class EnfantResponseDto {
@ApiProperty({ example: '2018-06-24', required: false })
birth_date?: string;
@ApiProperty({ example: '2025-12-15', required: false })
@ApiProperty({ example: '2024-12-24', required: false })
due_date?: string;
@ApiProperty({ example: 'https://monimage.com/photo.jpg', required: false })
@ -34,4 +34,5 @@ export class EnfantResponseDto {
@ApiProperty({ example: 'UUID-parent' })
parent_id: string;
}

View File

@ -1,4 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateEnfantsDto } from './create_enfants.dto';
import { PartialType } from "@nestjs/swagger";
import { CreateEnfantsDto } from "./create_enfants.dto";
export class UpdateEnfantsDto extends PartialType(CreateEnfantsDto) {}

View File

@ -1,70 +1,77 @@
import {
Body,
Controller,
Delete,
Get,
Param,
ParseUUIDPipe,
Patch,
Post,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { EnfantsService } from './enfants.service';
import { CreateEnfantsDto } from './dto/create_enfants.dto';
import { UpdateEnfantsDto } from './dto/update_enfants.dto';
import { RoleType, Users } from 'src/entities/users.entity';
import { User } from 'src/common/decorators/user.decorator';
import { AuthGuard } from 'src/common/guards/auth.guard';
import { Roles } from 'src/common/decorators/roles.decorator';
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { RolesGuard } from 'src/common/guards/roles.guard';
import { EnfantsService } from './enfants.service';
import { Roles } from 'src/common/decorators/roles.decorator';
import { RoleType } from 'src/entities/users.entity';
import { Children } from 'src/entities/children.entity';
import { CreateEnfantsDto } from './dto/create_enfants.dto';
import { EnfantResponseDto } from './dto/enfants_response.dto';
import { mapEnfantsToResponseDto, mapEnfantToResponseDto } from './enfants.mapper';
@ApiBearerAuth('access-token')
@ApiBearerAuth()
@ApiTags('Enfants')
@UseGuards(AuthGuard, RolesGuard)
@UseGuards(RolesGuard)
@Controller('enfants')
export class EnfantsController {
constructor(private readonly enfantsService: EnfantsService) { }
@Roles(RoleType.PARENT)
// Inscrire un enfant
@Post()
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
return this.enfantsService.create(dto, currentUser);
@ApiOperation({ summary: 'Inscrire un enfant' })
@ApiResponse({ status: 201, type: EnfantResponseDto, description: 'L\'enfant a été inscrit avec succès.' })
@Roles(RoleType.PARENT, RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
async create(@Body() dto: CreateEnfantsDto): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.create(dto);
// Mapper l'entité enfant vers le DTO de réponse
return mapEnfantToResponseDto(enfant);
}
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
// Récupérer tous les enfants avec leurs liens parents
@ApiOperation({ summary: 'Récupérer tous les enfants avec leurs liens parents' })
@ApiResponse({ status: 200, type: [EnfantResponseDto], description: 'Liste de tous les enfants avec leurs liens parents.' })
@Get()
findAll() {
return this.enfantsService.findAll();
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async findAll(): Promise<EnfantResponseDto[]> {
const enfants = await this.enfantsService.findAll();
// Mapper les entités enfants vers les DTO de réponse
return mapEnfantsToResponseDto(enfants);
}
@Roles(
RoleType.PARENT,
RoleType.ADMINISTRATEUR,
RoleType.SUPER_ADMIN,
RoleType.GESTIONNAIRE
)
// Récupérer un enfant par son ID
@Get(':id')
findOne(
@Param('id', new ParseUUIDPipe()) id: string,
@User() currentUser: Users
) {
return this.enfantsService.findOne(id, currentUser);
@ApiOperation({ summary: 'Récupérer un enfant par son ID' })
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'Détails de l\'enfant avec ses liens parents.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN, RoleType.PARENT)
async findOne(@Param('id', new ParseUUIDPipe()) id: string): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.findOne(id);
// Mapper l'entité enfant vers le DTO de réponse
return mapEnfantToResponseDto(enfant);
}
@Roles(RoleType.ADMINISTRATEUR, RoleType.SUPER_ADMIN, RoleType.PARENT)
// Mettre à jour un enfant
@Patch(':id')
update(
@ApiOperation({ summary: 'Mettre à jour un enfant' })
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'L\'enfant a été mis à jour avec succès.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async update(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() dto: UpdateEnfantsDto,
@User() currentUser: Users,
) {
return this.enfantsService.update(id, dto, currentUser);
@Body() dto: Partial<CreateEnfantsDto>,
): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.update(id, dto);
// Mapper l'entité enfant vers le DTO de réponse
return mapEnfantToResponseDto(enfant);
}
@Roles(RoleType.SUPER_ADMIN)
// Supprimer un enfant
@Delete(':id')
remove(@Param('id', new ParseUUIDPipe()) id: string) {
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
@ApiOperation({ summary: 'Supprimer un enfant' })
@ApiResponse({ status: 204, description: 'L\'enfant a été supprimé avec succès.' })
async remove(@Param('id', new ParseUUIDPipe()) id: string): Promise<void> {
return this.enfantsService.remove(id);
}
}

View File

@ -0,0 +1,24 @@
import { Children } from "src/entities/children.entity";
import { EnfantResponseDto } from "./dto/enfants_response.dto";
// Fonction pour mapper une entité Children vers EnfantResponseDto
export function mapEnfantToResponseDto(enfant: Children): EnfantResponseDto {
return {
id: enfant.id,
status: enfant.status,
first_name: enfant.first_name,
last_name: enfant.last_name,
gender: enfant.gender,
birth_date: enfant.birth_date?.toISOString(),
due_date: enfant.due_date?.toISOString(),
photo_url: enfant.photo_url,
consent_photo: enfant.consent_photo,
is_multiple: enfant.is_multiple,
parent_id: enfant.parentLinks?.[0]?.parentId ?? null,
};
}
export function mapEnfantsToResponseDto(enfants: Children[]): EnfantResponseDto[] {
return enfants.map(mapEnfantToResponseDto);
};

View File

@ -3,16 +3,10 @@ import { EnfantsController } from './enfants.controller';
import { EnfantsService } from './enfants.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Children } from 'src/entities/children.entity';
import { Parents } from 'src/entities/parents.entity';
import { ParentsChildren } from 'src/entities/parents_children.entity';
import { AuthModule } from '../auth/auth.module';
@Module({
imports: [TypeOrmModule.forFeature([Children, Parents, ParentsChildren]),
AuthModule
],
imports: [TypeOrmModule.forFeature([Children])],
controllers: [EnfantsController],
providers: [EnfantsService]
providers: [EnfantsService],
})
export class EnfantsModule { }
export class EnfantsModule {}

View File

@ -1,16 +1,9 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Children, StatutEnfantType } from 'src/entities/children.entity';
import { Parents } from 'src/entities/parents.entity';
import { ParentsChildren } from 'src/entities/parents_children.entity';
import { RoleType, Users } from 'src/entities/users.entity';
import { Repository } from 'typeorm';
import { CreateEnfantsDto } from './dto/create_enfants.dto';
@Injectable()
@ -18,95 +11,99 @@ export class EnfantsService {
constructor(
@InjectRepository(Children)
private readonly childrenRepository: Repository<Children>,
@InjectRepository(Parents)
private readonly parentsRepository: Repository<Parents>,
@InjectRepository(ParentsChildren)
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
) { }
// Création dun enfant
async create(dto: CreateEnfantsDto, currentUser: Users): Promise<Children> {
const parent = await this.parentsRepository.findOne({
where: { user_id: currentUser.id },
});
if (!parent) throw new NotFoundException('Parent introuvable');
// Inscruire un enfant
async create(dto: CreateEnfantsDto): Promise<Children> {
// Vérif métier simple
if (dto.status !== StatutEnfantType.A_NAITRE && !dto.birth_date) {
throw new BadRequestException('Un enfant actif doit avoir une date de naissance');
// Vérifier que le parent existe
const parent = await this.parentsRepository.findOne({
where: { user_id: dto.id_parent },
});
// Si le parent n'existe pas, lever une exception
if (!parent) {
throw new NotFoundException(`Id de parent : ${dto.id_parent} introuvable`);
}
// Vérif doublon éventuel (ex: même prénom + date de naissance pour ce parent)
const exist = await this.childrenRepository.findOne({
where: {
// Evolution future : rendre l'option photo obligatoire ou non configurable
const optionObligatoire = false;
// Si l'enfant est pas a naitre, vérifier qu'une photo est fournie
// Puis si l'option est obligatoire
if (dto.status !== StatutEnfantType.A_NAITRE && !dto.photo_url && optionObligatoire) {
throw new BadRequestException(`Pour un enfant actif, une photo est obligatoire`);
}
// Créer l'enfant
const child = this.childrenRepository.create({
status: dto.status,
first_name: dto.first_name,
last_name: dto.last_name,
gender: dto.gender,
birth_date: dto.birth_date ? new Date(dto.birth_date) : undefined,
},
due_date: dto.due_date ? new Date(dto.due_date) : undefined,
photo_url: dto.photo_url,
consent_photo: dto.consent_photo,
consent_photo_at: dto.consent_photo_at ? new Date(dto.consent_photo_at) : undefined,
is_multiple: dto.is_multiple,
});
if (exist) throw new ConflictException('Cet enfant existe déjà');
// Création
const child = this.childrenRepository.create(dto);
await this.childrenRepository.save(child);
// Lien parent-enfant
// Créer le lien entre le parent et l'enfant
const parentLink = this.parentsChildrenRepository.create({
parentId: parent.user_id,
enfantId: child.id,
});
await this.parentsChildrenRepository.save(parentLink);
return this.findOne(child.id, currentUser);
return this.findOne(child.id);
}
// Liste des enfants
// Récupérer tous les enfants avec leurs liens parents
async findAll(): Promise<Children[]> {
return this.childrenRepository.find({
relations: ['parentLinks'],
order: { last_name: 'ASC', first_name: 'ASC' },
const all_children = await this.childrenRepository.find({
relations: [
'parentLinks',
'parentLinks.parent',
'parentLinks.parent.user',
],
order: { last_name: 'ASC', first_name: 'ASC' }
});
return all_children;
}
// Récupérer un enfant par id
async findOne(id: string, currentUser: Users): Promise<Children> {
// Récupérer un enfant par son id avec ses liens parents
async findOne(id: string): Promise<Children> {
const child = await this.childrenRepository.findOne({
where: { id },
relations: ['parentLinks'],
relations: [
'parentLinks',
'parentLinks.parent',
'parentLinks.parent.user',
],
});
if (!child) throw new NotFoundException('Enfant introuvable');
switch (currentUser.role) {
case RoleType.PARENT:
if (!child.parentLinks.some(link => link.parentId === currentUser.id)) {
throw new ForbiddenException('Cet enfant ne vous appartient pas');
if (!child) {
throw new NotFoundException(`Id d'enfant : ${id} introuvable`);
}
break;
case RoleType.ADMINISTRATEUR:
case RoleType.SUPER_ADMIN:
case RoleType.GESTIONNAIRE:
// accès complet
break;
default:
throw new ForbiddenException('Accès interdit');
}
return child;
}
// Mise à jour
async update(id: string, dto: Partial<CreateEnfantsDto>, currentUser: Users): Promise<Children> {
// Mettre à jour un enfant
async update(id: string, dto: Partial<CreateEnfantsDto>): Promise<Children> {
const child = await this.childrenRepository.findOne({ where: { id } });
if (!child) throw new NotFoundException('Enfant introuvable');
await this.childrenRepository.update(id, dto);
return this.findOne(id, currentUser);
if (!child) {
throw new NotFoundException(`Id d'enfant : ${id} introuvable`);
}
const { id_parent, ...childData } = dto;
await this.childrenRepository.update(id, childData);
return this.findOne(id);
}
// Suppression
// Supprimer un enfant
async remove(id: string): Promise<void> {
await this.childrenRepository.delete(id);
}

View File

@ -22,17 +22,16 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Get()
@ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
@ApiResponse({ status: 403, description: 'Accès refusé !' })
@ApiResponse({ status: 200, description: 'Liste des parents' })
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins' })
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: 200, 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);
}
@ -40,8 +39,7 @@ export class ParentsController {
@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é !' })
@ApiResponse({ status: 201, description: 'Parent créé avec succès' })
create(@Body() dto: CreateParentDto): Promise<Parents> {
return this.parentsService.create(dto);
}
@ -49,9 +47,8 @@ export class ParentsController {
@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: 200, 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);
}

View File

@ -9,8 +9,5 @@ import { Users } from 'src/entities/users.entity';
imports: [TypeOrmModule.forFeature([Parents, Users])],
controllers: [ParentsController],
providers: [ParentsService],
exports: [ParentsService,
TypeOrmModule,
],
})
export class ParentsModule { }
export class ParentsModule {}

View File

@ -5,21 +5,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Users } from 'src/entities/users.entity';
import { AuthModule } from '../auth/auth.module';
import { Validation } from 'src/entities/validations.entity';
import { ParentsModule } from '../parents/parents.module';
import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity';
import { AssistantesMaternellesModule } from '../assistantes_maternelles/assistantes_maternelles.module';
import { Parents } from 'src/entities/parents.entity';
@Module({
imports: [TypeOrmModule.forFeature(
[
Users,
Validation,
Parents,
AssistanteMaternelle,
]), forwardRef(() => AuthModule),
ParentsModule,
AssistantesMaternellesModule,
imports: [TypeOrmModule.forFeature([Users, Validation]),
forwardRef(() => AuthModule),
],
controllers: [UserController],
providers: [UserService],