diff --git a/.gitignore b/.gitignore index 834d3b4..410379a 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/src/routes/enfants/dto/enfants_response.dto.ts b/src/routes/enfants/dto/enfants_response.dto.ts new file mode 100644 index 0000000..724bfe0 --- /dev/null +++ b/src/routes/enfants/dto/enfants_response.dto.ts @@ -0,0 +1,38 @@ +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; + +} \ No newline at end of file diff --git a/src/routes/enfants/enfants.controller.ts b/src/routes/enfants/enfants.controller.ts index bd14a24..022e910 100644 --- a/src/routes/enfants/enfants.controller.ts +++ b/src/routes/enfants/enfants.controller.ts @@ -6,6 +6,8 @@ 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() @ApiTags('Enfants') @@ -13,43 +15,55 @@ import { CreateEnfantsDto } from './dto/create_enfants.dto'; @Controller('enfants') export class EnfantsController { constructor(private readonly enfantsService: EnfantsService) { } - + // Inscrire un enfant @Post() @ApiOperation({ summary: 'Inscrire un enfant' }) - @ApiResponse({ status: 201, type: Children, description: 'L\'enfant a été inscrit avec succès.' }) + @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 { - return this.enfantsService.create(dto); - } - + async create(@Body() dto: CreateEnfantsDto): Promise { + 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: [Children], description: 'Liste de 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 { - return this.enfantsService.findAll(); + async findAll(): Promise { + const enfants = await this.enfantsService.findAll(); + + // Mapper les entités enfants vers les DTO de réponse + return mapEnfantsToResponseDto(enfants); } // 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.' }) + @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 { - return this.enfantsService.findOne(id); + async findOne(@Param('id', new ParseUUIDPipe()) id: string): Promise { + const enfant = await this.enfantsService.findOne(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: Children, description: 'L\'enfant a été mis à jour avec succès.' }) + @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, - ): Promise { - return this.enfantsService.update(id, dto); + ): Promise { + const enfant = await this.enfantsService.update(id, dto); + + // Mapper l'entité enfant vers le DTO de réponse + return mapEnfantToResponseDto(enfant); } // Supprimer un enfant diff --git a/src/routes/enfants/enfants.mapper.ts b/src/routes/enfants/enfants.mapper.ts new file mode 100644 index 0000000..0fab245 --- /dev/null +++ b/src/routes/enfants/enfants.mapper.ts @@ -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); +}; +