Files
cod-api/controllers/error_code.controller.js

74 lines
2.5 KiB
JavaScript

const ErrorCodeService = require('../services/error_code.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const {
insertErrorCodeSchema,
updateErrorCodeSchema,
} = require('../validate/error_code.schema');
class ErrorCodeController {
static async getByBrandId(req, res) {
const { brandId } = req.params;
const queryParams = req.query;
const results = await ErrorCodeService.getErrorCodesByBrandId(brandId, 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) {
const { id } = req.params;
const result = await ErrorCodeService.getErrorCodeById(id);
const response = setResponse(result, 'Error code found');
res.status(response.statusCode).json(response);
}
// Create error code with solutions and spareparts
static async create(req, res) {
const { error, value } = await checkValidate(insertErrorCodeSchema, req);
if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400));
}
const { brandId } = req.params;
value.created_by = req.user?.user_id || null;
const result = await ErrorCodeService.createErrorCodeWithFullData(brandId, value);
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) {
const { error, value } = await checkValidate(updateErrorCodeSchema, req);
if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400));
}
const { brandId, errorCodeId } = req.params;
value.updated_by = req.user?.user_id || null;
const result = await ErrorCodeService.updateErrorCodeWithFullData(brandId, errorCodeId, value);
const response = setResponse(result, 'Error code updated successfully');
res.status(response.statusCode).json(response);
}
// Soft delete error code
static async delete(req, res) {
const { brandId, errorCodeId } = req.params;
const deletedBy = req.user?.user_id || null;
const result = await ErrorCodeService.deleteErrorCode(brandId, errorCodeId, deletedBy);
const response = setResponse(result, 'Error code deleted successfully');
res.status(response.statusCode).json(response);
}
}
module.exports = ErrorCodeController;