Proper handling of reservation validation errors
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-06-22 09:03:00 +02:00
parent f6d8d6b3d1
commit 136ed8121e
2 changed files with 25 additions and 3 deletions

View File

@ -2,6 +2,12 @@ import { APIClient } from "../ApiClient";
import { Family } from "../FamilyApi";
import { Accommodation } from "./AccommodationListApi";
export enum ValidateResaResult {
Success,
Error,
Conflict,
}
export interface AccommodationReservation {
id: number;
family_id: number;
@ -151,13 +157,19 @@ export class AccommodationsReservationsApi {
static async Validate(
r: AccommodationReservation,
accept: boolean
): Promise<void> {
await APIClient.exec({
): Promise<ValidateResaResult> {
const res = await APIClient.exec({
method: "POST",
uri: `/family/${r.family_id}/accommodations/reservation/${r.id}/validate`,
jsonData: {
validate: accept,
},
allowFail: true,
});
if (res.status >= 200 && res.status <= 299)
return ValidateResaResult.Success;
if (res.status === 409) return ValidateResaResult.Conflict;
return ValidateResaResult.Error;
}
}

View File

@ -34,6 +34,7 @@ import {
AccommodationReservation,
AccommodationsReservationsApi,
AccommodationsReservationsList,
ValidateResaResult,
} from "../../../api/accommodations/AccommodationsReservationsApi";
import { useAlert } from "../../../hooks/context_providers/AlertDialogProvider";
import { useConfirm } from "../../../hooks/context_providers/ConfirmDialogProvider";
@ -186,9 +187,18 @@ export function AccommodationsReservationsRoute(): React.ReactElement {
try {
loadingMessage.show("Validation de la réservation en cours...");
setValidateResaAnchorEl(null);
setActiveEvent(undefined);
await AccommodationsReservationsApi.Validate(r, validate);
const res = await AccommodationsReservationsApi.Validate(r, validate);
if (res === ValidateResaResult.Conflict) {
throw new Error(
"The reservation is in conflict with other reservations!"
);
} else if (res === ValidateResaResult.Error) {
throw new Error("Failed to validate the reservation!");
}
reload();
snackbar("La réservation a été mise à jour avec succès !");