Compare commits
4 Commits
446e393ee8
...
ec81b4b311
| Author | SHA1 | Date | |
|---|---|---|---|
| ec81b4b311 | |||
| 373caf307b | |||
| cdb9a7e0ef | |||
| 8d2a8565ff |
@@ -1,18 +1,17 @@
|
|||||||
const DeviceService = require('../services/device.service');
|
const DeviceService = require('../services/device.service');
|
||||||
const { deviceSchema } = require('../helpers/validation');
|
const { deviceSchema, deviceUpdateSchema } = require('../helpers/validation');
|
||||||
const { setResponse } = require('../helpers/utils');
|
const { setResponse } = require('../helpers/utils');
|
||||||
|
|
||||||
class DeviceController {
|
class DeviceController {
|
||||||
// Get all devices
|
// Get all devices
|
||||||
static async getAll(req, res) {
|
static async getAll(req, res) {
|
||||||
try {
|
try {
|
||||||
const devices = await DeviceService.getAllDevices();
|
const { search } = req.query;
|
||||||
return res.status(200).json(
|
const devices = await DeviceService.getAllDevices(search || '');
|
||||||
setResponse(devices, 'Devices retrieved successfully', 200)
|
return res.status(200).json(setResponse(devices, 'Devices retrieved successfully', 200));
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(err.statusCode || 500).json(
|
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 {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const device = await DeviceService.getDeviceById(id);
|
const device = await DeviceService.getDeviceById(id);
|
||||||
return res.status(200).json(
|
return res.status(200).json(setResponse(device, 'Device retrieved successfully', 200));
|
||||||
setResponse(device, 'Device retrieved successfully', 200)
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(err.statusCode || 500).json(
|
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) {
|
static async update(req, res) {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { error, value } = deviceSchema.validate(req.body || {}, { abortEarly: false });
|
const { error, value } = deviceUpdateSchema.validate(req.body || {}, { abortEarly: false });
|
||||||
if (error) {
|
if (error) {
|
||||||
const errors = error.details.reduce((acc, cur) => {
|
const errors = error.details.reduce((acc, cur) => {
|
||||||
const field = Array.isArray(cur.path) ? cur.path.join('.') : String(cur.path);
|
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));
|
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(
|
return res.status(200).json(
|
||||||
setResponse([], 'Device updated successfully', 200)
|
setResponse(updatedDevice.data, 'Device updated successfully', 200)
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(err.statusCode || 500).json(
|
return res.status(err.statusCode || 500).json(
|
||||||
|
|||||||
@@ -12,6 +12,25 @@ const getAllDevicesDb = async () => {
|
|||||||
return result.recordset;
|
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
|
// Get device by ID
|
||||||
const getDeviceByIdDb = async (id) => {
|
const getDeviceByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
@@ -73,4 +92,5 @@ module.exports = {
|
|||||||
createDeviceDb,
|
createDeviceDb,
|
||||||
updateDeviceDb,
|
updateDeviceDb,
|
||||||
softDeleteDeviceDb,
|
softDeleteDeviceDb,
|
||||||
|
searchDevicesDb,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ const registerSchema = Joi.object({
|
|||||||
'string.pattern.name': 'Password must contain at least one {#name}'
|
'string.pattern.name': 'Password must contain at least one {#name}'
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const loginSchema = Joi.object({
|
const loginSchema = Joi.object({
|
||||||
email: Joi.string().email().required(),
|
email: Joi.string().email().required(),
|
||||||
password: Joi.string().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 = {
|
module.exports = {
|
||||||
registerSchema,
|
registerSchema,
|
||||||
loginSchema,
|
loginSchema,
|
||||||
deviceSchema
|
deviceSchema,
|
||||||
|
deviceUpdateSchema
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,23 +4,24 @@ const {
|
|||||||
getDeviceByCodeDb,
|
getDeviceByCodeDb,
|
||||||
createDeviceDb,
|
createDeviceDb,
|
||||||
updateDeviceDb,
|
updateDeviceDb,
|
||||||
softDeleteDeviceDb
|
softDeleteDeviceDb,
|
||||||
|
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() {
|
static async getAllDevices(search) {
|
||||||
const devices = await getAllDevicesDb();
|
if (!search || search.trim() === '') {
|
||||||
return devices;
|
return await getAllDevicesDb();
|
||||||
|
}
|
||||||
|
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);
|
const device = await getDeviceByIdDb(id);
|
||||||
if (!device) {
|
if (!device) throw new ErrorHandler(404, 'Device not found');
|
||||||
throw new ErrorHandler(404, 'Device not found');
|
|
||||||
}
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +39,6 @@ class DeviceService {
|
|||||||
if (!data || typeof data !== 'object') data = {};
|
if (!data || typeof data !== 'object') data = {};
|
||||||
|
|
||||||
data.created_by = userId;
|
data.created_by = userId;
|
||||||
data.is_active = 1;
|
|
||||||
|
|
||||||
// cek kode unik
|
// cek kode unik
|
||||||
const existingDevice = await getDeviceByCodeDb(data.device_code);
|
const existingDevice = await getDeviceByCodeDb(data.device_code);
|
||||||
@@ -60,10 +60,15 @@ class DeviceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data.updated_by = userId;
|
data.updated_by = userId;
|
||||||
data.updated_at = new Date();
|
|
||||||
|
const updatedDevice = await updateDeviceDb(id, data);
|
||||||
|
|
||||||
|
|
||||||
await updateDeviceDb(id, data);
|
await updateDeviceDb(id, data);
|
||||||
return { message: 'Device updated successfully' };
|
return {
|
||||||
|
message: 'Device updated successfully',
|
||||||
|
data: updatedDevice,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Soft delete device
|
// Soft delete device
|
||||||
|
|||||||
Reference in New Issue
Block a user