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

@@ -46,6 +46,11 @@ async function query(text, params = []) {
return request.query(sqlText); return request.query(sqlText);
} }
function isValidDate(dateStr) {
const d = new Date(dateStr);
return !isNaN(d.getTime()); // true kalau valid
}
/** /**
* Build filter query * Build filter query
*/ */
@@ -71,10 +76,24 @@ function buildFilterQuery(filterQuery = [], fixedParams = []) {
queryParams.push(f.param ? 1 : 0); queryParams.push(f.param ? 1 : 0);
whereConditions.push(`${f.column} = $${queryParams.length}`); whereConditions.push(`${f.column} = $${queryParams.length}`);
break; break;
case 'between':
if (Array.isArray(f.param) && f.param.length === 2) {
const from = f.param[0];
const to = f.param[1];
if (isValidDate(from) && isValidDate(to)) {
queryParams.push(from);
queryParams.push(to);
whereConditions.push(
`${f.column} BETWEEN $${queryParams.length - 1} AND $${queryParams.length}`
);
}
}
break;
} }
}); });
return { whereConditions, queryParams }; return { whereConditions, whereParamAnd: queryParams };
} }
/** /**
@@ -96,13 +115,17 @@ function buildStringOrIlike(columnParam, criteria, fixedParams = []) {
? `AND (${orStringConditions.join(" OR ")})` ? `AND (${orStringConditions.join(" OR ")})`
: ""; : "";
return { whereOrConditions: whereClause, whereParam: queryParams }; return { whereOrConditions: whereClause, whereParamOr: queryParams };
} }
/** /**
* Build dynamic UPDATE * Build dynamic UPDATE
*/ */
function buildDynamicUpdate(table, data, where) { function buildDynamicUpdate(table, data, where) {
data.updated_by = data.userId
delete data.userId;
const setParts = []; const setParts = [];
const values = []; const values = [];
let index = 1; let index = 1;
@@ -118,8 +141,8 @@ function buildDynamicUpdate(table, data, where) {
throw new Error("Tidak ada kolom untuk diupdate"); throw new Error("Tidak ada kolom untuk diupdate");
} }
// updated_at otomatis pakai GETDATE() // updated_at otomatis pakai CURRENT_TIMESTAMP
setParts.push(`updated_at = GETDATE()`); setParts.push(`updated_at = CURRENT_TIMESTAMP`);
const whereParts = []; const whereParts = [];
for (const [key, value] of Object.entries(where)) { for (const [key, value] of Object.entries(where)) {
@@ -140,6 +163,11 @@ function buildDynamicUpdate(table, data, where) {
* Build dynamic INSERT * Build dynamic INSERT
*/ */
function buildDynamicInsert(table, data) { function buildDynamicInsert(table, data) {
data.created_by = data.userId
data.updated_by = data.userId
delete data.userId;
const columns = []; const columns = [];
const placeholders = []; const placeholders = [];
const values = []; const values = [];
@@ -159,7 +187,7 @@ function buildDynamicInsert(table, data) {
// created_at & updated_at otomatis // created_at & updated_at otomatis
columns.push("created_at", "updated_at"); columns.push("created_at", "updated_at");
placeholders.push("GETDATE()", "GETDATE()"); placeholders.push("CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP");
const query = ` const query = `
INSERT INTO ${table} (${columns.join(", ")}) INSERT INTO ${table} (${columns.join(", ")})

View File

@@ -1,101 +1,70 @@
const DeviceService = require('../services/device.service'); const DeviceService = require('../services/device.service');
const { deviceSchema, deviceUpdateSchema } = require('../helpers/validation'); const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { setResponse } = require('../helpers/utils'); const { insertDeviceSchema, updateDeviceSchema } = require('../validate/device.schema');
class DeviceController { class DeviceController {
// Get all devices // Get all devices
static async getAll(req, res) { static async getAll(req, res) {
try { const queryParams = req.query;
const { search } = req.query;
const devices = await DeviceService.getAllDevices(search || ''); const results = await DeviceService.getAllDevices(queryParams);
return res.status(200).json(setResponse(devices, 'Devices retrieved successfully', 200)); const response = await setResponsePaging(queryParams, results, 'Device found')
} catch (err) {
return res.status(err.statusCode || 500).json( res.status(response.statusCode).json(response);
setResponse([], err.message || 'Failed to retrieve devices', err.statusCode || 500)
);
}
} }
// Get device by ID // Get device by ID
static async getById(req, res) { static async getById(req, res) {
try {
const { id } = req.params; const { id } = req.params;
const device = await DeviceService.getDeviceById(id);
return res.status(200).json(setResponse(device, 'Device retrieved successfully', 200)); const results = await DeviceService.getDeviceById(id);
} catch (err) { const response = await setResponse(results, 'Device found')
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Device not found', err.statusCode || 500) res.status(response.statusCode).json(response);
);
}
} }
// Create device // Create device
static async create(req, res) { static async create(req, res) {
try { const { error, value } = await checkValidate(insertDeviceSchema, req)
const { error, value } = deviceSchema.validate(req.body || {}, { abortEarly: false });
if (error) { if (error) {
const errors = error.details.reduce((acc, cur) => { return res.status(400).json(setResponse(error, 'Validation failed', 400));
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 res.status(400).json(setResponse(errors, 'Validation failed', 400));
} }
const newDevice = await DeviceService.createDevice(value, req.user.user_id); value.userId = req.user.user_id
return res.status(201).json( const results = await DeviceService.createDevice(value);
setResponse(newDevice, 'Device created successfully', 201) const response = await setResponse(results, 'Device created successfully')
);
} catch (err) { return res.status(response.statusCode).json(response);
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to create device', err.statusCode || 500)
);
}
} }
// Update device // Update device
static async update(req, res) { static async update(req, res) {
try {
const { id } = req.params; const { id } = req.params;
const { error, value } = deviceUpdateSchema.validate(req.body || {}, { abortEarly: false });
const { error, value } = checkValidate(updateDeviceSchema, req)
if (error) { if (error) {
const errors = error.details.reduce((acc, cur) => { return res.status(400).json(setResponse(error, 'Validation failed', 400));
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 res.status(400).json(setResponse(errors, 'Validation failed', 400));
} }
const updatedDevice = await DeviceService.updateDevice(id, value, req.user.user_Id); value.userId = req.user.user_id
return res.status(200).json( const results = await DeviceService.updateDevice(id, value);
setResponse(updatedDevice.data, 'Device updated successfully', 200) const response = await setResponse(results, 'Device updated successfully')
);
} catch (err) { res.status(response.statusCode).json(response);
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to update device', err.statusCode || 500)
);
}
} }
// Soft delete device // Soft delete device
static async delete(req, res) { static async delete(req, res) {
try {
const { id } = req.params; const { id } = req.params;
await DeviceService.deleteDevice(id, req.user.userId); const results = await DeviceService.deleteDevice(id, req.user.user_id);
return res.status(200).json( const response = await setResponse(results, 'Device deleted successfully')
setResponse([], 'Device deleted successfully', 200)
); res.status(response.statusCode).json(response);
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to delete device', err.statusCode || 500)
);
}
} }
} }

View File

@@ -2,67 +2,97 @@ const pool = require("../config");
// Get all devices // Get all devices
const getAllDevicesDb = async (searchParams = {}) => { const getAllDevicesDb = async (searchParams = {}) => {
const { whereConditions, queryParams } = pool.buildFilterQuery([
{ column: "d.device_name", param: searchParams.name, type: "string" },
{ column: "d.device_code", param: searchParams.code, type: "string" },
{ column: "d.device_location", param: searchParams.location, type: "string" },
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
]);
const whereClause = whereConditions.length queryParams = []
? `AND ${whereConditions.join(" AND ")}` if (searchParams.limit) {
: ""; const page = Number(searchParams.page ?? 1) - 1
queryParams = [Number(searchParams.limit ?? 10), page];
}
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike([
"a.device_name",
"a.device_code",
"a.device_location",
"a.ip_address",
"b.brand_name"
], searchParams.criteria, queryParams);
queryParams = whereParamOr ? whereParamOr : queryParams
const { whereConditions, whereParamAnd } = pool.buildFilterQuery([
{ column: "a.device_name", param: searchParams.name, type: "string" },
{ column: "a.device_code", param: searchParams.code, type: "string" },
{ column: "a.device_location", param: searchParams.location, type: "string" },
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
], queryParams);
queryParams = whereParamAnd ? whereParamAnd : queryParams
const queryText = ` const queryText = `
SELECT d.*, b.brand_name SELECT COUNT(*) OVER() AS total_data, a.*, b.brand_name
FROM m_device d FROM m_device a
LEFT JOIN m_brands b ON d.brand_id = b.brand_id LEFT JOIN m_brands b ON a.brand_id = b.brand_id
WHERE d.deleted_at IS NULL ${whereClause} WHERE a.deleted_at IS NULL
ORDER BY d.device_id ASC ${whereConditions.length > 0 ? ` AND ${whereConditions.join(' AND ')}` : ''}
${whereOrConditions ? whereOrConditions : ''}
ORDER BY a.device_id ASC
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ''};
`; `;
const result = await pool.query(queryText, queryParams); const result = await pool.query(queryText, queryParams);
return result.recordset;
// Menghitung total data.
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
// Mengembalikan data dan total.
return { data: result.recordset, total };
}; };
const getDeviceByIdDb = async (id) => { const getDeviceByIdDb = async (id) => {
const queryText = ` const queryText = `
SELECT d.*, b.brand_name SELECT a.*, b.brand_name
FROM m_device d FROM m_device a
LEFT JOIN m_brands b ON d.brand_id = b.brand_id LEFT JOIN m_brands b ON a.brand_id = b.brand_id
WHERE d.device_id = $1 AND d.deleted_at IS NULL WHERE a.device_id = $1 AND a.deleted_at IS NULL
`; `;
const result = await pool.query(queryText, [id]); const result = await pool.query(queryText, [id]);
return result.recordset[0]; return result.recordset;
};
const getDeviceByCodeDb = async (code) => {
const queryText = `
SELECT d.*, b.brand_name
FROM m_device d
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
WHERE d.device_code = $1 AND d.deleted_at IS NULL
`;
const result = await pool.query(queryText, [code]);
return result.recordset[0];
}; };
const createDeviceDb = async (data) => { const createDeviceDb = async (data) => {
const { query: queryText, values } = pool.buildDynamicInsert("m_device", data);
const newCode = await pool.generateKode("DVC", "m_device", "device_code")
const store = {
...data,
device_code: newCode,
}
const { query: queryText, values } = pool.buildDynamicInsert("m_device", store);
const result = await pool.query(queryText, values); const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id; const insertedId = result.recordset[0]?.inserted_id;
return insertedId ? await getDeviceByIdDb(insertedId) : null; return insertedId ? await getDeviceByIdDb(insertedId) : null;
}; };
const updateDeviceDb = async (id, data) => { const updateDeviceDb = async (id, data) => {
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", data, { device_id: id });
const store = {
...data
}
// Kondisi WHERE
const whereData = {
device_id: id
};
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", store, whereData);
await pool.query(`${queryText} AND deleted_at IS NULL`, values); await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getDeviceByIdDb(id); return getDeviceByIdDb(id);
}; };
const softDeleteDeviceDb = async (id, deletedBy) => { const deleteDeviceDb = async (id, deletedBy) => {
const queryText = ` const queryText = `
UPDATE m_device UPDATE m_device
SET deleted_at = GETDATE(), deleted_by = $1 SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE device_id = $2 AND deleted_at IS NULL WHERE device_id = $2 AND deleted_at IS NULL
`; `;
await pool.query(queryText, [deletedBy, id]); await pool.query(queryText, [deletedBy, id]);
@@ -72,8 +102,7 @@ const softDeleteDeviceDb = async (id, deletedBy) => {
module.exports = { module.exports = {
getAllDevicesDb, getAllDevicesDb,
getDeviceByIdDb, getDeviceByIdDb,
getDeviceByCodeDb,
createDeviceDb, createDeviceDb,
updateDeviceDb, updateDeviceDb,
softDeleteDeviceDb, deleteDeviceDb,
}; };

View File

@@ -2,42 +2,28 @@ const setResponse = (data = null, message = "success", statusCode = 200) => {
const total = Array.isArray(data) ? data.length : null; const total = Array.isArray(data) ? data.length : null;
return { return {
data,
total,
message, 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 = { const response = {
message, message,
statusCode, statusCode,
data, rows: data?.data?.length,
total: data.length,
paging: { paging: {
total, current_limit: Number(queryParam.limit ?? 0),
limit, current_page: Number(queryParam.page ?? 0),
page, total_limit: data?.total,
page_total: totalPages total_page: totalPages
} },
} data: data?.data ?? []
return response
};
const setPaging = async (total, limit, page) => {
const totalPages = Math.ceil(total / limit);
const response = {
total,
limit,
page,
page_total: totalPages
} }
return response return response
@@ -86,4 +72,19 @@ function orderByClauseQuery(orderParams) {
return orderByClause 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 };

View File

@@ -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 // Users Validation
// ======================== // ========================
@@ -101,14 +71,12 @@ const userSchema = Joi.object({
'string.min': 'Password must be at least 8 characters long', 'string.min': 'Password must be at least 8 characters long',
'string.pattern.name': 'Password must contain at least one {#name}' '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 = { module.exports = {
registerSchema, registerSchema,
loginSchema, loginSchema,
newPasswordSchema, newPasswordSchema,
deviceSchema,
deviceUpdateSchema,
userSchema, userSchema,
}; };

View File

@@ -5,6 +5,6 @@ const { logger } = require("./utils/logger");
const server = http.createServer(app); const server = http.createServer(app);
const PORT = process.env.PORT || 9524; const PORT = process.env.PORT || 9530;
server.listen(PORT, () => logger.info(`Magic happening on port: ${PORT}`)); server.listen(PORT, () => logger.info(`Magic happening on port: ${PORT}`));

View File

@@ -5,10 +5,13 @@ const verifyAccess = require("../middleware/verifyAccess")
const router = express.Router(); const router = express.Router();
router.get('/', verifyToken.verifyAccessToken, DeviceController.getAll); router.route("/")
router.get('/:id', verifyToken.verifyAccessToken, DeviceController.getById); .get(verifyToken.verifyAccessToken, DeviceController.getAll)
router.post('/', verifyToken.verifyAccessToken, verifyAccess(), DeviceController.create); .post(verifyToken.verifyAccessToken, verifyAccess(), DeviceController.create);
router.put('/:id', verifyToken.verifyAccessToken, verifyAccess(), DeviceController.update);
router.delete('/:id', verifyToken.verifyAccessToken, verifyAccess(), DeviceController.delete); router.route("/:id")
.get(verifyToken.verifyAccessToken, DeviceController.getById)
.put(verifyToken.verifyAccessToken, verifyAccess(), DeviceController.update)
.delete(verifyToken.verifyAccessToken, verifyAccess(), DeviceController.delete);
module.exports = router; module.exports = router;

View File

@@ -1,81 +1,87 @@
const { const {
getAllDevicesDb, getAllDevicesDb,
getDeviceByIdDb, getDeviceByIdDb,
getDeviceByCodeDb,
createDeviceDb, createDeviceDb,
updateDeviceDb, updateDeviceDb,
softDeleteDeviceDb, deleteDeviceDb
searchDevicesDb
} = require('../db/device.db'); } = require('../db/device.db');
const { ErrorHandler } = require('../helpers/error'); const { ErrorHandler } = require('../helpers/error');
class DeviceService { class DeviceService {
// Get all devices // Get all devices
static async getAllDevices(search) { static async getAllDevices(param) {
if (!search || search.trim() === '') { try {
return await getAllDevicesDb(); const results = await getAllDevicesDb(param);
results.data.map(element => {
});
return results
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
} }
return await searchDevicesDb(search);
} }
// Get device by ID // Get device by ID
static async getDeviceById(id) { static async getDeviceById(id) {
const device = await getDeviceByIdDb(id); try {
if (!device) throw new ErrorHandler(404, 'Device not found'); const result = await getDeviceByIdDb(id);
return device;
}
// Get device by code if (result.length < 1) throw new ErrorHandler(404, 'Device not found');
static async getDeviceByCode(code) {
const device = await getDeviceByCodeDb(code); return result;
if (!device) throw new ErrorHandler(404, 'Device not found'); } catch (error) {
return device; throw new ErrorHandler(error.statusCode, error.message);
}
} }
// Create device // Create device
static async createDevice(data, userId) { static async createDevice(data) {
try {
if (!data || typeof data !== 'object') data = {}; if (!data || typeof data !== 'object') data = {};
data.created_by = userId; const result = await createDeviceDb(data);
// Cek kode unik return result;
const existingDevice = await getDeviceByCodeDb(data.device_code); } catch (error) {
if (existingDevice) { throw new ErrorHandler(error.statusCode, error.message);
throw new ErrorHandler(400, 'Device code already exists');
} }
const newDevice = await createDeviceDb(data);
return newDevice;
} }
// Update device // Update device
static async updateDevice(id, data, user_Id) { static async updateDevice(id, data) {
try {
if (!data || typeof data !== 'object') data = {}; if (!data || typeof data !== 'object') data = {};
const existingDevice = await getDeviceByIdDb(id); const dataExist = await getDeviceByIdDb(id);
if (!existingDevice) {
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Device not found'); throw new ErrorHandler(404, 'Device not found');
} }
data.updated_by = user_Id; const result = await updateDeviceDb(id, data);
const updatedDevice = await updateDeviceDb(id, data); return result;
} catch (error) {
return { throw new ErrorHandler(error.statusCode, error.message);
message: 'Device updated successfully', }
data: updatedDevice,
};
} }
// Soft delete device // Soft delete device
static async deleteDevice(id, userId) { static async deleteDevice(id, userId) {
const existingDevice = await getDeviceByIdDb(id); try {
if (!existingDevice) { const dataExist = await getDeviceByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Device not found'); throw new ErrorHandler(404, 'Device not found');
} }
await softDeleteDeviceDb(id, userId); const result = await deleteDeviceDb(id, userId);
return { message: 'Device deleted successfully' };
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
} }
} }

36
validate/device.schema.js Normal file
View File

@@ -0,0 +1,36 @@
// ========================
// Device Validation
const Joi = require("joi");
// ========================
const insertDeviceSchema = Joi.object({
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 updateDeviceSchema = Joi.object({
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);
// ✅ Export dengan CommonJS
module.exports = {
insertDeviceSchema, updateDeviceSchema
};