feat(backend): add responsable historique endpoint
This commit is contained in:
@@ -3,6 +3,7 @@ import { AppError } from '../errors/app-error';
|
||||
import {
|
||||
getDashboardResponsable,
|
||||
listerAnomaliesResponsable,
|
||||
listerHistoriqueResponsable,
|
||||
listerNotificationsResponsable,
|
||||
listerMaterielsResponsable,
|
||||
listerEmpruntsResponsable,
|
||||
@@ -11,6 +12,8 @@ import {
|
||||
parseCategorieId,
|
||||
parseLu,
|
||||
parseNotificationId,
|
||||
parseDateQuery,
|
||||
parsePositiveIntQuery,
|
||||
parseStatutAnomalie,
|
||||
parseStatutMateriel,
|
||||
parseStatutEmprunt,
|
||||
@@ -19,10 +22,31 @@ import {
|
||||
toAnomalieResponsableResponse,
|
||||
toDashboardResponsableResponse,
|
||||
toEmpruntResponsableResponse,
|
||||
toHistoriqueResponsableResponse,
|
||||
toMaterielResponsableResponse,
|
||||
toNotificationResponsableResponse,
|
||||
} from '../dtos/responsable.dto';
|
||||
|
||||
function getHistoriqueFiltres(req: Request) {
|
||||
const action = typeof req.query.action === 'string' ? req.query.action : undefined;
|
||||
return {
|
||||
action,
|
||||
utilisateurId: parsePositiveIntQuery(req.query.utilisateurId, 'utilisateurId'),
|
||||
materielId: parsePositiveIntQuery(req.query.materielId, 'materielId'),
|
||||
empruntId: parsePositiveIntQuery(req.query.empruntId, 'empruntId'),
|
||||
dateDebut: parseDateQuery(req.query.dateDebut, 'dateDebut'),
|
||||
dateFin: parseDateQuery(req.query.dateFin, 'dateFin'),
|
||||
};
|
||||
}
|
||||
|
||||
function escapeCsv(value: string | number | Date | null): string {
|
||||
if (value === null) {
|
||||
return '';
|
||||
}
|
||||
const raw = value instanceof Date ? value.toISOString() : String(value);
|
||||
return `"${raw.replace(/"/g, '""')}"`;
|
||||
}
|
||||
|
||||
export async function getDashboard(req: Request, res: Response): Promise<void> {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
@@ -113,3 +137,53 @@ export async function patchNotificationsLues(req: Request, res: Response): Promi
|
||||
const count = await marquerToutesNotificationsResponsableLues(user.id, user.roleCode);
|
||||
res.json({ data: { count } });
|
||||
}
|
||||
|
||||
export async function getHistorique(req: Request, res: Response): Promise<void> {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
throw new AppError(401, 'Authentification requise');
|
||||
}
|
||||
|
||||
const historique = await listerHistoriqueResponsable(
|
||||
user.roleCode,
|
||||
user.campusId,
|
||||
getHistoriqueFiltres(req),
|
||||
);
|
||||
res.json({ data: historique.map(toHistoriqueResponsableResponse) });
|
||||
}
|
||||
|
||||
export async function getHistoriqueCsv(req: Request, res: Response): Promise<void> {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
throw new AppError(401, 'Authentification requise');
|
||||
}
|
||||
|
||||
const historique = await listerHistoriqueResponsable(
|
||||
user.roleCode,
|
||||
user.campusId,
|
||||
getHistoriqueFiltres(req),
|
||||
);
|
||||
const lignes = [
|
||||
['id', 'dateAction', 'action', 'description', 'utilisateur', 'email', 'materiel', 'empruntId']
|
||||
.map(escapeCsv)
|
||||
.join(','),
|
||||
...historique.map((item) =>
|
||||
[
|
||||
item.id,
|
||||
item.dateAction,
|
||||
item.action,
|
||||
item.description,
|
||||
`${item.utilisateur.prenom} ${item.utilisateur.nom}`,
|
||||
item.utilisateur.email,
|
||||
item.materiel ? `${item.materiel.nom} (${item.materiel.reference})` : null,
|
||||
item.empruntId,
|
||||
]
|
||||
.map(escapeCsv)
|
||||
.join(','),
|
||||
),
|
||||
];
|
||||
|
||||
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="historique-responsable.csv"');
|
||||
res.send(lignes.join('\n'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user