From acd956db50d8b078ca0f9c84f19abee29be2a7d7 Mon Sep 17 00:00:00 2001 From: Yashwin Date: Wed, 4 Feb 2026 17:56:03 +0530 Subject: [PATCH] Refactor user service to streamline getAllUsers function and enhance code clarity. Update NewModuleModal by removing commented health_status field and adjusting error handling for improved validation. This change simplifies the module creation process and maintains consistency in data handling. --- src/components/superadmin/NewModuleModal.tsx | 5 +- src/services/user-service.ts | 59 +++++++++----------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/components/superadmin/NewModuleModal.tsx b/src/components/superadmin/NewModuleModal.tsx index 78990a9..8c7fb7b 100644 --- a/src/components/superadmin/NewModuleModal.tsx +++ b/src/components/superadmin/NewModuleModal.tsx @@ -49,7 +49,6 @@ const newModuleSchema = z.object({ min_replicas: z.number().int().min(1, 'min_replicas must be at least 1').max(50, 'min_replicas must be at most 50').optional().nullable(), max_replicas: z.number().int().min(1, 'max_replicas must be at least 1').max(50, 'max_replicas must be at most 50').optional().nullable(), last_health_check: z.string().optional().nullable(), - // health_status: z.string().max(20, 'health_status must be at most 20 characters').optional().nullable(), consecutive_failures: z.number().int().optional().nullable(), registered_by: z.string().uuid().optional().nullable(), tenant_id: z.string().uuid().optional().nullable(), @@ -96,7 +95,6 @@ export const NewModuleModal = ({ min_replicas: null, max_replicas: null, last_health_check: null, - // health_status: null, consecutive_failures: null, registered_by: null, tenant_id: null, @@ -126,7 +124,6 @@ export const NewModuleModal = ({ min_replicas: null, max_replicas: null, last_health_check: null, - // health_status: null, consecutive_failures: null, registered_by: null, tenant_id: null, @@ -158,7 +155,7 @@ export const NewModuleModal = ({ if (error?.response?.data?.details && Array.isArray(error.response.data.details)) { const validationErrors = error.response.data.details; validationErrors.forEach((detail: { path: string; message: string }) => { - if (detail.path === 'name' || detail.path === 'module_id' || detail.path === 'description' || detail.path === 'version' || detail.path === 'runtime_language' || detail.path === 'framework' || detail.path === 'webhookurl' || detail.path === 'base_url' || detail.path === 'health_endpoint' || detail.path === 'endpoints' || detail.path === 'kafka_topics' || detail.path === 'cpu_request' || detail.path === 'cpu_limit' || detail.path === 'memory_request' || detail.path === 'memory_limit' || detail.path === 'min_replicas' || detail.path === 'max_replicas' || detail.path === 'last_health_check' || detail.path === 'health_status' || detail.path === 'consecutive_failures' || detail.path === 'registered_by' || detail.path === 'tenant_id' || detail.path === 'metadata') { + if (detail.path === 'name' || detail.path === 'module_id' || detail.path === 'description' || detail.path === 'version' || detail.path === 'runtime_language' || detail.path === 'framework' || detail.path === 'webhookurl' || detail.path === 'base_url' || detail.path === 'health_endpoint' || detail.path === 'endpoints' || detail.path === 'kafka_topics' || detail.path === 'cpu_request' || detail.path === 'cpu_limit' || detail.path === 'memory_request' || detail.path === 'memory_limit' || detail.path === 'min_replicas' || detail.path === 'max_replicas' || detail.path === 'last_health_check' || detail.path === 'consecutive_failures' || detail.path === 'registered_by' || detail.path === 'tenant_id' || detail.path === 'metadata') { setError(detail.path as keyof NewModuleFormData, { type: 'server', message: detail.message, diff --git a/src/services/user-service.ts b/src/services/user-service.ts index cae8295..7f20e4f 100644 --- a/src/services/user-service.ts +++ b/src/services/user-service.ts @@ -9,27 +9,33 @@ import type { DeleteUserResponse, } from '@/types/user'; +const getAllUsers = async ( + page: number = 1, + limit: number = 20, + status?: string | null, + orderBy?: string[] | null, + tenantId?: string | null +): Promise => { + const params = new URLSearchParams(); + params.append('page', String(page)); + params.append('limit', String(limit)); + if (tenantId) { + params.append('tenant_id', tenantId); + } + if (status) { + params.append('status', status); + } + if (orderBy && Array.isArray(orderBy) && orderBy.length === 2) { + // Send array as orderBy[]=field&orderBy[]=direction + params.append('orderBy[]', orderBy[0]); + params.append('orderBy[]', orderBy[1]); + } + const response = await apiClient.get(`/users?${params.toString()}`); + return response.data; +}; + export const userService = { - getAll: async ( - page: number = 1, - limit: number = 20, - status?: string | null, - orderBy?: string[] | null - ): Promise => { - const params = new URLSearchParams(); - params.append('page', String(page)); - params.append('limit', String(limit)); - if (status) { - params.append('status', status); - } - if (orderBy && Array.isArray(orderBy) && orderBy.length === 2) { - // Send array as orderBy[]=field&orderBy[]=direction - params.append('orderBy[]', orderBy[0]); - params.append('orderBy[]', orderBy[1]); - } - const response = await apiClient.get(`/users?${params.toString()}`); - return response.data; - }, + getAll: getAllUsers, create: async (data: CreateUserRequest): Promise => { const response = await apiClient.post('/users', data); return response.data; @@ -45,18 +51,7 @@ export const userService = { status?: string | null, orderBy?: string[] | null ): Promise => { - const params = new URLSearchParams(); - params.append('page', String(page)); - params.append('limit', String(limit)); - if (status) { - params.append('status', status); - } - if (orderBy && Array.isArray(orderBy) && orderBy.length === 2) { - params.append('orderBy[]', orderBy[0]); - params.append('orderBy[]', orderBy[1]); - } - const response = await apiClient.get(`/users/tenant/${tenantId}?${params.toString()}`); - return response.data; + return getAllUsers(page, limit, status, orderBy, tenantId); }, update: async (id: string, data: UpdateUserRequest): Promise => { const response = await apiClient.put(`/users/${id}`, data);