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.

This commit is contained in:
Yashwin 2026-02-04 17:56:03 +05:30
parent c656594154
commit acd956db50
2 changed files with 28 additions and 36 deletions

View File

@ -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(), 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(), 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(), 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(), consecutive_failures: z.number().int().optional().nullable(),
registered_by: z.string().uuid().optional().nullable(), registered_by: z.string().uuid().optional().nullable(),
tenant_id: z.string().uuid().optional().nullable(), tenant_id: z.string().uuid().optional().nullable(),
@ -96,7 +95,6 @@ export const NewModuleModal = ({
min_replicas: null, min_replicas: null,
max_replicas: null, max_replicas: null,
last_health_check: null, last_health_check: null,
// health_status: null,
consecutive_failures: null, consecutive_failures: null,
registered_by: null, registered_by: null,
tenant_id: null, tenant_id: null,
@ -126,7 +124,6 @@ export const NewModuleModal = ({
min_replicas: null, min_replicas: null,
max_replicas: null, max_replicas: null,
last_health_check: null, last_health_check: null,
// health_status: null,
consecutive_failures: null, consecutive_failures: null,
registered_by: null, registered_by: null,
tenant_id: null, tenant_id: null,
@ -158,7 +155,7 @@ export const NewModuleModal = ({
if (error?.response?.data?.details && Array.isArray(error.response.data.details)) { if (error?.response?.data?.details && Array.isArray(error.response.data.details)) {
const validationErrors = error.response.data.details; const validationErrors = error.response.data.details;
validationErrors.forEach((detail: { path: string; message: string }) => { 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, { setError(detail.path as keyof NewModuleFormData, {
type: 'server', type: 'server',
message: detail.message, message: detail.message,

View File

@ -9,27 +9,33 @@ import type {
DeleteUserResponse, DeleteUserResponse,
} from '@/types/user'; } from '@/types/user';
const getAllUsers = async (
page: number = 1,
limit: number = 20,
status?: string | null,
orderBy?: string[] | null,
tenantId?: string | null
): Promise<UsersResponse> => {
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<UsersResponse>(`/users?${params.toString()}`);
return response.data;
};
export const userService = { export const userService = {
getAll: async ( getAll: getAllUsers,
page: number = 1,
limit: number = 20,
status?: string | null,
orderBy?: string[] | null
): Promise<UsersResponse> => {
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<UsersResponse>(`/users?${params.toString()}`);
return response.data;
},
create: async (data: CreateUserRequest): Promise<CreateUserResponse> => { create: async (data: CreateUserRequest): Promise<CreateUserResponse> => {
const response = await apiClient.post<CreateUserResponse>('/users', data); const response = await apiClient.post<CreateUserResponse>('/users', data);
return response.data; return response.data;
@ -45,18 +51,7 @@ export const userService = {
status?: string | null, status?: string | null,
orderBy?: string[] | null orderBy?: string[] | null
): Promise<UsersResponse> => { ): Promise<UsersResponse> => {
const params = new URLSearchParams(); return getAllUsers(page, limit, status, orderBy, tenantId);
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<UsersResponse>(`/users/tenant/${tenantId}?${params.toString()}`);
return response.data;
}, },
update: async (id: string, data: UpdateUserRequest): Promise<UpdateUserResponse> => { update: async (id: string, data: UpdateUserRequest): Promise<UpdateUserResponse> => {
const response = await apiClient.put<UpdateUserResponse>(`/users/${id}`, data); const response = await apiClient.put<UpdateUserResponse>(`/users/${id}`, data);