From cdb9a7e0ef19a71c92d6be0e25d60554ab1d59a7 Mon Sep 17 00:00:00 2001 From: Antony Kurniawan Date: Wed, 1 Oct 2025 14:47:58 +0700 Subject: [PATCH] fix: get all device --- controllers/device.controller.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/controllers/device.controller.js b/controllers/device.controller.js index 4de3e9d..eeb5ef4 100644 --- a/controllers/device.controller.js +++ b/controllers/device.controller.js @@ -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) { + 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(