feat(platform): add platform tenant management, provisioning DTOs, metrics & support impersonation headers
This commit is contained in:
@@ -17,6 +17,10 @@ axiosInstance.interceptors.request.use(
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
const impersonatedTenantId = localStorage.getItem('impersonatedTenantId');
|
||||
if (impersonatedTenantId) {
|
||||
config.headers['X-Impersonated-Tenant-Id'] = impersonatedTenantId;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error: unknown) => Promise.reject(error)
|
||||
|
||||
@@ -70,14 +70,80 @@ export function useTenant() {
|
||||
}
|
||||
};
|
||||
|
||||
const fetchPlatformTenants = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const data = await tenantService.getPlatformTenants();
|
||||
setTenants(data);
|
||||
return data;
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch platform tenants');
|
||||
notify.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const provisionTenant = async (data: any) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const result = await tenantService.provisionTenant(data);
|
||||
notify.success('Tenant provisioned successfully with Admin credentials');
|
||||
return result;
|
||||
} catch (err) {
|
||||
notify.error(err);
|
||||
throw err;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const updatePlatformStatus = async (id: string, status: boolean) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const updated = await tenantService.updatePlatformStatus(id, status);
|
||||
setTenants(prev => prev.map(t => t.id === id ? { ...t, status: updated.status } : t));
|
||||
notify.success(`Tenant status updated to ${updated.status ? 'Active' : 'Suspended'}`);
|
||||
return updated;
|
||||
} catch (err) {
|
||||
notify.error(err);
|
||||
throw err;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const impersonateTenant = async (tenantId: string) => {
|
||||
try {
|
||||
const result = await tenantService.impersonateTenant(tenantId);
|
||||
localStorage.setItem('impersonatedTenantId', tenantId);
|
||||
notify.success(result.message || 'Support impersonation active');
|
||||
return result;
|
||||
} catch (err) {
|
||||
notify.error(err);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
const stopImpersonation = () => {
|
||||
localStorage.removeItem('impersonatedTenantId');
|
||||
notify.info('Support impersonation ended');
|
||||
};
|
||||
|
||||
return {
|
||||
tenants,
|
||||
loading,
|
||||
error,
|
||||
fetchTenants,
|
||||
fetchPlatformTenants,
|
||||
getTenant,
|
||||
createTenant,
|
||||
provisionTenant,
|
||||
updateTenant,
|
||||
deleteTenant
|
||||
updatePlatformStatus,
|
||||
deleteTenant,
|
||||
impersonateTenant,
|
||||
stopImpersonation
|
||||
};
|
||||
}
|
||||
|
||||
@@ -25,5 +25,31 @@ export const tenantService = {
|
||||
delete: async (id: string) => {
|
||||
const response = await api.delete(`/api/v1/tenants/${id}`);
|
||||
return (response as any).data;
|
||||
},
|
||||
|
||||
// Platform Admin Endpoints
|
||||
getPlatformTenants: async () => {
|
||||
const response = await api.get('/api/v1/platform/tenants');
|
||||
return (response as any).data;
|
||||
},
|
||||
|
||||
provisionTenant: async (data: any) => {
|
||||
const response = await api.post('/api/v1/platform/tenants', data);
|
||||
return (response as any).data;
|
||||
},
|
||||
|
||||
updatePlatformStatus: async (id: string, status: boolean) => {
|
||||
const response = await api.patch(`/api/v1/platform/tenants/${id}/status`, { status });
|
||||
return (response as any).data;
|
||||
},
|
||||
|
||||
getPlatformMetrics: async () => {
|
||||
const response = await api.get('/api/v1/platform/metrics');
|
||||
return (response as any).data;
|
||||
},
|
||||
|
||||
impersonateTenant: async (tenantId: string) => {
|
||||
const response = await api.post(`/api/v1/platform/impersonate/${tenantId}`);
|
||||
return (response as any).data;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,3 +25,18 @@ export interface UpdateTenantDTO {
|
||||
mobile?: string;
|
||||
status?: boolean;
|
||||
}
|
||||
|
||||
export interface PlatformTenant extends Tenant {
|
||||
total_products?: number;
|
||||
total_assets?: number;
|
||||
total_users?: number;
|
||||
}
|
||||
|
||||
export interface ProvisionTenantDTO {
|
||||
tenant_name: string;
|
||||
domain?: string;
|
||||
contact_email: string;
|
||||
admin_name?: string;
|
||||
admin_email?: string;
|
||||
admin_password?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user