Files
tm_panel/src/lib/api/services/user-services.ts
T

91 lines
2.5 KiB
TypeScript

import api from "../axios";
import { API_ENDPOINTS } from "../endpoints";
import {
ApiResponse,
IAdminListResponse,
ICreateAdminCredentials,
ILoginCredentials,
ILoginResponse,
ILogoutResponse,
LoginResponseSchema,
LogoutResponseSchema,
} from "../types";
export const userService = {
login: async (
credentials: ILoginCredentials,
): Promise<ApiResponse<ILoginResponse>> => {
try {
const response = await api.post(API_ENDPOINTS.USER.LOGIN, credentials);
return LoginResponseSchema.parse(response.data);
} catch (error) {
console.error("ERROR caught in User Service Login:", error);
throw error;
}
},
logout: async (): Promise<ApiResponse<ILogoutResponse>> => {
try {
const response = await api.delete(API_ENDPOINTS.USER.LOGOUT);
return LogoutResponseSchema.parse(response.data);
} catch (error) {
console.error("ERROR caught in User Service Logout", error);
throw error;
}
},
adminsList: async (
params?: Record<string, any>,
): Promise<ApiResponse<IAdminListResponse>> => {
try {
const response = await api.get(API_ENDPOINTS.USER.ADMINS, { params });
return response.data;
} catch (error) {
console.error("ERROR caught in User Service User List", error);
throw error;
}
},
createAdmin: async (credentials: ICreateAdminCredentials) => {
try {
return (await api.post(API_ENDPOINTS.USER.ADMINS, credentials)).data
.message;
} catch (error) {
console.error("ERROR caught in User Service Create Admin", error);
throw error;
}
},
updateAdmin: async ({
id,
credentials,
}: {
id: string;
credentials: ICreateAdminCredentials;
}) => {
try {
return (await api.put(API_ENDPOINTS.USER.ADMIN_DETAILS(id), credentials))
.data.message;
} catch (error) {
console.error("ERROR caught in User Service Update Admin", error);
throw error;
}
},
adminDetails: async (id: string) => {
try {
return (await api.get(API_ENDPOINTS.USER.ADMIN_DETAILS(id))).data;
} catch (error) {
console.error("ERROR caught in User Service Admin Details", error);
throw error;
}
},
deleteAdmin: async (
id: string,
): Promise<ApiResponse<{ message: string }>> => {
try {
return await api.delete(`${API_ENDPOINTS.USER.ADMINS}/${id}`);
} catch (error) {
console.error("ERROR caught in User Service Delete Admin", error);
throw error;
}
},
};