Compare commits

..

58 Commits

Author SHA1 Message Date
f64f13b545 corrected token required 2025-10-02 12:10:36 +02:00
3caad838f6 corrected token required 2025-10-02 12:06:07 +02:00
f5ba267f78 added logout to controller 2025-10-02 11:56:24 +02:00
753237ee83 now more role specific 2025-10-02 11:17:22 +02:00
c9f58a81f1 enfants corrected 2025-10-01 22:07:08 +02:00
0d4ddc8f0e enfants corrected 2025-10-01 22:00:29 +02:00
8e313c9b04 enfants corrected 2025-10-01 21:45:39 +02:00
bdf43b6a96 remove route enfant 2025-10-01 20:52:24 +02:00
80bc815d20 je veux juste eviter les erreurs 500 2025-10-01 20:33:05 +02:00
ae75a79c2f je veux juste eviter les erreurs 500 2025-10-01 19:53:52 +02:00
5c447c7f2c je veux juste eviter les erreurs 500 2025-10-01 19:21:37 +02:00
868572a1e2 je veux juste eviter les erreurs 500 2025-10-01 16:13:16 +02:00
05529d299b correction + authguard current id corrected 2025-10-01 15:43:36 +02:00
78c155c910 correction ùapping id_utilisateur 2025-10-01 14:18:30 +02:00
b809932fc2 correction ùapping id_utilisateur 2025-10-01 14:10:36 +02:00
d79af25e04 change of swagger path to /api/docs 2025-09-30 20:27:25 +02:00
7d46b7bbf3 change of swagger path to /api/docs 2025-09-30 20:25:06 +02:00
6839dbb701 change of swagger path to /api/docs 2025-09-30 20:21:53 +02:00
23d56229ae change of swagger path to /api/docs 2025-09-30 20:20:38 +02:00
3eee920a58 change of swagger path to /api/docs 2025-09-30 20:17:37 +02:00
acec011bc1 change of swagger path to /api/docs 2025-09-30 20:14:23 +02:00
c46edbfdca change of swagger path to /api/docs 2025-09-30 19:59:44 +02:00
e7a189153d enfants corrected 2025-09-30 15:28:23 +02:00
7f8caf7df8 change of swagger path to /api/docs 2025-09-30 15:16:27 +02:00
d58406ff56 enfants controller corrected 2025-09-30 14:36:49 +02:00
28c7aa54bb enfants controller corrected 2025-09-30 14:28:43 +02:00
5626ff10f3 enfants controller corrected 2025-09-30 14:26:43 +02:00
730a41d81e change of swagger path to /api/docs 2025-09-30 13:01:44 +02:00
d2f2bbaabb change of swagger path to /api/docs 2025-09-30 12:53:46 +02:00
4b872cf32f change of swagger path to /api/docs 2025-09-30 12:30:21 +02:00
a16b07b8e0 added global prefix as a start to requests 2025-09-30 12:19:58 +02:00
476623b9fd enfants controller corrected 2025-09-30 11:47:21 +02:00
2f351b8302 added global prefix as a start to requests 2025-09-30 11:09:36 +02:00
824815b921 added global prefix as a start to requests 2025-09-30 11:03:50 +02:00
b313c62814 added api responses for parents controller 2025-09-30 10:37:59 +02:00
866d8ca1b2 Merge branch 'back/enfants-crud-01' into master 2025-09-29 15:14:20 +02:00
223bce143b adding dtos and changes to master branch 2025-09-29 13:01:57 +02:00
e402d75610 test push to add remove route (admins and gestionnaires only) 2025-09-29 10:27:43 +02:00
6b2ffd017c reset main for swagger 2025-09-26 17:03:29 +02:00
65ae32dc87 reset main for swagger 2025-09-26 16:44:47 +02:00
4cbb2ba64c swagger back on source 2025-09-26 16:32:05 +02:00
73e767322b come on swagger work now! 2025-09-26 15:41:58 +02:00
2bb29b681c correction user module 2025-09-26 13:31:05 +02:00
24c508187b edit for swagger 2025-09-26 12:58:06 +02:00
670a8c5b46 swagger please work 2025-09-26 12:37:31 +02:00
43607842e6 come on swagger! 2025-09-26 12:31:26 +02:00
4c822300c4 come on swagger! 2025-09-26 12:24:43 +02:00
7ff7dd71a8 fixed swagger import 2025-09-26 11:22:55 +02:00
ca1c11ff18 fixed swagger import 2025-09-26 11:08:58 +02:00
28aa0abcec trying to make api-docs public 2025-09-26 10:54:43 +02:00
1210016142 trying to make api-docs public 2025-09-26 10:47:28 +02:00
6cdbe702fc plese fix 2025-09-26 10:18:54 +02:00
ce23a75b8e please work this time 2025-09-25 13:19:21 +02:00
4ae50fca11 modules really fixed this time? 2025-09-25 13:13:21 +02:00
d615467ee5 modules fixed 2025-09-25 12:50:55 +02:00
c948fff21d modules fixed 2025-09-25 12:09:51 +02:00
5608459355 EnfantModule added 2025-09-25 11:39:57 +02:00
f240713dc0 feat(enfants): ajout de controller et service (refs #01) 2025-09-25 10:20:57 +02:00
18 changed files with 284 additions and 237 deletions

View File

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

View File

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

View File

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

View File

@ -66,4 +66,16 @@ export class AssistantesMaternellesController {
update(@Param('id') id: string, @Body() dto: UpdateAssistanteDto): Promise<AssistanteMaternelle> { update(@Param('id') id: string, @Body() dto: UpdateAssistanteDto): Promise<AssistanteMaternelle> {
return this.assistantesMaternellesService.update(id, dto); 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,9 +1,20 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AssistantesMaternellesService } from './assistantes_maternelles.service'; import { AssistantesMaternellesService } from './assistantes_maternelles.service';
import { AssistantesMaternellesController } from './assistantes_maternelles.controller'; 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({ @Module({
imports: [TypeOrmModule.forFeature([AssistanteMaternelle, Users]),
AuthModule
],
controllers: [AssistantesMaternellesController], controllers: [AssistantesMaternellesController],
providers: [AssistantesMaternellesService], providers: [AssistantesMaternellesService],
exports: [
AssistantesMaternellesService,
TypeOrmModule,
],
}) })
export class AssistantesMaternellesModule {} export class AssistantesMaternellesModule { }

View File

@ -71,4 +71,10 @@ export class AssistantesMaternellesService {
await this.assistantesMaternelleRepository.update(id, dto); await this.assistantesMaternelleRepository.update(id, dto);
return this.findOne(id); 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,6 +9,8 @@ import type { Request } from 'express';
import { UserService } from '../user/user.service'; import { UserService } from '../user/user.service';
import { ProfileResponseDto } from './dto/profile_response.dto'; import { ProfileResponseDto } from './dto/profile_response.dto';
import { RefreshTokenDto } from './dto/refresh_token.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') @ApiTags('Authentification')
@Controller('auth') @Controller('auth')
@ -62,5 +64,12 @@ export class AuthController {
statut: user.statut, statut: user.statut,
}; };
} }
@UseGuards(AuthGuard)
@ApiBearerAuth('access-token')
@Post('logout')
logout(@User() currentUser: Users) {
return this.authService.logout(currentUser.id);
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,24 +0,0 @@
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,10 +3,16 @@ import { EnfantsController } from './enfants.controller';
import { EnfantsService } from './enfants.service'; import { EnfantsService } from './enfants.service';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Children } from 'src/entities/children.entity'; 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({ @Module({
imports: [TypeOrmModule.forFeature([Children])], imports: [TypeOrmModule.forFeature([Children, Parents, ParentsChildren]),
AuthModule
],
controllers: [EnfantsController], controllers: [EnfantsController],
providers: [EnfantsService], providers: [EnfantsService]
}) })
export class EnfantsModule {} export class EnfantsModule { }

View File

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

View File

@ -22,16 +22,17 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) @Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Get() @Get()
@ApiResponse({ status: 200, description: 'Liste des parents' }) @ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins' }) @ApiResponse({ status: 403, description: 'Accès refusé !' })
getAll(): Promise<Parents[]> { getAll(): Promise<Parents[]> {
return this.parentsService.findAll(); return this.parentsService.findAll();
} }
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) @Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Get(':id') @Get(':id')
@ApiResponse({ status: 200, description: 'Détails du parent par ID utilisateur' }) @ApiResponse({ status: 200, type: Parents, description: 'Détails du parent par ID utilisateur' })
@ApiResponse({ status: 404, description: 'Parent non trouvé' }) @ApiResponse({ status: 404, description: 'Parent non trouvé' })
@ApiResponse({ status: 403, description: 'Accès refusé !' })
getOne(@Param('id') user_id: string): Promise<Parents> { getOne(@Param('id') user_id: string): Promise<Parents> {
return this.parentsService.findOne(user_id); return this.parentsService.findOne(user_id);
} }
@ -39,7 +40,8 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) @Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Post() @Post()
@ApiBody({ type: CreateParentDto }) @ApiBody({ type: CreateParentDto })
@ApiResponse({ status: 201, description: 'Parent créé avec succès' }) @ApiResponse({ status: 201, type: Parents, description: 'Parent créé avec succès' })
@ApiResponse({ status: 403, description: 'Accès refusé !' })
create(@Body() dto: CreateParentDto): Promise<Parents> { create(@Body() dto: CreateParentDto): Promise<Parents> {
return this.parentsService.create(dto); return this.parentsService.create(dto);
} }
@ -47,8 +49,9 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) @Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Patch(':id') @Patch(':id')
@ApiBody({ type: UpdateParentsDto }) @ApiBody({ type: UpdateParentsDto })
@ApiResponse({ status: 200, description: 'Parent mis à jour avec succès' }) @ApiResponse({ status: 200, type: Parents, description: 'Parent mis à jour avec succès' })
@ApiResponse({ status: 404, description: 'Parent introuvable' }) @ApiResponse({ status: 404, description: 'Parent introuvable' })
@ApiResponse({ status: 403, description: 'Accès refusé !' })
update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> { update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> {
return this.parentsService.update(id, dto); return this.parentsService.update(id, dto);
} }

View File

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

View File

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