repair: brand device connect to sparepart
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
const BrandService = require('../services/brand.service');
|
||||
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
||||
const { createFileUploadDb } = require('../db/file_uploads.db');
|
||||
const {
|
||||
insertBrandSchema,
|
||||
updateBrandSchema,
|
||||
@@ -46,55 +45,36 @@ class BrandController {
|
||||
return res.status(response.statusCode).json(response);
|
||||
}
|
||||
|
||||
// Update brand
|
||||
// Update brand
|
||||
static async update(req, res) {
|
||||
const { id } = req.params;
|
||||
|
||||
// Debug logging untuk lihat request body
|
||||
console.log('🔍 BE Raw Request Body:', req.body);
|
||||
console.log('🔍 BE Request Headers:', req.headers);
|
||||
console.log('🔍 BE Request Method:', req.method);
|
||||
|
||||
const { error, value } = await checkValidate(updateBrandSchema, req);
|
||||
|
||||
if (error) {
|
||||
console.log('❌ BE Validation Error:', {
|
||||
error,
|
||||
details: error.details?.map(d => ({
|
||||
field: d.path.join('.'),
|
||||
message: d.message,
|
||||
value: d.context?.value
|
||||
})),
|
||||
requestBody: req.body
|
||||
});
|
||||
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||
}
|
||||
|
||||
try {
|
||||
if (req.file) {
|
||||
const file = req.file;
|
||||
const ext = require('path').extname(file.originalname).toLowerCase();
|
||||
const typeDoc = ext === ".pdf" ? "PDF" : "IMAGE";
|
||||
const folder = typeDoc === "PDF" ? "pdf" : "images";
|
||||
const pathDocument = `${folder}/${file.filename}`;
|
||||
value.updated_by = req.user?.user_id || null;
|
||||
|
||||
// Insert to file_upload table
|
||||
const fileData = {
|
||||
file_upload_name: file.originalname,
|
||||
createdBy: req.user?.user_id || null,
|
||||
};
|
||||
await createFileUploadDb(fileData);
|
||||
|
||||
if (value.error_code && Array.isArray(value.error_code)) {
|
||||
for (const errorCode of value.error_code) {
|
||||
if (errorCode.solution && Array.isArray(errorCode.solution)) {
|
||||
for (const solution of errorCode.solution) {
|
||||
if (solution.type_solution !== 'text' && (!solution.path_solution || solution.path_solution === '')) {
|
||||
solution.path_solution = pathDocument;
|
||||
solution.type_solution = typeDoc.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const results = await BrandService.updateBrandWithFullData(id, value);
|
||||
const response = await setResponse(results, 'Brand updated successfully');
|
||||
|
||||
value.updated_by = req.user?.user_id || null;
|
||||
|
||||
const results = await BrandService.updateBrandWithFullData(id, value);
|
||||
const response = await setResponse(results, 'Brand updated successfully');
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Soft delete brand by ID
|
||||
|
||||
@@ -147,9 +147,39 @@ const deleteSparepartDb = async (id, deletedBy) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
// Get multiple spareparts by IDs
|
||||
const getSparepartsByIdsDb = async (sparepartIds) => {
|
||||
if (!sparepartIds || sparepartIds.length === 0) return [];
|
||||
|
||||
const placeholders = sparepartIds.map((_, index) => `$${index + 1}`).join(', ');
|
||||
const queryText = `
|
||||
SELECT
|
||||
sparepart_id,
|
||||
sparepart_name,
|
||||
sparepart_code,
|
||||
sparepart_description,
|
||||
sparepart_model,
|
||||
sparepart_foto,
|
||||
sparepart_item_type,
|
||||
sparepart_qty,
|
||||
sparepart_unit,
|
||||
sparepart_merk,
|
||||
sparepart_stok,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM m_sparepart
|
||||
WHERE sparepart_id IN (${placeholders})
|
||||
AND deleted_at IS NULL
|
||||
`;
|
||||
|
||||
const result = await pool.query(queryText, sparepartIds);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getAllSparepartDb,
|
||||
getSparepartByIdDb,
|
||||
getSparepartsByIdsDb,
|
||||
checkSparepartNameExistsDb,
|
||||
createSparepartDb,
|
||||
updateSparepartDb,
|
||||
|
||||
@@ -2,7 +2,6 @@ const express = require('express');
|
||||
const BrandController = require('../controllers/brand.controller');
|
||||
const verifyToken = require('../middleware/verifyToken');
|
||||
const verifyAccess = require('../middleware/verifyAccess');
|
||||
const upload = require('../middleware/uploads');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -12,7 +11,7 @@ router.route('/')
|
||||
|
||||
router.route('/:id')
|
||||
.get(verifyToken.verifyAccessToken, BrandController.getById)
|
||||
.put(verifyToken.verifyAccessToken, verifyAccess(), upload.single('file'), BrandController.update)
|
||||
.put(verifyToken.verifyAccessToken, verifyAccess(), BrandController.update)
|
||||
.delete(verifyToken.verifyAccessToken, verifyAccess(), BrandController.delete);
|
||||
|
||||
module.exports = router;
|
||||
@@ -2,7 +2,6 @@
|
||||
const {
|
||||
getAllBrandsDb,
|
||||
getBrandByIdDb,
|
||||
getBrandByNameDb,
|
||||
createBrandDb,
|
||||
updateBrandDb,
|
||||
deleteBrandDb,
|
||||
@@ -12,7 +11,6 @@ const {
|
||||
const {
|
||||
insertMultipleBrandSparepartsDb,
|
||||
updateBrandSparepartsDb,
|
||||
deleteAllBrandSparepartsDb,
|
||||
getSparepartsByBrandIdDb,
|
||||
} = require("../db/brand_sparepart.db");
|
||||
|
||||
@@ -24,6 +22,9 @@ const {
|
||||
deleteErrorCodeDb,
|
||||
} = require("../db/brand_code.db");
|
||||
|
||||
// Sparepart operations
|
||||
const { getSparepartsByIdsDb } = require("../db/sparepart.db");
|
||||
|
||||
// Solution operations
|
||||
const {
|
||||
getSolutionsByErrorCodeIdDb,
|
||||
@@ -236,12 +237,33 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
// Validate sparepart id
|
||||
static async validateSparepartIds(sparepartIds) {
|
||||
if (!sparepartIds || !Array.isArray(sparepartIds) || sparepartIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const existingSpareparts = await getSparepartsByIdsDb(sparepartIds);
|
||||
|
||||
if (existingSpareparts.length !== sparepartIds.length) {
|
||||
const existingIds = existingSpareparts.map(sp => sp.sparepart_id);
|
||||
const invalidIds = sparepartIds.filter(id => !existingIds.includes(id));
|
||||
throw new ErrorHandler(400, `Invalid sparepart IDs: ${invalidIds.join(', ')}`);
|
||||
}
|
||||
|
||||
return existingSpareparts;
|
||||
}
|
||||
|
||||
// Update brand
|
||||
static async updateBrandWithFullData(id, data) {
|
||||
try {
|
||||
const existingBrand = await getBrandByIdDb(id);
|
||||
if (!existingBrand) throw new ErrorHandler(404, "Brand not found");
|
||||
|
||||
if (data.spareparts) {
|
||||
await this.validateSparepartIds(data.spareparts);
|
||||
}
|
||||
|
||||
if (data.brand_name && data.brand_name !== existingBrand.brand_name) {
|
||||
const brandExists = await checkBrandNameExistsDb(data.brand_name, id);
|
||||
if (brandExists) {
|
||||
|
||||
Reference in New Issue
Block a user