ptitspas-ynov-back/src/routes/enfants/enfants.service.ts

110 lines
4.1 KiB
TypeScript

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';
@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): Promise<Children> {
// 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`);
}
// 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`);
}
// 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);
}
// 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;
}
// 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);
}
// Supprimer un enfant
async remove(id: string): Promise<void> {
await this.childrenRepository.delete(id);
}
}