forked from Ynov/ptitspas-ynov-back
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
import {
|
||
BadRequestException,
|
||
ConflictException,
|
||
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 { Users } from 'src/entities/users.entity';
|
||
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>,
|
||
) {}
|
||
|
||
// 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');
|
||
|
||
// 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');
|
||
}
|
||
|
||
// 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à');
|
||
|
||
// 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);
|
||
}
|
||
|
||
// 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): Promise<Children> {
|
||
const child = await this.childrenRepository.findOne({
|
||
where: { id },
|
||
relations: ['parentLinks'],
|
||
});
|
||
if (!child) throw new NotFoundException('Enfant introuvable');
|
||
return child;
|
||
}
|
||
|
||
// Mise à jour
|
||
async update(id: string, dto: Partial<CreateEnfantsDto>): Promise<Children> {
|
||
const child = await this.childrenRepository.findOne({ where: { id } });
|
||
if (!child) throw new NotFoundException('Enfant introuvable');
|
||
|
||
await this.childrenRepository.update(id, dto);
|
||
return this.findOne(id);
|
||
}
|
||
|
||
// Suppression
|
||
async remove(id: string): Promise<void> {
|
||
await this.childrenRepository.delete(id);
|
||
}
|
||
}
|