TorbatYar/frontend/modules/delivery/hooks/useDeliveryPermissions.ts
Mortezakoohjani 7978970783 feat(delivery): add Torbat Driver frontend module with real API wiring.
Ship the delivery platform UI under modules/delivery with thin app routes, BFF proxy, CRUD for phase 10.0-10.1 APIs, and capability-gated future screens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-27 12:02:01 +03:30

46 lines
1.5 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import { useTenantId } from "@/hooks/useTenantId";
import { useMe } from "@/hooks/useMe";
import { deliveryApi } from "@/modules/delivery/services/delivery-api";
/** Permission checks — backend is authoritative; owner gets full access in UI. */
export function useDeliveryPermissions() {
const { tenantId } = useTenantId();
const { me } = useMe();
const catalogQ = useQuery({
queryKey: ["delivery", tenantId, "permissions-catalog"],
queryFn: () => deliveryApi.permissions.catalog(tenantId!),
enabled: !!tenantId,
staleTime: 5 * 60_000,
});
const membership = me?.memberships.find((m) => m.tenant_id === tenantId);
const isOwner = membership?.is_owner ?? false;
const isPlatformAdmin = me?.platform_role === "platform_admin";
const can = (permission?: string) => {
if (!permission) return true;
if (isOwner || isPlatformAdmin) return true;
if (permission.endsWith(".view") || permission === "delivery.view") return !!tenantId;
return false;
};
const canManage = (viewPerm?: string, managePerm?: string) => {
if (isOwner || isPlatformAdmin) return true;
if (managePerm) return can(managePerm);
if (viewPerm) return can(viewPerm.replace(".view", ".manage"));
return can("delivery.manage");
};
return {
can,
canManage,
isOwner,
catalog: catalogQ.data,
isLoading: catalogQ.isLoading,
};
}