feat(backend): add responsable anomalies status cycle
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { AppError } from '../errors/app-error';
|
||||
import {
|
||||
changerStatutAnomalieResponsable,
|
||||
countAnomaliesParStatut,
|
||||
countEmpruntsParStatut,
|
||||
countMaterielsParStatut,
|
||||
countNotificationsNonLues,
|
||||
findActiviteRecente,
|
||||
findAnomalieResponsableById,
|
||||
findAnomaliesResponsable,
|
||||
findEmpruntsResponsable,
|
||||
findHistoriqueResponsable,
|
||||
@@ -65,6 +67,18 @@ export interface ListerHistoriqueResponsableFiltres {
|
||||
dateFin?: Date;
|
||||
}
|
||||
|
||||
export interface ChangerStatutAnomalieResponsableRequest {
|
||||
statut: StatutAnomalie;
|
||||
observation?: string;
|
||||
}
|
||||
|
||||
const TRANSITIONS_ANOMALIE: Record<StatutAnomalie, StatutAnomalie[]> = {
|
||||
DETECTEE: ['EN_COURS_TRAITEMENT'],
|
||||
EN_COURS_TRAITEMENT: ['RESOLUE'],
|
||||
RESOLUE: ['CLOTUREE'],
|
||||
CLOTUREE: [],
|
||||
};
|
||||
|
||||
function verifierResponsable(roleCode: string): void {
|
||||
if (roleCode !== 'RESPONSABLE') {
|
||||
throw new AppError(403, 'Acces reserve au responsable materiel');
|
||||
@@ -153,6 +167,19 @@ export function parseNotificationId(value: unknown): number {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function parseAnomalieId(value: unknown): number {
|
||||
if (typeof value !== 'string') {
|
||||
throw new AppError(400, 'anomalieId invalide');
|
||||
}
|
||||
|
||||
const parsed = Number(value);
|
||||
if (!Number.isInteger(parsed) || parsed <= 0) {
|
||||
throw new AppError(400, 'anomalieId invalide');
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function parsePositiveIntQuery(value: unknown, champ: string): number | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
@@ -264,3 +291,31 @@ export function listerHistoriqueResponsable(
|
||||
verifierResponsable(roleCode);
|
||||
return findHistoriqueResponsable(campusId, filtres);
|
||||
}
|
||||
|
||||
export async function changerStatutAnomalieResponsableService(
|
||||
responsableId: number,
|
||||
roleCode: string,
|
||||
campusId: number,
|
||||
anomalieId: number,
|
||||
data: ChangerStatutAnomalieResponsableRequest,
|
||||
): Promise<AnomalieResponsable> {
|
||||
verifierResponsable(roleCode);
|
||||
|
||||
const anomalie = await findAnomalieResponsableById(campusId, anomalieId);
|
||||
if (!anomalie) {
|
||||
throw new AppError(404, 'Anomalie introuvable');
|
||||
}
|
||||
|
||||
const statutActuel = anomalie.statut as StatutAnomalie;
|
||||
if (!TRANSITIONS_ANOMALIE[statutActuel]?.includes(data.statut)) {
|
||||
throw new AppError(400, `Transition invalide depuis ${anomalie.statut} vers ${data.statut}`);
|
||||
}
|
||||
|
||||
return changerStatutAnomalieResponsable({
|
||||
anomalieId,
|
||||
campusId,
|
||||
responsableId,
|
||||
statut: data.statut,
|
||||
observation: data.observation,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user