diff --git a/controllers/error_code.controller.js b/controllers/error_code.controller.js index 07ff945..3e520ca 100644 --- a/controllers/error_code.controller.js +++ b/controllers/error_code.controller.js @@ -1,104 +1,70 @@ const ErrorCodeService = require('../services/error_code.service'); const { setResponse, setResponsePaging } = require('../helpers/utils'); -const { ErrorHandler } = require('../helpers/error'); class ErrorCodeController { - // Get all error codes with pagination and search - static async getAll(req, res) { - try { - const queryParams = req.query; - const results = await ErrorCodeService.getAllErrorCodes(queryParams); - const response = await setResponsePaging(queryParams, results, 'Error codes found'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } + static async getByBrandId(req, res) { + const { brandId } = req.params; + const queryParams = { + ...req.query, + brand_id: brandId + }; + + const results = await ErrorCodeService.getAllErrorCodes(queryParams); + const response = await setResponsePaging(queryParams, results, 'Error codes found'); + + res.status(response.statusCode).json(response); } // Get error code by ID static async getById(req, res) { - try { - const { id } = req.params; - const result = await ErrorCodeService.getErrorCodeById(id); - const response = setResponse(result, 200, 'Error code found'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } - } + const { id } = req.params; + const result = await ErrorCodeService.getErrorCodeById(id); + const response = setResponse(result, 'Error code found'); - // Get error codes by brand ID with pagination and search - static async getByBrandId(req, res) { - try { - const { brandId } = req.params; - const queryParams = { - ...req.query, - brand_id: brandId - }; - const results = await ErrorCodeService.getAllErrorCodes(queryParams); - const response = await setResponsePaging(queryParams, results, 'Error codes found'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } + res.status(response.statusCode).json(response); } // Create error code with solutions and spareparts static async create(req, res) { - try { - const { brandId } = req.params; - const createdBy = req.user?.user_id || null; + const { brandId } = req.params; + const createdBy = req.user?.user_id || null; - const data = { - ...req.body, - created_by: createdBy - }; + const data = { + ...req.body, + created_by: createdBy + }; - const result = await ErrorCodeService.createErrorCodeWithFullData(brandId, data); - const response = setResponse(result, 201, 'Error code created successfully'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } + const result = await ErrorCodeService.createErrorCodeWithFullData(brandId, data); + const response = setResponse(result, 'Error code created successfully'); + + res.status(response.statusCode).json(response); } // Update error code with solutions and spareparts static async update(req, res) { - try { - const { brandId, errorCode } = req.params; - const updatedBy = req.user?.user_id || null; + const { brandId, errorCode } = req.params; + const updatedBy = req.user?.user_id || null; - const data = { - ...req.body, - updated_by: updatedBy - }; + const data = { + ...req.body, + updated_by: updatedBy + }; - const result = await ErrorCodeService.updateErrorCodeWithFullData(brandId, errorCode, data); - const response = setResponse(result, 200, 'Error code updated successfully'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } + const result = await ErrorCodeService.updateErrorCodeWithFullData(brandId, errorCode, data); + const response = setResponse(result, 'Error code updated successfully'); + + res.status(response.statusCode).json(response); } // Soft delete error code static async delete(req, res) { - try { - const { brandId, errorCode } = req.params; - const deletedBy = req.user?.user_id || null; + const { brandId, errorCode } = req.params; + const deletedBy = req.user?.user_id || null; - const result = await ErrorCodeService.deleteErrorCode(brandId, errorCode, deletedBy); - const response = setResponse(result, 200, 'Error code deleted successfully'); - res.status(response.statusCode).json(response); - } catch (error) { - const response = setResponse(error.message, error.statusCode || 500); - res.status(response.statusCode).json(response); - } + const result = await ErrorCodeService.deleteErrorCode(brandId, errorCode, deletedBy); + const response = setResponse(result, 'Error code deleted successfully'); + + res.status(response.statusCode).json(response); } }