clean code master plant sub section and master device
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getTotal = async (query = '') => {
|
||||
const prefix = `${query ? `?${query}` : ''}`;
|
||||
const fullUrl = `${import.meta.env.VITE_API_SERVER}/dashboard/${prefix}`;
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `dashboard/${prefix}`,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(`[API Call] Failed: GET ${fullUrl}`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const getTotalPermit = async (query = '') => {
|
||||
const prefix = `dashboard/permit-total${query ? `?${query}` : ''}`;
|
||||
const fullUrl = `${import.meta.env.VITE_API_SERVER}/${prefix}`;
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(`[API Call] Failed: GET ${fullUrl}`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const getTotalPermitPerYear = async (query = '') => {
|
||||
const prefix = `dashboard/permit-breakdown${query ? `?${query}` : ''}`;
|
||||
const fullUrl = `${import.meta.env.VITE_API_SERVER}/${prefix}`;
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(`[API Call] Failed: GET ${fullUrl}`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export { getTotal, getTotalPermit, getTotalPermitPerYear };
|
||||
@@ -1,55 +1,41 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllJadwalShift = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `jadwal-shift?${queryParams.toString()}`,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('getAllJadwalShift error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `jadwal-shift?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getJadwalShiftById = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `jadwal-shift/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const createJadwalShift = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'post',
|
||||
prefix: `jadwal-shift`,
|
||||
data: queryParams,
|
||||
params: queryParams,
|
||||
});
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateJadwalShift = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `jadwal-shift/${id}`,
|
||||
data: queryParams,
|
||||
params: queryParams,
|
||||
});
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteJadwalShift = async (id) => {
|
||||
@@ -57,11 +43,13 @@ const deleteJadwalShift = async (id) => {
|
||||
method: 'delete',
|
||||
prefix: `jadwal-shift/${id}`,
|
||||
});
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllJadwalShift, createJadwalShift, updateJadwalShift, deleteJadwalShift };
|
||||
export {
|
||||
getAllJadwalShift,
|
||||
getJadwalShiftById,
|
||||
createJadwalShift,
|
||||
updateJadwalShift,
|
||||
deleteJadwalShift,
|
||||
};
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllApd = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `apd?${queryParams.toString()}`,
|
||||
});
|
||||
return response;
|
||||
};
|
||||
|
||||
const createApd = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'post',
|
||||
prefix: `apd`,
|
||||
params: queryParams,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateApd = async (vendor_id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `apd/${vendor_id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteApd = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `apd/${queryParams}`,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getJenisPermit = async () => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `apd/jenis-permit`,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllApd, createApd, updateApd, deleteApd, getJenisPermit };
|
||||
@@ -1,70 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllBrands = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `brand?${queryParams.toString()}`,
|
||||
});
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `brand?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllBrands error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getBrandById = async (id) => {
|
||||
@@ -72,6 +14,7 @@ const getBrandById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `brand/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -81,55 +24,27 @@ const createBrand = async (queryParams) => {
|
||||
prefix: `brand`,
|
||||
params: queryParams,
|
||||
});
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateBrand = async (brand_id, queryParams) => {
|
||||
const updateBrand = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `brand/${brand_id}`,
|
||||
prefix: `brand/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteBrand = async (queryParams) => {
|
||||
const deleteBrand = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `brand/${queryParams}`,
|
||||
prefix: `brand/${id}`,
|
||||
});
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllBrands, getBrandById, createBrand, updateBrand, deleteBrand };
|
||||
|
||||
@@ -1,88 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllDevice = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `device?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllDevice response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `device?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
// Backend response structure:
|
||||
// {
|
||||
// statusCode: 200,
|
||||
// data: [...devices],
|
||||
// paging: {
|
||||
// current_page: 1,
|
||||
// current_limit: 10,
|
||||
// total_limit: 50,
|
||||
// total_page: 5
|
||||
// }
|
||||
// }
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 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 || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllDevice error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getDeviceById = async (id) => {
|
||||
@@ -90,6 +14,7 @@ const getDeviceById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `device/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -99,71 +24,27 @@ const createDevice = async (queryParams) => {
|
||||
prefix: `device`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createDevice full response:', response);
|
||||
console.log('createDevice payload sent:', queryParams);
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [device_object] }
|
||||
// Check if response is empty array (error from SendRequest)
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Extract first item from data array
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateDevice = async (device_id, queryParams) => {
|
||||
const updateDevice = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `device/${device_id}`,
|
||||
prefix: `device/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateDevice full response:', response);
|
||||
console.log('updateDevice payload sent:', queryParams);
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [device_object] }
|
||||
// Check if response is empty array (error from SendRequest)
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Extract first item from data array
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteDevice = async (queryParams) => {
|
||||
const deleteDevice = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `device/${queryParams}`,
|
||||
prefix: `device/${id}`,
|
||||
});
|
||||
console.log('deleteDevice full response:', response);
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllDevice, getDeviceById, createDevice, updateDevice, deleteDevice };
|
||||
|
||||
@@ -1,97 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllPlantSection = async (queryParams) => {
|
||||
try {
|
||||
// Ensure queryParams is URLSearchParams object
|
||||
const params = queryParams instanceof URLSearchParams ? queryParams : new URLSearchParams(queryParams);
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section?${params.toString()}`,
|
||||
});
|
||||
console.log('getAllPlantSection response:', response);
|
||||
console.log('Query params:', params.toString());
|
||||
|
||||
// Backend response structure:
|
||||
// {
|
||||
// statusCode: 200,
|
||||
// data: [...plantSections],
|
||||
// paging: {
|
||||
// current_page: 1,
|
||||
// current_limit: 10,
|
||||
// total_limit: 50,
|
||||
// total_page: 5
|
||||
// }
|
||||
// }
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
// Extract total_data from first record, or fallback to total_limit or rows
|
||||
const totalData = response.data?.[0]?.total_data || response.paging.total_limit || response.rows || response.data?.length || 0;
|
||||
|
||||
// Use total_limit as total count, handle 0 values for page/limit
|
||||
const currentPage = response.paging.current_page || 1;
|
||||
const currentLimit = response.paging.current_limit || 10;
|
||||
const totalPages = response.paging.total_page || Math.ceil(totalData / currentLimit);
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
total: totalData,
|
||||
page_total: totalPages
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination (old behavior)
|
||||
const parsedParams = Object.fromEntries(params);
|
||||
const currentPage = parseInt(parsedParams.page) || 1;
|
||||
const currentLimit = parseInt(parsedParams.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllPlantSection error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getPlantSectionById = async (id) => {
|
||||
@@ -99,6 +14,7 @@ const getPlantSectionById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -108,80 +24,33 @@ const createPlantSection = async (queryParams) => {
|
||||
prefix: `plant-sub-section`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createPlantSection full response:', response);
|
||||
console.log('createPlantSection payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [plantSection_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updatePlantSection = async (plant_section_id, queryParams) => {
|
||||
const updatePlantSection = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `plant-sub-section/${plant_section_id}`,
|
||||
prefix: `plant-sub-section/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updatePlantSection full response:', response);
|
||||
console.log('updatePlantSection payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [plantSection_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deletePlantSection = async (queryParams) => {
|
||||
const deletePlantSection = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `plant-sub-section/${queryParams}`,
|
||||
prefix: `plant-sub-section/${id}`,
|
||||
});
|
||||
console.log('deletePlantSection full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllPlantSection, getPlantSectionById, createPlantSection, updatePlantSection, deletePlantSection };
|
||||
export {
|
||||
getAllPlantSection,
|
||||
getPlantSectionById,
|
||||
createPlantSection,
|
||||
updatePlantSection,
|
||||
deletePlantSection,
|
||||
};
|
||||
|
||||
@@ -1,95 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllShift = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `shift?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllShift response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `shift?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
// Check if response has error
|
||||
if (response.error) {
|
||||
console.error('getAllShift error response:', response);
|
||||
return {
|
||||
status: response.statusCode || 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: response.message
|
||||
};
|
||||
}
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllShift catch error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getShiftById = async (id) => {
|
||||
@@ -97,6 +14,7 @@ const getShiftById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `shift/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -106,26 +24,8 @@ const createShift = async (queryParams) => {
|
||||
prefix: `shift`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createShift full response:', response);
|
||||
console.log('createShift payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [shift_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateShift = async (id, queryParams) => {
|
||||
@@ -134,26 +34,8 @@ const updateShift = async (id, queryParams) => {
|
||||
prefix: `shift/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateShift full response:', response);
|
||||
console.log('updateShift payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [shift_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteShift = async (id) => {
|
||||
@@ -161,25 +43,8 @@ const deleteShift = async (id) => {
|
||||
method: 'delete',
|
||||
prefix: `shift/${id}`,
|
||||
});
|
||||
console.log('deleteShift full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllShift, getShiftById, createShift, updateShift, deleteShift };
|
||||
|
||||
@@ -1,89 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllStatus = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `status?${queryParams.toString()}`,
|
||||
});
|
||||
const getAllStatuss = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `status?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
console.error('getAllStatus error response:', response);
|
||||
return {
|
||||
status: response.statusCode || 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: response.message
|
||||
};
|
||||
}
|
||||
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllStatus catch error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getStatusById = async (id) => {
|
||||
@@ -91,6 +14,7 @@ const getStatusById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `status/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -101,68 +25,26 @@ const createStatus = async (queryParams) => {
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateStatus = async (status_id, queryParams) => {
|
||||
const updateStatus = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `status/${status_id}`,
|
||||
prefix: `status/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteStatus = async (queryParams) => {
|
||||
const deleteStatus = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `status/${queryParams}`,
|
||||
prefix: `status/${id}`,
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllStatus, getStatusById, createStatus, updateStatus, deleteStatus };
|
||||
export { getAllStatuss, getStatusById, createStatus, updateStatus, deleteStatus };
|
||||
|
||||
@@ -1,93 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllTag = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `tags?${queryParams.toString()}`,
|
||||
});
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `tags?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
// Check if response has error
|
||||
if (response.error) {
|
||||
console.error('getAllTag error response:', response);
|
||||
return {
|
||||
status: response.statusCode || 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: response.message
|
||||
};
|
||||
}
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 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 || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllTag catch error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getTagById = async (id) => {
|
||||
@@ -95,6 +14,7 @@ const getTagById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `tags/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -105,74 +25,26 @@ const createTag = async (queryParams) => {
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [tag_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateTag = async (tag_id, queryParams) => {
|
||||
const updateTag = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `tags/${tag_id}`,
|
||||
prefix: `tags/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [tag_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteTag = async (queryParams) => {
|
||||
const deleteTag = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `tags/${queryParams}`,
|
||||
prefix: `tags/${id}`,
|
||||
});
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllTag, getTagById, createTag, updateTag, deleteTag };
|
||||
|
||||
@@ -1,95 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllUnit = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `unit?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllUnit response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `unit?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
// Check if response has error
|
||||
if (response.error) {
|
||||
console.error('getAllUnit error response:', response);
|
||||
return {
|
||||
status: response.statusCode || 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: response.message
|
||||
};
|
||||
}
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllUnit catch error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getUnitById = async (id) => {
|
||||
@@ -97,101 +14,37 @@ const getUnitById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `unit/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const createUnit = async (queryParams) => {
|
||||
// Map frontend fields to backend fields
|
||||
const backendParams = {
|
||||
unit_name: queryParams.name,
|
||||
is_active: queryParams.is_active,
|
||||
};
|
||||
|
||||
const response = await SendRequest({
|
||||
method: 'post',
|
||||
prefix: `unit`,
|
||||
params: backendParams,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createUnit full response:', response);
|
||||
console.log('createUnit payload sent:', backendParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [unit_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateUnit = async (unit_id, queryParams) => {
|
||||
// Map frontend fields to backend fields
|
||||
const backendParams = {
|
||||
unit_name: queryParams.name,
|
||||
is_active: queryParams.is_active,
|
||||
};
|
||||
|
||||
const updateUnit = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `unit/${unit_id}`,
|
||||
params: backendParams,
|
||||
prefix: `unit/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateUnit full response:', response);
|
||||
console.log('updateUnit payload sent:', backendParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [unit_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteUnit = async (queryParams) => {
|
||||
const deleteUnit = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `unit/${queryParams}`,
|
||||
prefix: `unit/${id}`,
|
||||
});
|
||||
console.log('deleteUnit full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllUnit, getUnitById, createUnit, updateUnit, deleteUnit };
|
||||
|
||||
164
src/api/role.jsx
164
src/api/role.jsx
@@ -1,70 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllRole = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `roles?${queryParams.toString()}`,
|
||||
});
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `roles?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
console.log('Role API Response:', response);
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
// Backend already provides pagination info
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: response.paging,
|
||||
total: response.paging.total || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.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('getAllRole error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getRoleById = async (id) => {
|
||||
@@ -72,6 +14,7 @@ const getRoleById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `roles/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -82,107 +25,26 @@ const createRole = async (queryParams) => {
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
console.log('Create Role API Response:', response);
|
||||
|
||||
// Check for error status (not 200, 201, or success)
|
||||
const isSuccess =
|
||||
response.statusCode === 200 || response.statusCode === 201 || response.status === 'success';
|
||||
|
||||
if (!isSuccess && response.statusCode >= 400) {
|
||||
let errorMessage = response.message || 'Gagal menambahkan role';
|
||||
|
||||
// Handle SQL unique constraint violation
|
||||
if (
|
||||
errorMessage.includes('UNIQUE KEY constraint') ||
|
||||
errorMessage.includes('duplicate key')
|
||||
) {
|
||||
errorMessage = `Role dengan nama "${queryParams.role_name}" sudah ada. Silakan gunakan nama lain.`;
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode,
|
||||
data: response.data,
|
||||
message: errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message || 'Berhasil menambahkan role',
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateRole = async (role_id, queryParams) => {
|
||||
const updateRole = async (id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `roles/${role_id}`,
|
||||
prefix: `roles/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
|
||||
console.log('Update Role API Response:', response);
|
||||
|
||||
// Check for error status (not 200, 201, or success)
|
||||
const isSuccess =
|
||||
response.statusCode === 200 || response.statusCode === 201 || response.status === 'success';
|
||||
|
||||
if (!isSuccess && response.statusCode >= 400) {
|
||||
let errorMessage = response.message || 'Gagal mengubah role';
|
||||
|
||||
// Handle SQL unique constraint violation
|
||||
if (
|
||||
errorMessage.includes('UNIQUE KEY constraint') ||
|
||||
errorMessage.includes('duplicate key')
|
||||
) {
|
||||
errorMessage = `Role dengan nama "${queryParams.role_name}" sudah ada. Silakan gunakan nama lain.`;
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode,
|
||||
data: response.data,
|
||||
message: errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message || 'Berhasil mengubah role',
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteRole = async (queryParams) => {
|
||||
const deleteRole = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `roles/${queryParams}`,
|
||||
prefix: `roles/${id}`,
|
||||
});
|
||||
|
||||
console.log('Delete API Response:', response);
|
||||
|
||||
// Check for errors
|
||||
if (response.statusCode !== 200) {
|
||||
let errorMessage = response.message || 'Gagal menghapus role';
|
||||
|
||||
// Handle foreign key constraint
|
||||
if (errorMessage.includes('REFERENCE constraint') || errorMessage.includes('foreign key')) {
|
||||
errorMessage = 'Role tidak dapat dihapus karena masih digunakan oleh user.';
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: response.statusCode,
|
||||
data: response.data,
|
||||
message: errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllRole, getRoleById, createRole, updateRole, deleteRole };
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
// user-admin.jsx
|
||||
import axios from 'axios';
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const baseURL = import.meta.env.VITE_API_SERVER;
|
||||
|
||||
const getAllUser = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `admin-user?${queryParams.toString()}`,
|
||||
});
|
||||
return response;
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getUserDetail = async (id) => {
|
||||
@@ -17,7 +13,7 @@ const getUserDetail = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `admin-user/${id}`,
|
||||
});
|
||||
return response;
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateUser = async (id, data) => {
|
||||
@@ -26,7 +22,7 @@ const updateUser = async (id, data) => {
|
||||
prefix: `admin-user/${id}`,
|
||||
params: data,
|
||||
});
|
||||
return response;
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteUser = async (id) => {
|
||||
@@ -34,7 +30,7 @@ const deleteUser = async (id) => {
|
||||
method: 'delete',
|
||||
prefix: `admin-user/${id}`,
|
||||
});
|
||||
return response;
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const approvalUser = async (id, queryParams) => {
|
||||
@@ -43,42 +39,7 @@ const approvalUser = async (id, queryParams) => {
|
||||
prefix: `admin-user/approve/${id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
return response;
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const uploadFile = async (formData) => {
|
||||
try {
|
||||
const token = localStorage.getItem('token')?.replace(/"/g, '') || '';
|
||||
const url = `${baseURL}/file-upload`;
|
||||
|
||||
const response = await axios.post(url, formData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Accept-Language': 'en_US',
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
statusCode: response.data?.statusCode ?? 0,
|
||||
message: response.data?.message ?? '',
|
||||
data: response.data?.data ?? {},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('❌ ERROR di uploadFile:', error);
|
||||
return {
|
||||
statusCode: error?.response?.status || 500,
|
||||
message: error?.response?.data?.message || 'Upload gagal',
|
||||
data: {},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export { getAllUser, getUserDetail, updateUser, deleteUser, approvalUser, uploadFile };
|
||||
export { getAllUser, getUserDetail, updateUser, deleteUser, approvalUser };
|
||||
|
||||
169
src/api/user.jsx
169
src/api/user.jsx
@@ -1,97 +1,12 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllUser = async (queryParams) => {
|
||||
try {
|
||||
console.log('getAllUser queryParams:', queryParams.toString());
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `user?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `user?${queryParams.toString()}`,
|
||||
});
|
||||
|
||||
console.log('getAllUser response:', response);
|
||||
|
||||
// Backend now handles pagination, just return the response
|
||||
// Expected backend response structure:
|
||||
// {
|
||||
// statusCode: 200,
|
||||
// data: [...users],
|
||||
// paging: { page, limit, total, page_total }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
};
|
||||
}
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getUserById = async (id) => {
|
||||
@@ -99,6 +14,7 @@ const getUserById = async (id) => {
|
||||
method: 'get',
|
||||
prefix: `user/${id}`,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -108,12 +24,8 @@ const createUser = async (queryParams) => {
|
||||
prefix: `user`,
|
||||
params: queryParams,
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateUser = async (user_id, queryParams) => {
|
||||
@@ -122,12 +34,8 @@ const updateUser = async (user_id, queryParams) => {
|
||||
prefix: `user/${user_id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const deleteUser = async (queryParams) => {
|
||||
@@ -135,12 +43,8 @@ const deleteUser = async (queryParams) => {
|
||||
method: 'delete',
|
||||
prefix: `user/${queryParams}`,
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const approveUser = async (user_id) => {
|
||||
@@ -148,12 +52,8 @@ const approveUser = async (user_id) => {
|
||||
method: 'put',
|
||||
prefix: `user/${user_id}/approve`,
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const rejectUser = async (user_id) => {
|
||||
@@ -161,12 +61,8 @@ const rejectUser = async (user_id) => {
|
||||
method: 'put',
|
||||
prefix: `user/${user_id}/reject`,
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const toggleActiveUser = async (user_id, is_active) => {
|
||||
@@ -174,15 +70,11 @@ const toggleActiveUser = async (user_id, is_active) => {
|
||||
method: 'put',
|
||||
prefix: `user/${user_id}`,
|
||||
params: {
|
||||
is_active: is_active
|
||||
is_active: is_active,
|
||||
},
|
||||
});
|
||||
// Return full response with statusCode
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
};
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const changePassword = async (user_id, new_password) => {
|
||||
@@ -190,18 +82,21 @@ const changePassword = async (user_id, new_password) => {
|
||||
method: 'put',
|
||||
prefix: `user/change-password/${user_id}`,
|
||||
params: {
|
||||
new_password: new_password
|
||||
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'
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser, rejectUser, toggleActiveUser, changePassword };
|
||||
export {
|
||||
getAllUser,
|
||||
getUserById,
|
||||
createUser,
|
||||
updateUser,
|
||||
deleteUser,
|
||||
approveUser,
|
||||
rejectUser,
|
||||
toggleActiveUser,
|
||||
changePassword,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user