xpeditis2.0/apps/frontend/src/hooks/useReservationQuota.ts
David 0dc56ff1eb feat(bookings): reservation quota based on org plan + PAID bookings
- Nouvel endpoint GET /csv-bookings/reservation-quota: limite = vrai plan de
  l'org (pas l'override ADMIN PLATINIUM), used = bookings PAYES de l'annee
  (PENDING_BANK_TRANSFER/PENDING/ACCEPTED), limitReached.
- ShipmentCounter: countPaidShipmentsForOrganizationInYear.
- create-check compte desormais les payes (au lieu de tous).
- Frontend useReservationQuota consomme l'endpoint (fiable meme pour un admin
  dont l'overview renvoie PLATINIUM).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 17:36:15 +02:00

41 lines
1.2 KiB
TypeScript

'use client';
import { useQuery } from '@tanstack/react-query';
import { getReservationQuota } from '@/lib/api/bookings';
import { useAuth } from '@/lib/context/auth-context';
/**
* Reservation (paid shipment) quota for the current plan.
*
* The free (Bronze) plan is limited to a number of PAID reservations per year
* (`maxShipmentsPerYear`, e.g. 5); higher plans are unlimited. The value comes
* from the backend, which uses the organization's real plan (not the ADMIN
* PLATINIUM override) and counts only paid shipments — so the UI can prompt an
* upgrade once the limit is reached instead of failing at the end of the flow.
*/
export function useReservationQuota() {
const { isAuthenticated } = useAuth();
const { data, isLoading } = useQuery({
queryKey: ['reservation-quota'],
queryFn: getReservationQuota,
enabled: isAuthenticated,
staleTime: 30_000,
});
const max = data?.max ?? -1;
const used = data?.used ?? 0;
const unlimited = data?.unlimited ?? true;
const limitReached = data?.limitReached ?? false;
return {
loading: isLoading,
max,
used,
remaining: unlimited ? Infinity : Math.max(0, max - used),
unlimited,
limitReached,
blocked: limitReached,
};
}