[Ticket #18] API Inscription Parent - Étape 3 (Enfants)
✅ Modifications apportées : - Rendu le champ 'genre' obligatoire dans CreateEnfantsDto (conforme CDC) - Ajout upload photo avec Multer (max 5MB, formats jpg/jpeg/png/gif) - Rattachement automatique au co-parent s'il existe - Création dossier /app/uploads/photos dans Dockerfile avec permissions - Gestion enfants à naître vs nés - Gestion consentement photo avec horodatage ✅ Tests réalisés : - Création enfant 'actif' avec date de naissance (Emma Martin) - Création enfant avec rattachement aux 2 parents (Noah Martin) - Création enfant 'a_naitre' avec date prévue (Léa Martin) - Vérification base de données : enfants bien rattachés aux parents Refs: #18
This commit is contained in:
parent
9aea26805d
commit
dfad408902
@ -32,6 +32,9 @@ COPY --from=builder /app/dist ./dist
|
|||||||
RUN addgroup -g 1001 -S nodejs
|
RUN addgroup -g 1001 -S nodejs
|
||||||
RUN adduser -S nestjs -u 1001
|
RUN adduser -S nestjs -u 1001
|
||||||
|
|
||||||
|
# Créer le dossier uploads et donner les permissions
|
||||||
|
RUN mkdir -p /app/uploads/photos && chown -R nestjs:nodejs /app/uploads
|
||||||
|
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
@ -29,10 +29,10 @@ export class CreateEnfantsDto {
|
|||||||
@MaxLength(100)
|
@MaxLength(100)
|
||||||
last_name?: string;
|
last_name?: string;
|
||||||
|
|
||||||
@ApiProperty({ enum: GenreType, required: false })
|
@ApiProperty({ enum: GenreType })
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(GenreType)
|
@IsEnum(GenreType)
|
||||||
gender?: GenreType;
|
@IsNotEmpty()
|
||||||
|
gender: GenreType;
|
||||||
|
|
||||||
@ApiProperty({ example: '2018-06-24', required: false })
|
@ApiProperty({ example: '2018-06-24', required: false })
|
||||||
@ValidateIf(o => o.status !== StatutEnfantType.A_NAITRE)
|
@ValidateIf(o => o.status !== StatutEnfantType.A_NAITRE)
|
||||||
|
|||||||
@ -8,8 +8,13 @@ import {
|
|||||||
Patch,
|
Patch,
|
||||||
Post,
|
Post,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
|
UseInterceptors,
|
||||||
|
UploadedFile,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
|
import { ApiBearerAuth, ApiTags, ApiConsumes } from '@nestjs/swagger';
|
||||||
|
import { diskStorage } from 'multer';
|
||||||
|
import { extname } from 'path';
|
||||||
import { EnfantsService } from './enfants.service';
|
import { EnfantsService } from './enfants.service';
|
||||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||||
import { UpdateEnfantsDto } from './dto/update_enfants.dto';
|
import { UpdateEnfantsDto } from './dto/update_enfants.dto';
|
||||||
@ -28,8 +33,34 @@ export class EnfantsController {
|
|||||||
|
|
||||||
@Roles(RoleType.PARENT)
|
@Roles(RoleType.PARENT)
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() dto: CreateEnfantsDto, @User() currentUser: Users) {
|
@ApiConsumes('multipart/form-data')
|
||||||
return this.enfantsService.create(dto, currentUser);
|
@UseInterceptors(
|
||||||
|
FileInterceptor('photo', {
|
||||||
|
storage: diskStorage({
|
||||||
|
destination: './uploads/photos',
|
||||||
|
filename: (req, file, cb) => {
|
||||||
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
|
||||||
|
const ext = extname(file.originalname);
|
||||||
|
cb(null, `enfant-${uniqueSuffix}${ext}`);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
fileFilter: (req, file, cb) => {
|
||||||
|
if (!file.mimetype.match(/\/(jpg|jpeg|png|gif)$/)) {
|
||||||
|
return cb(new Error('Seules les images sont autorisées'), false);
|
||||||
|
}
|
||||||
|
cb(null, true);
|
||||||
|
},
|
||||||
|
limits: {
|
||||||
|
fileSize: 5 * 1024 * 1024,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
create(
|
||||||
|
@Body() dto: CreateEnfantsDto,
|
||||||
|
@UploadedFile() photo: Express.Multer.File,
|
||||||
|
@User() currentUser: Users,
|
||||||
|
) {
|
||||||
|
return this.enfantsService.create(dto, currentUser, photo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||||
|
|||||||
@ -24,10 +24,11 @@ export class EnfantsService {
|
|||||||
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
private readonly parentsChildrenRepository: Repository<ParentsChildren>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
// Création d’un enfant
|
// Création d'un enfant
|
||||||
async create(dto: CreateEnfantsDto, currentUser: Users): Promise<Children> {
|
async create(dto: CreateEnfantsDto, currentUser: Users, photoFile?: Express.Multer.File): Promise<Children> {
|
||||||
const parent = await this.parentsRepository.findOne({
|
const parent = await this.parentsRepository.findOne({
|
||||||
where: { user_id: currentUser.id },
|
where: { user_id: currentUser.id },
|
||||||
|
relations: ['co_parent'],
|
||||||
});
|
});
|
||||||
if (!parent) throw new NotFoundException('Parent introuvable');
|
if (!parent) throw new NotFoundException('Parent introuvable');
|
||||||
|
|
||||||
@ -46,17 +47,34 @@ export class EnfantsService {
|
|||||||
});
|
});
|
||||||
if (exist) throw new ConflictException('Cet enfant existe déjà');
|
if (exist) throw new ConflictException('Cet enfant existe déjà');
|
||||||
|
|
||||||
|
// Gestion de la photo uploadée
|
||||||
|
if (photoFile) {
|
||||||
|
dto.photo_url = `/uploads/photos/${photoFile.filename}`;
|
||||||
|
if (dto.consent_photo) {
|
||||||
|
dto.consent_photo_at = new Date().toISOString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Création
|
// Création
|
||||||
const child = this.childrenRepository.create(dto);
|
const child = this.childrenRepository.create(dto);
|
||||||
await this.childrenRepository.save(child);
|
await this.childrenRepository.save(child);
|
||||||
|
|
||||||
// Lien parent-enfant
|
// Lien parent-enfant (Parent 1)
|
||||||
const parentLink = this.parentsChildrenRepository.create({
|
const parentLink = this.parentsChildrenRepository.create({
|
||||||
parentId: parent.user_id,
|
parentId: parent.user_id,
|
||||||
enfantId: child.id,
|
enfantId: child.id,
|
||||||
});
|
});
|
||||||
await this.parentsChildrenRepository.save(parentLink);
|
await this.parentsChildrenRepository.save(parentLink);
|
||||||
|
|
||||||
|
// Rattachement automatique au co-parent s'il existe
|
||||||
|
if (parent.co_parent) {
|
||||||
|
const coParentLink = this.parentsChildrenRepository.create({
|
||||||
|
parentId: parent.co_parent.id,
|
||||||
|
enfantId: child.id,
|
||||||
|
});
|
||||||
|
await this.parentsChildrenRepository.save(coParentLink);
|
||||||
|
}
|
||||||
|
|
||||||
return this.findOne(child.id, currentUser);
|
return this.findOne(child.id, currentUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user