Compare commits
39
Commits
e402d75610
...
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 | ||
|
|
223bce143b | ||
|
|
a8ca887f3d | ||
|
|
7c06fc9c00 |
@@ -55,3 +55,10 @@ pids
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
.env
|
||||
|
||||
|
||||
# Tests bdd
|
||||
.vscode/
|
||||
BDD.sql
|
||||
migrations/
|
||||
src/seed/
|
||||
|
||||
@@ -11,7 +11,7 @@ export class AuthGuard implements CanActivate {
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly reflector: Reflector,
|
||||
private readonly configService: ConfigService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
@@ -24,8 +24,8 @@ export class AuthGuard implements CanActivate {
|
||||
if (request.path.startsWith('/api-docs')) {
|
||||
return true;
|
||||
}
|
||||
const authHeader = request.headers['authorization'] as string | undefined;
|
||||
|
||||
const authHeader = request.headers['authorization'] as string | undefined;
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
throw new UnauthorizedException('Token manquant ou invalide');
|
||||
}
|
||||
@@ -35,7 +35,12 @@ export class AuthGuard implements CanActivate {
|
||||
const payload = await this.jwtService.verifyAsync(token, {
|
||||
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é');
|
||||
|
||||
+3
-4
@@ -41,12 +41,11 @@ async function bootstrap() {
|
||||
},
|
||||
'access-token',
|
||||
)
|
||||
//.addServer('/api/v1')
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config, {
|
||||
ignoreGlobalPrefix: true,
|
||||
});
|
||||
SwaggerModule.setup('api-docs', app, document);
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api/v1/swagger', app, document);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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'}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { GenreType, StatutEnfantType } from 'src/entities/children.entity';
|
||||
|
||||
export class EnfantResponseDto {
|
||||
@ApiProperty({ example: 'UUID-enfant' })
|
||||
id: string;
|
||||
|
||||
@ApiProperty({ enum: StatutEnfantType })
|
||||
status: StatutEnfantType;
|
||||
|
||||
@ApiProperty({ example: 'Georges', required: false })
|
||||
first_name?: string;
|
||||
|
||||
@ApiProperty({ example: 'Dupont', required: false })
|
||||
last_name?: string;
|
||||
|
||||
@ApiProperty({ enum: GenreType, required: false })
|
||||
gender?: GenreType;
|
||||
|
||||
@ApiProperty({ example: '2018-06-24', required: false })
|
||||
birth_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: false })
|
||||
consent_photo: boolean;
|
||||
|
||||
@ApiProperty({ example: false })
|
||||
is_multiple: boolean;
|
||||
|
||||
@ApiProperty({ example: 'UUID-parent' })
|
||||
parent_id: string;
|
||||
}
|
||||
@@ -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) {}
|
||||
|
||||
@@ -1,63 +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 { 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) { }
|
||||
constructor(private readonly enfantsService: EnfantsService) { }
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Inscrire un enfant' })
|
||||
@ApiResponse({ status: 201, type: Children, description: 'L\'enfant a été inscrit avec succès.' })
|
||||
@Roles(RoleType.PARENT, RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
async create(@Body() dto: CreateEnfantsDto): Promise<Children> {
|
||||
return this.enfantsService.create(dto);
|
||||
}
|
||||
@Roles(RoleType.PARENT)
|
||||
@Post()
|
||||
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
|
||||
return this.enfantsService.create(dto, currentUser);
|
||||
}
|
||||
|
||||
// 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: [Children], description: 'Liste de tous les enfants avec leurs liens parents.' })
|
||||
@Get()
|
||||
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||
async findAll(): Promise<Children[]> {
|
||||
return this.enfantsService.findAll();
|
||||
}
|
||||
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.enfantsService.findAll();
|
||||
}
|
||||
|
||||
// Récupérer un enfant par son ID
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Récupérer un enfant par son ID' })
|
||||
@ApiResponse({ status: 200, type: Children, 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<Children> {
|
||||
return this.enfantsService.findOne(id);
|
||||
}
|
||||
@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: Children, 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<Children> {
|
||||
return 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +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 { ParentsModule } from '../parents/parents.module';
|
||||
import { ParentsChildren } from 'src/entities/parents_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, ParentsChildren, Parents]),
|
||||
ParentsModule,
|
||||
],
|
||||
imports: [TypeOrmModule.forFeature([Children, Parents, ParentsChildren]),
|
||||
AuthModule
|
||||
|
||||
],
|
||||
controllers: [EnfantsController],
|
||||
providers: [EnfantsService],
|
||||
exports: [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 { 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 d’un 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user