example for template master device

This commit is contained in:
2025-10-10 14:53:43 +07:00
parent 6eed13bc4f
commit ab3b38eb49
9 changed files with 266 additions and 226 deletions

View File

@@ -2,42 +2,28 @@ const setResponse = (data = null, message = "success", statusCode = 200) => {
const total = Array.isArray(data) ? data.length : null;
return {
data,
total,
message,
statusCode
statusCode,
rows: total,
data,
};
};
const setResponsePaging = async (data = [], total, limit, page, message = "success", statusCode = 200) => {
const setResponsePaging = async (queryParam, data = [], message = "success", statusCode = 200) => {
const totalPages = Math.ceil(total / limit);
const totalPages = Math.ceil(data?.total / Number(queryParam.limit ?? 0));
const response = {
message,
statusCode,
data,
total: data.length,
rows: data?.data?.length,
paging: {
total,
limit,
page,
page_total: totalPages
}
}
return response
};
const setPaging = async (total, limit, page) => {
const totalPages = Math.ceil(total / limit);
const response = {
total,
limit,
page,
page_total: totalPages
current_limit: Number(queryParam.limit ?? 0),
current_page: Number(queryParam.page ?? 0),
total_limit: data?.total,
total_page: totalPages
},
data: data?.data ?? []
}
return response
@@ -86,4 +72,19 @@ function orderByClauseQuery(orderParams) {
return orderByClause
}
module.exports = { setResponse, setResponsePaging, setPaging, convertId, formatToYYYYMMDD, orderByClauseQuery };
const checkValidate = (validateSchema, req) => {
const { error, value } = validateSchema.validate(req.body || {}, { abortEarly: false });
if (error) {
const errors = error.details.reduce((acc, cur) => {
const field = Array.isArray(cur.path) ? cur.path.join('.') : String(cur.path);
if (!acc[field]) acc[field] = [];
acc[field].push(cur.message);
return acc;
}, {});
return { error: errors, value }
}
return { error, value }
}
module.exports = { setResponse, setResponsePaging, convertId, formatToYYYYMMDD, orderByClauseQuery, checkValidate };