144 lines
4.2 KiB
JavaScript
144 lines
4.2 KiB
JavaScript
import { getRequest, postRequest, putRequest, deleteRequest } from '@/services/api/CommonService';
|
|
|
|
const admin = '/admin_users';
|
|
|
|
export const getAdminUser = async () => {
|
|
try {
|
|
const response = await getRequest(admin);
|
|
return response.data || [];
|
|
} catch (error) {
|
|
console.error('Error fetching units:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const createadminusers = async (admindata) => {
|
|
try {
|
|
const response = await postRequest(admin, admindata);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error creating unit:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateAdminUser = async (id, admindata) => {
|
|
try {
|
|
const response = await putRequest(`${admin}/${id}`, admindata);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error updating unit:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
export const getAdminUserById = async (id) => {
|
|
try {
|
|
const response = await getRequest(`${admin}/${id}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching admin user:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const changeAdminUserPassword = async (id, passwordData) => {
|
|
try {
|
|
if (!id) throw new Error("Invalid user ID");
|
|
|
|
console.log('Debug: Password data received:', {
|
|
hasOldPassword: !!passwordData.old_password,
|
|
hasNewPassword: !!passwordData.new_password,
|
|
hasConfirmPassword: !!passwordData.confirm_password
|
|
});
|
|
|
|
// Prepare the request data according to API specification
|
|
const requestData = {
|
|
old_password: passwordData.old_password,
|
|
new_password: passwordData.new_password,
|
|
confirm_password: passwordData.confirm_password
|
|
};
|
|
|
|
|
|
// Use the correct endpoint for admin users
|
|
// const response = await putRequest(`/admin_users/${id}/change-password`, requestData);
|
|
const response = await putRequest(`admin_users/${id}/change-password`, requestData);
|
|
|
|
return response.data;
|
|
|
|
} catch (error) {
|
|
console.error("Error changing admin user password:", error);
|
|
|
|
if (error.response) {
|
|
console.error("🔍 Debug: API error response:", {
|
|
status: error.response.status,
|
|
data: error.response.data,
|
|
headers: error.response.headers
|
|
});
|
|
|
|
// Extract meaningful error message
|
|
let errorMessage = 'Failed to change password';
|
|
|
|
if (error.response.status === 404) {
|
|
errorMessage = 'Admin user not found or endpoint unavailable';
|
|
} else if (error.response.data.message) {
|
|
errorMessage = error.response.data.message;
|
|
} else if (error.response.data.errors) {
|
|
// Handle Laravel validation errors
|
|
const errors = error.response.data.errors;
|
|
errorMessage = Object.values(errors).flat().join(', ');
|
|
} else if (error.response.data.error) {
|
|
errorMessage = error.response.data.error;
|
|
}
|
|
|
|
const detailedError = new Error(errorMessage);
|
|
detailedError.status = error.response.status;
|
|
detailedError.data = error.response.data;
|
|
|
|
throw detailedError;
|
|
} else if (error.request) {
|
|
console.error("🔍 Debug: No response received:", error.request);
|
|
throw new Error('No response from server. Please check your connection.');
|
|
} else {
|
|
console.error("🔍 Debug: Request setup error:", error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
};
|
|
// export const deleteAdminUser = async (id) => {
|
|
// try {
|
|
// const response = await deleteRequest(`${admin}/${id}`);
|
|
// return response.data;
|
|
// } catch (error) {
|
|
// console.error('Error deleting admin user:', error);
|
|
// throw error;
|
|
// }
|
|
// };
|
|
|
|
export const deleteAdminUser = async (id) => {
|
|
try {
|
|
if (!id) throw new Error("Invalid user ID");
|
|
|
|
const response = await deleteRequest(`${admin}/${id}`);
|
|
|
|
// Log for debugging
|
|
|
|
// Safely check success from API
|
|
const resData = response?.data || response;
|
|
|
|
const success =
|
|
resData?.status === "success" ||
|
|
resData?.success === true ||
|
|
resData?.code === 200 ||
|
|
resData?.message?.toLowerCase()?.includes("deleted");
|
|
|
|
if (!success) {
|
|
throw new Error("Delete API did not return success");
|
|
}
|
|
|
|
return resData;
|
|
} catch (error) {
|
|
console.error("Error deleting admin user:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|