remove route enfant
This commit is contained in:
parent
80bc815d20
commit
bdf43b6a96
@ -1,57 +0,0 @@
|
|||||||
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 })
|
|
||||||
@IsEnum(StatutEnfantType)
|
|
||||||
@IsNotEmpty()
|
|
||||||
status: StatutEnfantType;
|
|
||||||
|
|
||||||
@ApiProperty({ example: 'Georges', required: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
@MaxLength(100)
|
|
||||||
first_name?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ example: 'Lucas', required: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
@MaxLength(100)
|
|
||||||
last_name?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ enum: GenreType, required: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(GenreType)
|
|
||||||
gender?: GenreType;
|
|
||||||
|
|
||||||
@ApiProperty({ example: '2018-06-24', required: false })
|
|
||||||
@ValidateIf(o => o.status !== StatutEnfantType.A_NAITRE)
|
|
||||||
@IsOptional()
|
|
||||||
@IsDateString()
|
|
||||||
birth_date?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ example: '2024-12-24', required: false })
|
|
||||||
@ValidateIf(o => o.status === StatutEnfantType.A_NAITRE)
|
|
||||||
@IsOptional()
|
|
||||||
@IsDateString()
|
|
||||||
due_date?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ example: 'https://monimage.com/photo.jpg', required: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
photo_url?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ default: false })
|
|
||||||
@IsBoolean()
|
|
||||||
consent_photo: boolean;
|
|
||||||
|
|
||||||
@ApiProperty({ required: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsDateString()
|
|
||||||
consent_photo_at?: string;
|
|
||||||
|
|
||||||
@ApiProperty({ default: false })
|
|
||||||
@IsBoolean()
|
|
||||||
is_multiple: boolean;
|
|
||||||
}
|
|
||||||
@ -1,38 +0,0 @@
|
|||||||
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: '2024-12-24', 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 +0,0 @@
|
|||||||
import { PartialType } from "@nestjs/swagger";
|
|
||||||
import { CreateEnfantsDto } from "./create_enfants.dto";
|
|
||||||
|
|
||||||
export class UpdateEnfantsDto extends PartialType(CreateEnfantsDto) {}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { EnfantsController } from './enfants.controller';
|
|
||||||
|
|
||||||
describe('EnfantsController', () => {
|
|
||||||
let controller: EnfantsController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [EnfantsController],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<EnfantsController>(EnfantsController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,69 +0,0 @@
|
|||||||
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, Users } from 'src/entities/users.entity';
|
|
||||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
|
||||||
import { EnfantResponseDto } from './dto/enfants_response.dto';
|
|
||||||
import { mapEnfantsToResponseDto, mapEnfantToResponseDto } from './enfants.mapper';
|
|
||||||
import { AuthGuard } from 'src/common/guards/auth.guard';
|
|
||||||
import { User } from 'src/common/decorators/user.decorator';
|
|
||||||
|
|
||||||
@ApiBearerAuth('access-token')
|
|
||||||
@ApiTags('Enfants')
|
|
||||||
@UseGuards(AuthGuard, RolesGuard)
|
|
||||||
@Controller('enfants')
|
|
||||||
export class EnfantsController {
|
|
||||||
constructor(private readonly enfantsService: EnfantsService) { }
|
|
||||||
|
|
||||||
@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,
|
|
||||||
@User() currentUser: Users, // <-- ton décorateur
|
|
||||||
): Promise<EnfantResponseDto> {
|
|
||||||
const enfant = await this.enfantsService.create(dto, currentUser);
|
|
||||||
return mapEnfantToResponseDto(enfant);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get()
|
|
||||||
@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.' })
|
|
||||||
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
|
||||||
async findAll(): Promise<EnfantResponseDto[]> {
|
|
||||||
const enfants = await this.enfantsService.findAll();
|
|
||||||
return mapEnfantsToResponseDto(enfants);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
return mapEnfantToResponseDto(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);
|
|
||||||
return mapEnfantToResponseDto(enfant);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
@ApiOperation({ summary: 'Supprimer un enfant' })
|
|
||||||
@ApiResponse({ status: 204, description: 'L\'enfant a été supprimé avec succès.' })
|
|
||||||
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
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
|
|
||||||
],
|
|
||||||
controllers: [EnfantsController],
|
|
||||||
providers: [EnfantsService],
|
|
||||||
})
|
|
||||||
export class EnfantsModule { }
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { EnfantsService } from './enfants.service';
|
|
||||||
|
|
||||||
describe('EnfantsService', () => {
|
|
||||||
let service: EnfantsService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [EnfantsService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<EnfantsService>(EnfantsService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,106 +0,0 @@
|
|||||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
||||||
import { InjectRepository } from '@nestjs/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 { CreateEnfantsDto } from './dto/create_enfants.dto';
|
|
||||||
import { Users } from 'src/entities/users.entity';
|
|
||||||
|
|
||||||
@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>,
|
|
||||||
) { }
|
|
||||||
|
|
||||||
// Inscruire un enfant
|
|
||||||
async create(dto: CreateEnfantsDto, currentUser: Users): Promise<Children> {
|
|
||||||
// Récupérer le parent lié à l'utilisateur connecté
|
|
||||||
const parent = await this.parentsRepository.findOne({
|
|
||||||
where: { user_id: currentUser.id },
|
|
||||||
});
|
|
||||||
if (!parent) {
|
|
||||||
throw new NotFoundException(`Parent introuvable pour l'utilisateur connecté`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vérifier la règle métier sur la photo
|
|
||||||
const optionObligatoire = false;
|
|
||||||
if (dto.status !== StatutEnfantType.A_NAITRE && !dto.photo_url && optionObligatoire) {
|
|
||||||
throw new BadRequestException(`Pour un enfant actif, une photo est obligatoire`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Créer l'enfant
|
|
||||||
const child = this.childrenRepository.create({
|
|
||||||
status: dto.status,
|
|
||||||
first_name: dto.first_name,
|
|
||||||
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 automatiquement avec le parent connecté
|
|
||||||
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
|
|
||||||
async findAll(): Promise<Children[]> {
|
|
||||||
const all_children = await this.childrenRepository.find({
|
|
||||||
relations: [
|
|
||||||
'parentLinks',
|
|
||||||
'parentLinks.parent',
|
|
||||||
],
|
|
||||||
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'], // charge aussi le parent
|
|
||||||
});
|
|
||||||
if (!child) {
|
|
||||||
throw new NotFoundException(`Id d'enfant : ${id} introuvable`);
|
|
||||||
}
|
|
||||||
child.parentLinks = child.parentLinks ?? [];
|
|
||||||
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`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// On n’a plus besoin de gérer id_parent ici
|
|
||||||
await this.childrenRepository.update(id, dto);
|
|
||||||
return this.findOne(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Supprimer un enfant
|
|
||||||
async remove(id: string): Promise<void> {
|
|
||||||
await this.childrenRepository.delete(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user