repair: brand + errorcode
This commit is contained in:
@@ -25,7 +25,6 @@ const {
|
||||
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
||||
|
||||
class ErrorCodeService {
|
||||
// Get all error codes with pagination and search (without solutions and spareparts)
|
||||
static async getAllErrorCodes(param) {
|
||||
try {
|
||||
const results = await getAllErrorCodesDb(param);
|
||||
@@ -169,19 +168,34 @@ class ErrorCodeService {
|
||||
}
|
||||
|
||||
// Update error code with solutions and spareparts
|
||||
static async updateErrorCodeWithFullData(brandId, errorCode, data) {
|
||||
static async updateErrorCodeWithFullData(brandId, errorCodeId, data) {
|
||||
try {
|
||||
const existingErrorCode = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
||||
const existingErrorCode = await getErrorCodeByIdDb(errorCodeId);
|
||||
if (!existingErrorCode) throw new ErrorHandler(404, "Error code not found");
|
||||
|
||||
await updateErrorCodeDb(brandId, errorCode, {
|
||||
error_code_name: data.error_code_name,
|
||||
error_code_description: data.error_code_description,
|
||||
error_code_color: data.error_code_color,
|
||||
path_icon: data.path_icon,
|
||||
is_active: data.is_active,
|
||||
updated_by: data.updated_by,
|
||||
});
|
||||
// Verify the error code belongs to the specified brand
|
||||
if (existingErrorCode.brand_id !== parseInt(brandId)) {
|
||||
throw new ErrorHandler(403, "Error code does not belong to specified brand");
|
||||
}
|
||||
|
||||
// Check if there are any error code fields to update
|
||||
const hasMainFieldUpdate =
|
||||
data.error_code_name !== undefined ||
|
||||
data.error_code_description !== undefined ||
|
||||
data.error_code_color !== undefined ||
|
||||
data.path_icon !== undefined ||
|
||||
data.is_active !== undefined;
|
||||
|
||||
if (hasMainFieldUpdate) {
|
||||
await updateErrorCodeDb(brandId, existingErrorCode.error_code, {
|
||||
error_code_name: data.error_code_name,
|
||||
error_code_description: data.error_code_description,
|
||||
error_code_color: data.error_code_color,
|
||||
path_icon: data.path_icon,
|
||||
is_active: data.is_active,
|
||||
updated_by: data.updated_by,
|
||||
});
|
||||
}
|
||||
|
||||
if (data.spareparts && Array.isArray(data.spareparts)) {
|
||||
await updateErrorCodeSparepartsDb(existingErrorCode.error_code_id, data.spareparts, data.updated_by);
|
||||
@@ -235,15 +249,20 @@ class ErrorCodeService {
|
||||
}
|
||||
|
||||
// Soft delete error code
|
||||
static async deleteErrorCode(brandId, errorCode, deletedBy) {
|
||||
static async deleteErrorCode(brandId, errorCodeId, deletedBy) {
|
||||
try {
|
||||
const errorCodeExist = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
||||
const errorCodeExist = await getErrorCodeByIdDb(errorCodeId);
|
||||
|
||||
if (!errorCodeExist) {
|
||||
throw new ErrorHandler(404, "Error code not found");
|
||||
}
|
||||
|
||||
const result = await deleteErrorCodeDb(brandId, errorCode, deletedBy);
|
||||
// Verify the error code belongs to the specified brand
|
||||
if (errorCodeExist.brand_id !== parseInt(brandId)) {
|
||||
throw new ErrorHandler(403, "Error code does not belong to specified brand");
|
||||
}
|
||||
|
||||
const result = await deleteErrorCodeDb(brandId, errorCodeExist.error_code, deletedBy);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user