Compare commits
No commits in common. "master" and "back/enfants-crud-01" have entirely different histories.
master
...
back/enfan
@ -13,7 +13,6 @@ import { ParentsModule } from './routes/parents/parents.module';
|
||||
import { AuthModule } from './routes/auth/auth.module';
|
||||
import { SentryGlobalFilter } from '@sentry/nestjs/setup';
|
||||
import { AllExceptionsFilter } from './common/filters/all_exceptions.filters';
|
||||
import { EnfantsModule } from './routes/enfants/enfants.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -47,7 +46,6 @@ import { EnfantsModule } from './routes/enfants/enfants.module';
|
||||
}),
|
||||
UserModule,
|
||||
ParentsModule,
|
||||
EnfantsModule,
|
||||
AuthModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
|
||||
@ -21,11 +21,8 @@ export class AuthGuard implements CanActivate {
|
||||
if (isPublic) return true;
|
||||
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
if (request.path.startsWith('/api-docs')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const authHeader = request.headers['authorization'] as string | undefined;
|
||||
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
throw new UnauthorizedException('Token manquant ou invalide');
|
||||
}
|
||||
@ -33,14 +30,9 @@ export class AuthGuard implements CanActivate {
|
||||
const token = authHeader.split(' ')[1];
|
||||
try {
|
||||
const payload = await this.jwtService.verifyAsync(token, {
|
||||
secret: this.configService.get<string>('jwt.accessSecret'),
|
||||
secret: this.configService.get<string>('jwt.accessSecret'), // ✅ corrige ici
|
||||
});
|
||||
|
||||
request.user = {
|
||||
...payload,
|
||||
id: payload.sub,
|
||||
};
|
||||
|
||||
request.user = payload;
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new UnauthorizedException('Token invalide ou expiré');
|
||||
|
||||
@ -41,11 +41,10 @@ async function bootstrap() {
|
||||
},
|
||||
'access-token',
|
||||
)
|
||||
//.addServer('/api/v1')
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api/v1/swagger', app, document);
|
||||
SwaggerModule.setup('api-docs', app, document);
|
||||
|
||||
|
||||
|
||||
|
||||
@ -66,16 +66,4 @@ export class AssistantesMaternellesController {
|
||||
update(@Param('id') id: string, @Body() dto: UpdateAssistanteDto): Promise<AssistanteMaternelle> {
|
||||
return this.assistantesMaternellesService.update(id, dto);
|
||||
}
|
||||
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE, RoleType.ADMINISTRATEUR)
|
||||
@ApiOperation({ summary: 'Supprimer une nounou' })
|
||||
@ApiResponse({ status: 200, description: 'Nounou supprimée avec succès' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins, gestionnaires et administrateurs' })
|
||||
@ApiResponse({ status: 404, description: 'Nounou non trouvée' })
|
||||
@ApiParam({ name: 'id', description: "UUID de la nounou" })
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string): Promise<{ message: string }>
|
||||
{
|
||||
return this.assistantesMaternellesService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,20 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AssistantesMaternellesService } from './assistantes_maternelles.service';
|
||||
import { AssistantesMaternellesController } from './assistantes_maternelles.controller';
|
||||
import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Users } from 'src/entities/users.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([AssistanteMaternelle, Users]),
|
||||
AuthModule
|
||||
],
|
||||
controllers: [AssistantesMaternellesController],
|
||||
providers: [AssistantesMaternellesService],
|
||||
exports: [
|
||||
AssistantesMaternellesService,
|
||||
TypeOrmModule,
|
||||
],
|
||||
})
|
||||
export class AssistantesMaternellesModule { }
|
||||
export class AssistantesMaternellesModule {}
|
||||
|
||||
@ -71,10 +71,4 @@ export class AssistantesMaternellesService {
|
||||
await this.assistantesMaternelleRepository.update(id, dto);
|
||||
return this.findOne(id);
|
||||
}
|
||||
|
||||
// Suppression d’une assistante maternelle
|
||||
async remove(id: string): Promise<{ message: string }> {
|
||||
await this.assistantesMaternelleRepository.delete(id);
|
||||
return { message: 'Assistante maternelle supprimée' };
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,8 +9,6 @@ import type { Request } from 'express';
|
||||
import { UserService } from '../user/user.service';
|
||||
import { ProfileResponseDto } from './dto/profile_response.dto';
|
||||
import { RefreshTokenDto } from './dto/refresh_token.dto';
|
||||
import { User } from 'src/common/decorators/user.decorator';
|
||||
import { Users } from 'src/entities/users.entity';
|
||||
|
||||
@ApiTags('Authentification')
|
||||
@Controller('auth')
|
||||
@ -64,12 +62,5 @@ export class AuthController {
|
||||
statut: user.statut,
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth('access-token')
|
||||
@Post('logout')
|
||||
logout(@User() currentUser: Users) {
|
||||
return this.authService.logout(currentUser.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ import { RegisterDto } from './dto/register.dto';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { RoleType, StatutUtilisateurType, Users } from 'src/entities/users.entity';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { DeepPartial } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@ -130,8 +131,6 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async logout(userId: string) {
|
||||
// Pour le moment envoyer un message clair
|
||||
return { success: true, message: 'Deconnexion'}
|
||||
|
||||
// Pour une implémentation simple, on ne fait rien ici.
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,6 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import {
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
MaxLength,
|
||||
ValidateIf,
|
||||
} from 'class-validator';
|
||||
import { GenreType, StatutEnfantType } from 'src/entities/children.entity';
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsDateString, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength, ValidateIf } from "class-validator";
|
||||
import { GenreType, StatutEnfantType } from "src/entities/children.entity";
|
||||
|
||||
export class CreateEnfantsDto {
|
||||
@ApiProperty({ enum: StatutEnfantType, example: StatutEnfantType.ACTIF })
|
||||
@ -23,7 +14,7 @@ export class CreateEnfantsDto {
|
||||
@MaxLength(100)
|
||||
first_name?: string;
|
||||
|
||||
@ApiProperty({ example: 'Dupont', required: false })
|
||||
@ApiProperty({ example: 'Lucas', required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(100)
|
||||
@ -40,7 +31,7 @@ export class CreateEnfantsDto {
|
||||
@IsDateString()
|
||||
birth_date?: string;
|
||||
|
||||
@ApiProperty({ example: '2025-12-15', required: false })
|
||||
@ApiProperty({ example: '2024-12-24', required: false })
|
||||
@ValidateIf(o => o.status === StatutEnfantType.A_NAITRE)
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
@ -63,4 +54,10 @@ export class CreateEnfantsDto {
|
||||
@ApiProperty({ default: false })
|
||||
@IsBoolean()
|
||||
is_multiple: boolean;
|
||||
|
||||
@ApiProperty({ example: 'UUID-parent' })
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
id_parent: string;
|
||||
|
||||
}
|
||||
|
||||
@ -1,37 +1,38 @@
|
||||
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: '2025-12-15', required: false })
|
||||
due_date?: string;
|
||||
@ApiProperty({ example: '2024-12-24', 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;
|
||||
}
|
||||
@ -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) {}
|
||||
@ -1,70 +1,77 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { EnfantsService } from './enfants.service';
|
||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||
import { UpdateEnfantsDto } from './dto/update_enfants.dto';
|
||||
import { RoleType, Users } from 'src/entities/users.entity';
|
||||
import { User } from 'src/common/decorators/user.decorator';
|
||||
import { AuthGuard } from 'src/common/guards/auth.guard';
|
||||
import { Roles } from 'src/common/decorators/roles.decorator';
|
||||
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { RolesGuard } from 'src/common/guards/roles.guard';
|
||||
import { EnfantsService } from './enfants.service';
|
||||
import { Roles } from 'src/common/decorators/roles.decorator';
|
||||
import { RoleType } from 'src/entities/users.entity';
|
||||
import { Children } from 'src/entities/children.entity';
|
||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||
import { EnfantResponseDto } from './dto/enfants_response.dto';
|
||||
import { mapEnfantsToResponseDto, mapEnfantToResponseDto } from './enfants.mapper';
|
||||
|
||||
@ApiBearerAuth('access-token')
|
||||
@ApiBearerAuth()
|
||||
@ApiTags('Enfants')
|
||||
@UseGuards(AuthGuard, RolesGuard)
|
||||
@UseGuards(RolesGuard)
|
||||
@Controller('enfants')
|
||||
export class EnfantsController {
|
||||
constructor(private readonly enfantsService: EnfantsService) { }
|
||||
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);
|
||||
|
||||
@Roles(RoleType.PARENT)
|
||||
@Post()
|
||||
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
|
||||
return this.enfantsService.create(dto, currentUser);
|
||||
}
|
||||
// Mapper l'entité enfant vers le DTO de réponse
|
||||
return mapEnfantToResponseDto(enfant);
|
||||
}
|
||||
|
||||
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.enfantsService.findAll();
|
||||
}
|
||||
// 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();
|
||||
|
||||
@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);
|
||||
}
|
||||
// Mapper les entités enfants vers les DTO de réponse
|
||||
return mapEnfantsToResponseDto(enfants);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
// 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.SUPER_ADMIN)
|
||||
@Delete(':id')
|
||||
remove(@Param('id', new ParseUUIDPipe()) id: string) {
|
||||
return this.enfantsService.remove(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,
|
||||
@Body() dto: Partial<CreateEnfantsDto>,
|
||||
): Promise<EnfantResponseDto> {
|
||||
const enfant = await this.enfantsService.update(id, dto);
|
||||
|
||||
// Mapper l'entité enfant vers le DTO de réponse
|
||||
return mapEnfantToResponseDto(enfant);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
24
src/routes/enfants/enfants.mapper.ts
Normal file
24
src/routes/enfants/enfants.mapper.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Children } from "src/entities/children.entity";
|
||||
import { EnfantResponseDto } from "./dto/enfants_response.dto";
|
||||
|
||||
// Fonction pour mapper une entité Children vers EnfantResponseDto
|
||||
export function mapEnfantToResponseDto(enfant: Children): EnfantResponseDto {
|
||||
return {
|
||||
id: enfant.id,
|
||||
status: enfant.status,
|
||||
first_name: enfant.first_name,
|
||||
last_name: enfant.last_name,
|
||||
gender: enfant.gender,
|
||||
birth_date: enfant.birth_date?.toISOString(),
|
||||
due_date: enfant.due_date?.toISOString(),
|
||||
photo_url: enfant.photo_url,
|
||||
consent_photo: enfant.consent_photo,
|
||||
is_multiple: enfant.is_multiple,
|
||||
parent_id: enfant.parentLinks?.[0]?.parentId ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export function mapEnfantsToResponseDto(enfants: Children[]): EnfantResponseDto[] {
|
||||
return enfants.map(mapEnfantToResponseDto);
|
||||
};
|
||||
|
||||
@ -3,16 +3,10 @@ import { EnfantsController } from './enfants.controller';
|
||||
import { EnfantsService } from './enfants.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Children } from 'src/entities/children.entity';
|
||||
import { Parents } from 'src/entities/parents.entity';
|
||||
import { ParentsChildren } from 'src/entities/parents_children.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Children, Parents, ParentsChildren]),
|
||||
AuthModule
|
||||
|
||||
],
|
||||
imports: [TypeOrmModule.forFeature([Children])],
|
||||
controllers: [EnfantsController],
|
||||
providers: [EnfantsService]
|
||||
providers: [EnfantsService],
|
||||
})
|
||||
export class EnfantsModule { }
|
||||
export class EnfantsModule {}
|
||||
|
||||
@ -1,113 +1,110 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Children, StatutEnfantType } from 'src/entities/children.entity';
|
||||
import { Parents } from 'src/entities/parents.entity';
|
||||
import { ParentsChildren } from 'src/entities/parents_children.entity';
|
||||
import { RoleType, Users } from 'src/entities/users.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||
|
||||
@Injectable()
|
||||
export class EnfantsService {
|
||||
constructor(
|
||||
@InjectRepository(Children)
|
||||
private readonly childrenRepository: Repository<Children>,
|
||||
@InjectRepository(Parents)
|
||||
private readonly parentsRepository: Repository<Parents>,
|
||||
@InjectRepository(ParentsChildren)
|
||||
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
||||
) { }
|
||||
constructor(
|
||||
@InjectRepository(Children)
|
||||
private readonly childrenRepository: Repository<Children>,
|
||||
|
||||
// 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(Parents)
|
||||
private readonly parentsRepository: Repository<Parents>,
|
||||
|
||||
// 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');
|
||||
}
|
||||
@InjectRepository(ParentsChildren)
|
||||
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
||||
) { }
|
||||
|
||||
// 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à');
|
||||
// Inscruire un enfant
|
||||
async create(dto: CreateEnfantsDto): Promise<Children> {
|
||||
|
||||
// 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');
|
||||
// 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`);
|
||||
}
|
||||
break;
|
||||
|
||||
case RoleType.ADMINISTRATEUR:
|
||||
case RoleType.SUPER_ADMIN:
|
||||
case RoleType.GESTIONNAIRE:
|
||||
// accès complet
|
||||
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`);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new ForbiddenException('Accès interdit');
|
||||
// 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);
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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');
|
||||
// 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);
|
||||
}
|
||||
|
||||
await this.childrenRepository.update(id, dto);
|
||||
return this.findOne(id, currentUser);
|
||||
}
|
||||
|
||||
// Suppression
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.childrenRepository.delete(id);
|
||||
}
|
||||
// Supprimer un enfant
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.childrenRepository.delete(id);
|
||||
}
|
||||
}
|
||||
@ -22,17 +22,16 @@ export class ParentsController {
|
||||
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
@Get()
|
||||
@ApiResponse({ status: 200, type: [Parents], description: 'Liste des parents' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
||||
@ApiResponse({ status: 200, description: 'Liste des parents' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé : Réservé aux super_admins' })
|
||||
getAll(): Promise<Parents[]> {
|
||||
return this.parentsService.findAll();
|
||||
}
|
||||
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
@Get(':id')
|
||||
@ApiResponse({ status: 200, type: Parents, description: 'Détails du parent par ID utilisateur' })
|
||||
@ApiResponse({ status: 200, description: 'Détails du parent par ID utilisateur' })
|
||||
@ApiResponse({ status: 404, description: 'Parent non trouvé' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
||||
getOne(@Param('id') user_id: string): Promise<Parents> {
|
||||
return this.parentsService.findOne(user_id);
|
||||
}
|
||||
@ -40,8 +39,7 @@ export class ParentsController {
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
@Post()
|
||||
@ApiBody({ type: CreateParentDto })
|
||||
@ApiResponse({ status: 201, type: Parents, description: 'Parent créé avec succès' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
||||
@ApiResponse({ status: 201, description: 'Parent créé avec succès' })
|
||||
create(@Body() dto: CreateParentDto): Promise<Parents> {
|
||||
return this.parentsService.create(dto);
|
||||
}
|
||||
@ -49,9 +47,8 @@ export class ParentsController {
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
@Patch(':id')
|
||||
@ApiBody({ type: UpdateParentsDto })
|
||||
@ApiResponse({ status: 200, type: Parents, description: 'Parent mis à jour avec succès' })
|
||||
@ApiResponse({ status: 200, description: 'Parent mis à jour avec succès' })
|
||||
@ApiResponse({ status: 404, description: 'Parent introuvable' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé !' })
|
||||
update(@Param('id') id: string, @Body() dto: UpdateParentsDto): Promise<Parents> {
|
||||
return this.parentsService.update(id, dto);
|
||||
}
|
||||
|
||||
@ -9,8 +9,5 @@ import { Users } from 'src/entities/users.entity';
|
||||
imports: [TypeOrmModule.forFeature([Parents, Users])],
|
||||
controllers: [ParentsController],
|
||||
providers: [ParentsService],
|
||||
exports: [ParentsService,
|
||||
TypeOrmModule,
|
||||
],
|
||||
})
|
||||
export class ParentsModule { }
|
||||
export class ParentsModule {}
|
||||
@ -5,21 +5,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Users } from 'src/entities/users.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { Validation } from 'src/entities/validations.entity';
|
||||
import { ParentsModule } from '../parents/parents.module';
|
||||
import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity';
|
||||
import { AssistantesMaternellesModule } from '../assistantes_maternelles/assistantes_maternelles.module';
|
||||
import { Parents } from 'src/entities/parents.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature(
|
||||
[
|
||||
Users,
|
||||
Validation,
|
||||
Parents,
|
||||
AssistanteMaternelle,
|
||||
]), forwardRef(() => AuthModule),
|
||||
ParentsModule,
|
||||
AssistantesMaternellesModule,
|
||||
imports: [TypeOrmModule.forFeature([Users, Validation]),
|
||||
forwardRef(() => AuthModule),
|
||||
],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user