fix: get all device

This commit is contained in:
2025-10-01 14:47:58 +07:00
parent 8d2a8565ff
commit cdb9a7e0ef

View File

@@ -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(