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

View File

@ -21,8 +21,11 @@ export class AuthGuard implements CanActivate {
if (isPublic) return true;
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 ')) {
throw new UnauthorizedException('Token manquant ou invalide');
}
@ -30,9 +33,14 @@ 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'), // ✅ corrige ici
secret: this.configService.get<string>('jwt.accessSecret'),
});
request.user = payload;
request.user = {
...payload,
id: payload.sub,
};
return true;
} catch (error) {
throw new UnauthorizedException('Token invalide ou expiré');

View File

@ -41,10 +41,11 @@ async function bootstrap() {
},
'access-token',
)
//.addServer('/api/v1')
.build();
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> {
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 { 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,4 +71,10 @@ 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,6 +9,8 @@ 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')
@ -62,5 +64,12 @@ 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,7 +10,6 @@ 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 {
@ -131,6 +130,8 @@ export class AuthService {
}
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 { IsBoolean, IsDateString, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID, 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,
MaxLength,
ValidateIf,
} from 'class-validator';
import { GenreType, StatutEnfantType } from 'src/entities/children.entity';
export class CreateEnfantsDto {
@ApiProperty({ enum: StatutEnfantType, example: StatutEnfantType.ACTIF })
@ -14,7 +23,7 @@ export class CreateEnfantsDto {
@MaxLength(100)
first_name?: string;
@ApiProperty({ example: 'Lucas', required: false })
@ApiProperty({ example: 'Dupont', required: false })
@IsOptional()
@IsString()
@MaxLength(100)
@ -31,7 +40,7 @@ export class CreateEnfantsDto {
@IsDateString()
birth_date?: string;
@ApiProperty({ example: '2024-12-24', required: false })
@ApiProperty({ example: '2025-12-15', required: false })
@ValidateIf(o => o.status === StatutEnfantType.A_NAITRE)
@IsOptional()
@IsDateString()
@ -54,10 +63,4 @@ export class CreateEnfantsDto {
@ApiProperty({ default: false })
@IsBoolean()
is_multiple: boolean;
@ApiProperty({ example: 'UUID-parent' })
@IsUUID()
@IsNotEmpty()
id_parent: string;
}

View File

@ -1,38 +1,37 @@
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' })
id: string;
@ApiProperty({ example: 'UUID-enfant' })
id: string;
@ApiProperty({ enum: StatutEnfantType })
status: StatutEnfantType;
@ApiProperty({ enum: StatutEnfantType })
status: StatutEnfantType;
@ApiProperty({ example: 'Georges', required: false })
first_name?: string;
@ApiProperty({ example: 'Georges', required: false })
first_name?: string;
@ApiProperty({ example: 'Dupont', required: false })
last_name?: string;
@ApiProperty({ example: 'Dupont', required: false })
last_name?: string;
@ApiProperty({ enum: GenreType, required: false })
gender?: GenreType;
@ApiProperty({ enum: GenreType, required: false })
gender?: GenreType;
@ApiProperty({ example: '2018-06-24', required: false })
birth_date?: string;
@ApiProperty({ example: '2018-06-24', required: false })
birth_date?: string;
@ApiProperty({ example: '2024-12-24', required: false })
due_date?: string;
@ApiProperty({ example: '2025-12-15', required: false })
due_date?: string;
@ApiProperty({ example: 'https://monimage.com/photo.jpg', required: false })
photo_url?: string;
@ApiProperty({ example: 'https://monimage.com/photo.jpg', required: false })
photo_url?: string;
@ApiProperty({ example: false })
consent_photo: boolean;
@ApiProperty({ example: false })
consent_photo: boolean;
@ApiProperty({ example: false })
is_multiple: boolean;
@ApiProperty({ example: false })
is_multiple: boolean;
@ApiProperty({ example: 'UUID-parent' })
parent_id: string;
}
@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) {}
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 { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { RolesGuard } from 'src/common/guards/roles.guard';
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 { 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';
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 { RolesGuard } from 'src/common/guards/roles.guard';
@ApiBearerAuth()
@ApiBearerAuth('access-token')
@ApiTags('Enfants')
@UseGuards(RolesGuard)
@UseGuards(AuthGuard, RolesGuard)
@Controller('enfants')
export class EnfantsController {
constructor(private readonly enfantsService: EnfantsService) { }
// Inscrire un enfant
@Post()
@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);
}
// 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()
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async findAll(): Promise<EnfantResponseDto[]> {
const enfants = await this.enfantsService.findAll();
constructor(private readonly enfantsService: EnfantsService) { }
// Mapper les entités enfants vers les DTO de réponse
return mapEnfantsToResponseDto(enfants);
}
@Roles(RoleType.PARENT)
@Post()
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
return this.enfantsService.create(dto, currentUser);
}
// Récupérer un enfant par son ID
@Get(':id')
@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);
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
@Get()
findAll() {
return this.enfantsService.findAll();
}
// Mapper l'entité enfant vers le DTO de réponse
return mapEnfantToResponseDto(enfant);
}
@Roles(
RoleType.PARENT,
RoleType.ADMINISTRATEUR,
RoleType.SUPER_ADMIN,
RoleType.GESTIONNAIRE
)
@Get(':id')
findOne(
@Param('id', new ParseUUIDPipe()) id: string,
@User() currentUser: Users
) {
return this.enfantsService.findOne(id, currentUser);
}
// 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,
@Body() dto: Partial<CreateEnfantsDto>,
): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.update(id, dto);
@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);
}
// Mapper l'entité enfant vers le DTO de réponse
return mapEnfantToResponseDto(enfant);
}
// Supprimer un enfant
@Delete(':id')
@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);
}
@Roles(RoleType.SUPER_ADMIN)
@Delete(':id')
remove(@Param('id', new ParseUUIDPipe()) id: string) {
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 { 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])],
imports: [TypeOrmModule.forFeature([Children, Parents, ParentsChildren]),
AuthModule
],
controllers: [EnfantsController],
providers: [EnfantsService],
providers: [EnfantsService]
})
export class EnfantsModule {}
export class EnfantsModule { }

View File

@ -1,110 +1,113 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import {
BadRequestException,
ConflictException,
ForbiddenException,
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 { Repository } from 'typeorm';
import { RoleType, Users } from 'src/entities/users.entity';
import { CreateEnfantsDto } from './dto/create_enfants.dto';
@Injectable()
export class EnfantsService {
constructor(
@InjectRepository(Children)
private readonly childrenRepository: Repository<Children>,
constructor(
@InjectRepository(Children)
private readonly childrenRepository: Repository<Children>,
@InjectRepository(Parents)
private readonly parentsRepository: Repository<Parents>,
@InjectRepository(ParentsChildren)
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
) { }
@InjectRepository(Parents)
private readonly parentsRepository: Repository<Parents>,
// 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');
@InjectRepository(ParentsChildren)
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
) { }
// 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');
}
// Inscruire un enfant
async create(dto: CreateEnfantsDto): Promise<Children> {
// Vérif doublon éventuel (ex: même prénom + date de naissance pour ce parent)
const exist = await this.childrenRepository.findOne({
where: {
first_name: dto.first_name,
last_name: dto.last_name,
birth_date: dto.birth_date ? new Date(dto.birth_date) : undefined,
},
});
if (exist) throw new ConflictException('Cet enfant existe déjà');
// 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`);
// Création
const child = this.childrenRepository.create(dto);
await this.childrenRepository.save(child);
// Lien parent-enfant
const parentLink = this.parentsChildrenRepository.create({
parentId: parent.user_id,
enfantId: child.id,
});
await this.parentsChildrenRepository.save(parentLink);
return this.findOne(child.id, currentUser);
}
// Liste des enfants
async findAll(): Promise<Children[]> {
return this.childrenRepository.find({
relations: ['parentLinks'],
order: { last_name: 'ASC', first_name: 'ASC' },
});
}
// Récupérer un enfant par id
async findOne(id: string, currentUser: Users): Promise<Children> {
const child = await this.childrenRepository.findOne({
where: { id },
relations: ['parentLinks'],
});
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');
}
break;
// 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`);
}
case RoleType.ADMINISTRATEUR:
case RoleType.SUPER_ADMIN:
case RoleType.GESTIONNAIRE:
// accès complet
break;
// 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,
});
await this.childrenRepository.save(child);
// 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);
default:
throw new ForbiddenException('Accès interdit');
}
// Récupérer tous les enfants avec leurs liens parents
async findAll(): Promise<Children[]> {
const all_children = await this.childrenRepository.find({
relations: [
'parentLinks',
'parentLinks.parent',
'parentLinks.parent.user',
],
order: { last_name: 'ASC', first_name: 'ASC' }
});
return all_children;
}
return child;
}
// 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',
'parentLinks.parent',
'parentLinks.parent.user',
],
});
if (!child) {
throw new NotFoundException(`Id d'enfant : ${id} introuvable`);
}
return child;
}
// 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(`Id d'enfant : ${id} introuvable`);
}
const { id_parent, ...childData } = dto;
await this.childrenRepository.update(id, childData);
return this.findOne(id);
}
// Mise à jour
async update(id: string, dto: Partial<CreateEnfantsDto>, currentUser: Users): Promise<Children> {
const child = await this.childrenRepository.findOne({ where: { id } });
if (!child) throw new NotFoundException('Enfant introuvable');
// Supprimer un enfant
async remove(id: string): Promise<void> {
await this.childrenRepository.delete(id);
}
}
await this.childrenRepository.update(id, dto);
return this.findOne(id, currentUser);
}
// Suppression
async remove(id: string): Promise<void> {
await this.childrenRepository.delete(id);
}
}

View File

@ -22,16 +22,17 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Get()
@ApiResponse({ status: 200, description: 'Liste des parents' })
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins' })
@ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
@ApiResponse({ status: 403, description: 'Accès refusé !' })
getAll(): Promise<Parents[]> {
return this.parentsService.findAll();
}
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@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: 403, description: 'Accès refusé !' })
getOne(@Param('id') user_id: string): Promise<Parents> {
return this.parentsService.findOne(user_id);
}
@ -39,7 +40,8 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Post()
@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> {
return this.parentsService.create(dto);
}
@ -47,8 +49,9 @@ export class ParentsController {
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
@Patch(':id')
@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: 403, description: 'Accès refusé !' })
update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> {
return this.parentsService.update(id, dto);
}

View File

@ -9,5 +9,8 @@ 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,10 +5,21 @@ 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]),
forwardRef(() => AuthModule),
imports: [TypeOrmModule.forFeature(
[
Users,
Validation,
Parents,
AssistanteMaternelle,
]), forwardRef(() => AuthModule),
ParentsModule,
AssistantesMaternellesModule,
],
controllers: [UserController],
providers: [UserService],