Compare commits
4 Commits
446e393ee8
...
ec81b4b311
| Author | SHA1 | Date | |
|---|---|---|---|
| ec81b4b311 | |||
| 373caf307b | |||
| cdb9a7e0ef | |||
| 8d2a8565ff |
@@ -1,18 +1,17 @@
|
||||
const DeviceService = require('../services/device.service');
|
||||
const { deviceSchema } = require('../helpers/validation');
|
||||
const { deviceSchema, deviceUpdateSchema } = require('../helpers/validation');
|
||||
const { setResponse } = require('../helpers/utils');
|
||||
|
||||
class DeviceController {
|
||||
// Get all devices
|
||||
static async getAll(req, res) {
|
||||
try {
|
||||
const devices = await DeviceService.getAllDevices();
|
||||
return res.status(200).json(
|
||||
setResponse(devices, 'Devices retrieved successfully', 200)
|
||||
);
|
||||
const { search } = req.query;
|
||||
const devices = await DeviceService.getAllDevices(search || '');
|
||||
return res.status(200).json(setResponse(devices, 'Devices retrieved successfully', 200));
|
||||
} catch (err) {
|
||||
return res.status(err.statusCode || 500).json(
|
||||
setResponse([], err.message || 'Failed to get devices', err.statusCode || 500)
|
||||
setResponse([], err.message || 'Failed to retrieve devices', err.statusCode || 500)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -22,12 +21,10 @@ class DeviceController {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const device = await DeviceService.getDeviceById(id);
|
||||
return res.status(200).json(
|
||||
setResponse(device, 'Device retrieved successfully', 200)
|
||||
);
|
||||
return res.status(200).json(setResponse(device, 'Device retrieved successfully', 200));
|
||||
} catch (err) {
|
||||
return res.status(err.statusCode || 500).json(
|
||||
setResponse([], err.message || 'Failed to get device', err.statusCode || 500)
|
||||
setResponse([], err.message || 'Device not found', err.statusCode || 500)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -61,7 +58,7 @@ class DeviceController {
|
||||
static async update(req, res) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { error, value } = deviceSchema.validate(req.body || {}, { abortEarly: false });
|
||||
const { error, value } = deviceUpdateSchema.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);
|
||||
@@ -72,9 +69,10 @@ class DeviceController {
|
||||
return res.status(400).json(setResponse(errors, 'Validation failed', 400));
|
||||
}
|
||||
|
||||
await DeviceService.updateDevice(id, value, req.user.userId);
|
||||
const updatedDevice = await DeviceService.updateDevice(id, value, req.user.userId);
|
||||
|
||||
return res.status(200).json(
|
||||
setResponse([], 'Device updated successfully', 200)
|
||||
setResponse(updatedDevice.data, 'Device updated successfully', 200)
|
||||
);
|
||||
} catch (err) {
|
||||
return res.status(err.statusCode || 500).json(
|
||||
|
||||
@@ -12,6 +12,25 @@ const getAllDevicesDb = async () => {
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Search devices by keyword
|
||||
const searchDevicesDb = async (keyword) => {
|
||||
const queryText = `
|
||||
SELECT *
|
||||
FROM m_device
|
||||
WHERE deleted_at IS NULL
|
||||
AND (
|
||||
device_name LIKE '%' + $1 + '%'
|
||||
OR device_code LIKE '%' + $1 + '%'
|
||||
OR device_location LIKE '%' + $1 + '%'
|
||||
OR ip_address LIKE '%' + $1 + '%'
|
||||
OR device_description LIKE '%' + $1 + '%'
|
||||
)
|
||||
ORDER BY device_id ASC
|
||||
`;
|
||||
const result = await pool.query(queryText, [keyword]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get device by ID
|
||||
const getDeviceByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
@@ -73,4 +92,5 @@ module.exports = {
|
||||
createDeviceDb,
|
||||
updateDeviceDb,
|
||||
softDeleteDeviceDb,
|
||||
searchDevicesDb,
|
||||
};
|
||||
|
||||
@@ -26,7 +26,6 @@ const registerSchema = Joi.object({
|
||||
'string.pattern.name': 'Password must contain at least one {#name}'
|
||||
})
|
||||
});
|
||||
|
||||
const loginSchema = Joi.object({
|
||||
email: Joi.string().email().required(),
|
||||
password: Joi.string().required(),
|
||||
@@ -51,8 +50,22 @@ const deviceSchema = Joi.object({
|
||||
})
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
module.exports = {
|
||||
registerSchema,
|
||||
loginSchema,
|
||||
deviceSchema
|
||||
deviceSchema,
|
||||
deviceUpdateSchema
|
||||
};
|
||||
|
||||
@@ -4,23 +4,24 @@ const {
|
||||
getDeviceByCodeDb,
|
||||
createDeviceDb,
|
||||
updateDeviceDb,
|
||||
softDeleteDeviceDb
|
||||
softDeleteDeviceDb,
|
||||
searchDevicesDb
|
||||
} = require('../db/device.db');
|
||||
const { ErrorHandler } = require('../helpers/error');
|
||||
|
||||
class DeviceService {
|
||||
// Get all devices
|
||||
static async getAllDevices() {
|
||||
const devices = await getAllDevicesDb();
|
||||
return devices;
|
||||
static async getAllDevices(search) {
|
||||
if (!search || search.trim() === '') {
|
||||
return await getAllDevicesDb();
|
||||
}
|
||||
return await searchDevicesDb(search);
|
||||
}
|
||||
|
||||
// Get device by ID
|
||||
static async getDeviceById(id) {
|
||||
const device = await getDeviceByIdDb(id);
|
||||
if (!device) {
|
||||
throw new ErrorHandler(404, 'Device not found');
|
||||
}
|
||||
if (!device) throw new ErrorHandler(404, 'Device not found');
|
||||
return device;
|
||||
}
|
||||
|
||||
@@ -38,7 +39,6 @@ class DeviceService {
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
|
||||
data.created_by = userId;
|
||||
data.is_active = 1;
|
||||
|
||||
// cek kode unik
|
||||
const existingDevice = await getDeviceByCodeDb(data.device_code);
|
||||
@@ -60,10 +60,15 @@ class DeviceService {
|
||||
}
|
||||
|
||||
data.updated_by = userId;
|
||||
data.updated_at = new Date();
|
||||
|
||||
const updatedDevice = await updateDeviceDb(id, data);
|
||||
|
||||
|
||||
await updateDeviceDb(id, data);
|
||||
return { message: 'Device updated successfully' };
|
||||
return {
|
||||
message: 'Device updated successfully',
|
||||
data: updatedDevice,
|
||||
};
|
||||
}
|
||||
|
||||
// Soft delete device
|
||||
|
||||
Reference in New Issue
Block a user