Compare commits
36
Commits
223bce143b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f64f13b545 | ||
|
|
3caad838f6 | ||
|
|
f5ba267f78 | ||
|
|
753237ee83 | ||
|
|
c9f58a81f1 | ||
|
|
0d4ddc8f0e | ||
|
|
8e313c9b04 | ||
|
|
bdf43b6a96 | ||
|
|
80bc815d20 | ||
|
|
ae75a79c2f | ||
|
|
5c447c7f2c | ||
|
|
868572a1e2 | ||
|
|
05529d299b | ||
|
|
78c155c910 | ||
|
|
b809932fc2 | ||
|
|
d79af25e04 | ||
|
|
7d46b7bbf3 | ||
|
|
6839dbb701 | ||
|
|
23d56229ae | ||
|
|
3eee920a58 | ||
|
|
acec011bc1 | ||
|
|
c46edbfdca | ||
|
|
e7a189153d | ||
|
|
7f8caf7df8 | ||
|
|
d58406ff56 | ||
|
|
28c7aa54bb | ||
|
|
5626ff10f3 | ||
|
|
730a41d81e | ||
|
|
d2f2bbaabb | ||
|
|
4b872cf32f | ||
|
|
a16b07b8e0 | ||
|
|
476623b9fd | ||
|
|
2f351b8302 | ||
|
|
824815b921 | ||
|
|
b313c62814 | ||
|
|
866d8ca1b2 |
@@ -11,7 +11,7 @@ export class AuthGuard implements CanActivate {
|
|||||||
private readonly jwtService: JwtService,
|
private readonly jwtService: JwtService,
|
||||||
private readonly reflector: Reflector,
|
private readonly reflector: Reflector,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||||
@@ -24,8 +24,8 @@ export class AuthGuard implements CanActivate {
|
|||||||
if (request.path.startsWith('/api-docs')) {
|
if (request.path.startsWith('/api-docs')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const authHeader = request.headers['authorization'] as string | undefined;
|
|
||||||
|
|
||||||
|
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');
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,12 @@ export class AuthGuard implements CanActivate {
|
|||||||
const payload = await this.jwtService.verifyAsync(token, {
|
const payload = await this.jwtService.verifyAsync(token, {
|
||||||
secret: this.configService.get<string>('jwt.accessSecret'),
|
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é');
|
||||||
|
|||||||
+3
-4
@@ -41,12 +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);
|
||||||
ignoreGlobalPrefix: true,
|
SwaggerModule.setup('api/v1/swagger', app, document);
|
||||||
});
|
|
||||||
SwaggerModule.setup('api-docs', app, document);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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'}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,37 @@
|
|||||||
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' })
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@ApiProperty({ enum: StatutEnfantType })
|
@ApiProperty({ enum: StatutEnfantType })
|
||||||
status: StatutEnfantType;
|
status: StatutEnfantType;
|
||||||
|
|
||||||
@ApiProperty({ example: 'Georges', required: false })
|
@ApiProperty({ example: 'Georges', required: false })
|
||||||
first_name?: string;
|
first_name?: string;
|
||||||
|
|
||||||
@ApiProperty({ example: 'Dupont', required: false })
|
@ApiProperty({ example: 'Dupont', required: false })
|
||||||
last_name?: string;
|
last_name?: string;
|
||||||
|
|
||||||
@ApiProperty({ enum: GenreType, required: false })
|
@ApiProperty({ enum: GenreType, required: false })
|
||||||
gender?: GenreType;
|
gender?: GenreType;
|
||||||
|
|
||||||
@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 })
|
||||||
photo_url?: string;
|
photo_url?: string;
|
||||||
|
|
||||||
@ApiProperty({ example: false })
|
@ApiProperty({ example: false })
|
||||||
consent_photo: boolean;
|
consent_photo: boolean;
|
||||||
|
|
||||||
@ApiProperty({ example: false })
|
@ApiProperty({ example: false })
|
||||||
is_multiple: boolean;
|
is_multiple: boolean;
|
||||||
|
|
||||||
@ApiProperty({ example: 'UUID-parent' })
|
@ApiProperty({ example: 'UUID-parent' })
|
||||||
parent_id: string;
|
parent_id: string;
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -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) {}
|
||||||
|
|||||||
@@ -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
|
|
||||||
@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();
|
|
||||||
|
|
||||||
// Mapper les entités enfants vers les DTO de réponse
|
@Roles(RoleType.PARENT)
|
||||||
return mapEnfantsToResponseDto(enfants);
|
@Post()
|
||||||
}
|
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
|
||||||
|
return this.enfantsService.create(dto, currentUser);
|
||||||
|
}
|
||||||
|
|
||||||
// Récupérer un enfant par son ID
|
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||||
@Get(':id')
|
@Get()
|
||||||
@ApiOperation({ summary: 'Récupérer un enfant par son ID' })
|
findAll() {
|
||||||
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'Détails de l\'enfant avec ses liens parents.' })
|
return this.enfantsService.findAll();
|
||||||
@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
|
@Roles(
|
||||||
return mapEnfantToResponseDto(enfant);
|
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
|
@Roles(RoleType.ADMINISTRATEUR, RoleType.SUPER_ADMIN, RoleType.PARENT)
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
@ApiOperation({ summary: 'Mettre à jour un enfant' })
|
update(
|
||||||
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'L\'enfant a été mis à jour avec succès.' })
|
@Param('id', new ParseUUIDPipe()) id: string,
|
||||||
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
@Body() dto: UpdateEnfantsDto,
|
||||||
async update(
|
@User() currentUser: Users,
|
||||||
@Param('id', new ParseUUIDPipe()) id: string,
|
) {
|
||||||
@Body() dto: Partial<CreateEnfantsDto>,
|
return this.enfantsService.update(id, dto, currentUser);
|
||||||
): Promise<EnfantResponseDto> {
|
}
|
||||||
const enfant = await this.enfantsService.update(id, dto);
|
|
||||||
|
|
||||||
// Mapper l'entité enfant vers le DTO de réponse
|
@Roles(RoleType.SUPER_ADMIN)
|
||||||
return mapEnfantToResponseDto(enfant);
|
@Delete(':id')
|
||||||
}
|
remove(@Param('id', new ParseUUIDPipe()) id: string) {
|
||||||
|
return this.enfantsService.remove(id);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
|
||||||
};
|
|
||||||
|
|
||||||
@@ -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 { }
|
||||||
|
|||||||
@@ -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 { 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()
|
||||||
export class EnfantsService {
|
export class EnfantsService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Children)
|
@InjectRepository(Children)
|
||||||
private readonly childrenRepository: Repository<Children>,
|
private readonly childrenRepository: Repository<Children>,
|
||||||
|
@InjectRepository(Parents)
|
||||||
|
private readonly parentsRepository: Repository<Parents>,
|
||||||
|
@InjectRepository(ParentsChildren)
|
||||||
|
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
||||||
|
) { }
|
||||||
|
|
||||||
@InjectRepository(Parents)
|
// Création d’un enfant
|
||||||
private readonly parentsRepository: Repository<Parents>,
|
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)
|
// Vérif métier simple
|
||||||
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
if (dto.status !== StatutEnfantType.A_NAITRE && !dto.birth_date) {
|
||||||
) { }
|
throw new BadRequestException('Un enfant actif doit avoir une date de naissance');
|
||||||
|
}
|
||||||
|
|
||||||
// Inscruire un enfant
|
// Vérif doublon éventuel (ex: même prénom + date de naissance pour ce parent)
|
||||||
async create(dto: CreateEnfantsDto): Promise<Children> {
|
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
|
// Création
|
||||||
const parent = await this.parentsRepository.findOne({
|
const child = this.childrenRepository.create(dto);
|
||||||
where: { user_id: dto.id_parent },
|
await this.childrenRepository.save(child);
|
||||||
});
|
|
||||||
// Si le parent n'existe pas, lever une exception
|
// Lien parent-enfant
|
||||||
if (!parent) {
|
const parentLink = this.parentsChildrenRepository.create({
|
||||||
throw new NotFoundException(`Id de parent : ${dto.id_parent} introuvable`);
|
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
|
case RoleType.ADMINISTRATEUR:
|
||||||
const optionObligatoire = false;
|
case RoleType.SUPER_ADMIN:
|
||||||
// Si l'enfant est pas a naitre, vérifier qu'une photo est fournie
|
case RoleType.GESTIONNAIRE:
|
||||||
// Puis si l'option est obligatoire
|
// accès complet
|
||||||
if (dto.status !== StatutEnfantType.A_NAITRE && !dto.photo_url && optionObligatoire) {
|
break;
|
||||||
throw new BadRequestException(`Pour un enfant actif, une photo est obligatoire`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Créer l'enfant
|
default:
|
||||||
const child = this.childrenRepository.create({
|
throw new ForbiddenException('Accès interdit');
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Récupérer tous les enfants avec leurs liens parents
|
return child;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
// Mise à jour
|
||||||
async update(id: string, dto: Partial<CreateEnfantsDto>): Promise<Children> {
|
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`);
|
|
||||||
}
|
|
||||||
const { id_parent, ...childData } = dto;
|
|
||||||
await this.childrenRepository.update(id, childData);
|
|
||||||
return this.findOne(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Supprimer un enfant
|
await this.childrenRepository.update(id, dto);
|
||||||
async remove(id: string): Promise<void> {
|
return this.findOne(id, currentUser);
|
||||||
await this.childrenRepository.delete(id);
|
}
|
||||||
}
|
|
||||||
}
|
// Suppression
|
||||||
|
async remove(id: string): Promise<void> {
|
||||||
|
await this.childrenRepository.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user