/** * useTenantActions Hook * * React hook for tenant creation via TanStack Query mutation. * Tenants are immutable after creation — no update or delete operations. * * @layer Presentation */ import { useMutation, useQueryClient } from '@tanstack/react-query'; import { queryKeys } from '@/lib/query-keys'; import { Tenant, TenantStatus } from '@/domain/entities/Tenant'; import { authenticatedUserStore } from '@/infrastructure/storage/AuthenticatedUserStore'; /** * useCreateTenant Hook * * Creates a new tenant with automatic cache invalidation. */ interface CreateTenantResult { tenant: Tenant; productionKey?: string; } export function useCreateTenant() { const queryClient = useQueryClient(); const companyId = authenticatedUserStore.get()?.companyId || ''; return useMutation({ retry: false, mutationFn: async (data: { name: string; domain: string; status: TenantStatus; }): Promise => { const path = `/companies/${companyId}/tenants`; const { apiClient } = await import('@/infrastructure/http/ApiClient'); const response = await apiClient.post>(path, { companyId, name: data.name, domain: data.domain, status: data.status, }); const { TenantResponseMapper } = await import( '@/infrastructure/http/api/tenant/mappers/TenantResponseMapper' ); const createdTenant = TenantResponseMapper.toTenant(response); const productionKey = (response as any)?.productionKey?.key as string | undefined; return { tenant: createdTenant, productionKey }; }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: queryKeys.tenants.lists() }); }, }); }