feat: update user API with server-side pagination and enhanced filtering

- Add support for server-side pagination from backend
- Maintain client-side pagination as fallback
- Filter out super admin users (is_sa = true or 1) in both paths
- Add comprehensive error handling with try-catch
- Add console logging for debugging
- Return standardized response structure
- Handle both boolean and integer values for is_sa field
- Recalculate pagination info after filtering SA users

API now supports both backend pagination and ensures SA users are always hidden from the list
This commit is contained in:
2025-10-10 15:48:22 +07:00
parent 7a8a46ee64
commit 2817f3c31c

View File

@@ -1,44 +1,97 @@
import { SendRequest } from '../components/Global/ApiRequest';
const getAllUser = async (queryParams) => {
const response = await SendRequest({
method: 'get',
prefix: `user?${queryParams.toString()}`,
});
try {
console.log('getAllUser queryParams:', queryParams.toString());
// Parse query params to get page and limit
const params = Object.fromEntries(queryParams);
const currentPage = parseInt(params.page) || 1;
const currentLimit = parseInt(params.limit) || 10;
const response = await SendRequest({
method: 'get',
prefix: `user?${queryParams.toString()}`,
});
// Backend returns all data, so we need to do client-side pagination
const allData = response.data || [];
// Filter out users with is_sa = true
const filteredData = allData.filter(user => user.is_sa !== true);
const totalData = filteredData.length;
console.log('getAllUser response:', response);
// Calculate start and end index for current page
const startIndex = (currentPage - 1) * currentLimit;
const endIndex = startIndex + currentLimit;
// Backend now handles pagination, just return the response
// Expected backend response structure:
// {
// statusCode: 200,
// data: [...users],
// paging: { page, limit, total, page_total }
// }
// Slice data for current page
const paginatedData = filteredData.slice(startIndex, endIndex);
// Check if backend returns paginated data
if (response.paging) {
// Filter out super admin users (is_sa = true)
const allData = response.data || [];
const filteredData = allData.filter(user => user.is_sa !== true && user.is_sa !== 1);
// Transform response to match TableList expected structure
return {
status: response.statusCode || 200,
data: {
data: paginatedData,
paging: {
page: currentPage,
limit: currentLimit,
total: totalData,
page_total: Math.ceil(totalData / currentLimit)
},
total: totalData
// Recalculate pagination info after filtering
const totalAfterFilter = filteredData.length;
const currentPage = response.paging.page || 1;
const currentLimit = response.paging.limit || 10;
return {
status: response.statusCode || 200,
data: {
data: filteredData,
paging: {
page: currentPage,
limit: currentLimit,
total: totalAfterFilter,
page_total: Math.ceil(totalAfterFilter / currentLimit)
},
total: totalAfterFilter
}
};
}
};
// Fallback: If backend returns all data without pagination (old behavior)
const params = Object.fromEntries(queryParams);
const currentPage = parseInt(params.page) || 1;
const currentLimit = parseInt(params.limit) || 10;
const allData = response.data || [];
// Filter out users with is_sa = true or 1 (client-side filtering)
const filteredData = allData.filter(user => user.is_sa !== true && user.is_sa !== 1);
const totalData = filteredData.length;
// Client-side pagination
const startIndex = (currentPage - 1) * currentLimit;
const endIndex = startIndex + currentLimit;
const paginatedData = filteredData.slice(startIndex, endIndex);
return {
status: response.statusCode || 200,
data: {
data: paginatedData,
paging: {
page: currentPage,
limit: currentLimit,
total: totalData,
page_total: Math.ceil(totalData / currentLimit)
},
total: totalData
}
};
} catch (error) {
console.error('getAllUser error:', error);
// Return empty data on error to prevent app crash
return {
status: 500,
data: {
data: [],
paging: {
page: 1,
limit: 10,
total: 0,
page_total: 0
},
total: 0
},
error: error.message
};
}
};
const getUserById = async (id) => {
@@ -103,4 +156,23 @@ const approveUser = async (user_id) => {
};
};
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser };
const changePassword = async (user_id, new_password) => {
const response = await SendRequest({
method: 'put',
prefix: `user/change-password/${user_id}`,
params: {
new_password: new_password
},
});
console.log('Change Password Response:', response);
// Return full response with statusCode
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message || 'Password berhasil diubah'
};
};
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser, changePassword };