feat(backend): expose POST /api/emprunts with departure checklist

This commit is contained in:
SaidSoighiri94
2026-07-06 09:46:03 +02:00
parent d8f044924e
commit 3215a3c11f
9 changed files with 267 additions and 11 deletions
+55 -3
View File
@@ -1,7 +1,59 @@
import { findEnCoursParUtilisateur, EmpruntAvecMateriel } from '../repositories/emprunt.repository';
import { AppError } from '../errors/app-error';
import { CreerEmpruntRequest } from '../dtos/emprunt.dto';
import {
findEnCoursParUtilisateur,
creerEmpruntComplet,
EmpruntAvecMateriel,
} from '../repositories/emprunt.repository';
import { findById as findMaterielById } from '../repositories/materiel.repository';
import { findById as findPosteById } from '../repositories/poste-emprunt.repository';
/* RG15 : un étudiant ne consulte que ses propres emprunts, et uniquement ceux
non encore restitués (en cours ou en retard). */
const DUREE_EMPRUNT_JOURS = 14;
/* RG15 : un étudiant ne consulte que ses propres emprunts non restitués. */
export function listerMesEmpruntsEnCours(utilisateurId: number): Promise<EmpruntAvecMateriel[]> {
return findEnCoursParUtilisateur(utilisateurId);
}
/* RG07/RG10/RG11/RG12 : création d'un emprunt avec sa checklist de départ.
Les contrôles d'éligibilité (campus, disponibilité) sont faits ici ; l'écriture
atomique est déléguée au repository. */
export async function creerEmprunt(
utilisateurId: number,
campusId: number,
request: CreerEmpruntRequest,
): Promise<EmpruntAvecMateriel> {
const materiel = await findMaterielById(request.materielId);
if (!materiel) {
throw new AppError(404, 'Materiel introuvable');
}
if (materiel.campusId !== campusId) {
throw new AppError(403, 'Materiel rattache a un autre campus');
}
if (materiel.statut !== 'DISPONIBLE') {
throw new AppError(409, 'Materiel non disponible');
}
const poste = await findPosteById(request.posteEmpruntId);
if (!poste) {
throw new AppError(400, 'Poste introuvable');
}
if (poste.sallePret.campusId !== campusId) {
throw new AppError(403, 'Poste rattache a un autre campus');
}
const dateRetourPrevue = new Date();
dateRetourPrevue.setDate(dateRetourPrevue.getDate() + DUREE_EMPRUNT_JOURS);
return creerEmpruntComplet({
utilisateurId,
materielId: request.materielId,
campusId,
sallePretId: poste.sallePretId,
posteEmpruntId: request.posteEmpruntId,
dateRetourPrevue,
modeIdentification: request.modeIdentification,
commentaireDepart: request.commentaireDepart,
elements: request.elements,
});
}