Files
EME_APP/eme-backend/src/controllers/emprunt.controller.ts
T
2026-07-24 13:41:40 +02:00

68 lines
2.1 KiB
TypeScript

import { Request, Response } from 'express';
import { AppError } from '../errors/app-error';
import {
creerEmprunt,
listerMesEmpruntsEnCours,
restituerEmprunt,
signalerTentativeModificationRetour,
} from '../services/emprunt.service';
import {
parseCreerEmpruntRequest,
parseRestituerEmpruntRequest,
parseSignalerTentativeRetourRequest,
toEmpruntResponse,
} from '../dtos/emprunt.dto';
export async function getMesEmprunts(req: Request, res: Response): Promise<void> {
const user = req.user;
if (!user) {
throw new AppError(401, 'Authentification requise');
}
const emprunts = await listerMesEmpruntsEnCours(user.id);
res.json({ data: emprunts.map(toEmpruntResponse) });
}
export async function postEmprunt(req: Request, res: Response): Promise<void> {
const user = req.user;
if (!user) {
throw new AppError(401, 'Authentification requise');
}
const request = parseCreerEmpruntRequest(req.body);
const emprunt = await creerEmprunt(user.id, user.campusId, request);
res.status(201).json({ data: toEmpruntResponse(emprunt) });
}
export async function postRestitution(req: Request, res: Response): Promise<void> {
const user = req.user;
if (!user) {
throw new AppError(401, 'Authentification requise');
}
const empruntId = Number(req.params.id);
if (!Number.isInteger(empruntId) || empruntId <= 0) {
throw new AppError(400, 'Identifiant emprunt invalide');
}
const request = parseRestituerEmpruntRequest(req.body);
const emprunt = await restituerEmprunt(user.id, empruntId, request);
res.json({ data: toEmpruntResponse(emprunt) });
}
export async function postAlerteChangementRetour(req: Request, res: Response): Promise<void> {
const user = req.user;
if (!user) {
throw new AppError(401, 'Authentification requise');
}
const empruntId = Number(req.params.id);
if (!Number.isInteger(empruntId) || empruntId <= 0) {
throw new AppError(400, 'Identifiant emprunt invalide');
}
const request = parseSignalerTentativeRetourRequest(req.body);
const alerteCreee = await signalerTentativeModificationRetour(user.id, empruntId, request);
res.json({ data: { alerteCreee } });
}