clean code master plant sub section and master device

This commit is contained in:
2025-10-21 20:26:05 +07:00
parent 55213480c9
commit 4bd0348a2a
24 changed files with 521 additions and 2460 deletions

View File

@@ -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 };