example for template master device
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -46,36 +46,6 @@ const newPasswordSchema = Joi.object({
|
||||
})
|
||||
})
|
||||
|
||||
// ========================
|
||||
// Device Validation
|
||||
// ========================
|
||||
const deviceSchema = Joi.object({
|
||||
device_code: Joi.string().max(100).required(),
|
||||
device_name: Joi.string().max(100).required(),
|
||||
device_status: Joi.boolean().required(),
|
||||
device_location: Joi.string().max(100).required(),
|
||||
device_description: Joi.string().required(),
|
||||
ip_address: Joi.string()
|
||||
.ip({ version: ['ipv4', 'ipv6'] })
|
||||
.required()
|
||||
.messages({
|
||||
'string.ip': 'IP address must be a valid IPv4 or IPv6 address'
|
||||
})
|
||||
});
|
||||
|
||||
const deviceUpdateSchema = Joi.object({
|
||||
device_code: Joi.string().max(100),
|
||||
device_name: Joi.string().max(100),
|
||||
device_status: Joi.boolean(),
|
||||
device_location: Joi.string().max(100),
|
||||
device_description: Joi.string(),
|
||||
ip_address: Joi.string()
|
||||
.ip({ version: ['ipv4', 'ipv6'] })
|
||||
.messages({
|
||||
'string.ip': 'IP address must be a valid IPv4 or IPv6 address'
|
||||
})
|
||||
}).min(1);
|
||||
|
||||
// ========================
|
||||
// Users Validation
|
||||
// ========================
|
||||
@@ -101,14 +71,12 @@ const userSchema = Joi.object({
|
||||
'string.min': 'Password must be at least 8 characters long',
|
||||
'string.pattern.name': 'Password must contain at least one {#name}'
|
||||
}),
|
||||
role_id : Joi.number().integer().min(1)
|
||||
role_id: Joi.number().integer().min(1)
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
registerSchema,
|
||||
loginSchema,
|
||||
newPasswordSchema,
|
||||
deviceSchema,
|
||||
deviceUpdateSchema,
|
||||
userSchema,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user