const asyncHandler = require("../Middleware/async"); const sequelize = require('sequelize') const CategoryModel = require("../Model/CategoryModel"); const CategoryDetailModel = require("../Model/CategoryDetailModel"); const ErrorResponse = require("../utils/errorResponse"); const SuccessResponse = require("../utils/successResponse"); const db = require("../Config/db") const fs = require('fs') const dotenv = require('dotenv') const dateFormat = require("dateformat"); const Location = process.env.ITEM_CATEGORY_LOCATION; const Detail_Location = process.env.ITEM_CATEGORY_DETAIL_LOCATION; exports.getAllBrand = asyncHandler(async (req, res, next) => { try { var categories = await CategoryModel.findAll({ where: { DB_CODE: req.user.DB_CODE } }) const categoryData = categories.map(category => { let categoryObject = category.get({ plain: true }); categoryObject.image = `${Location}${req.user.DB_CODE}_${category.ID}.jpg`; return categoryObject; }); const categoriesWithDetails = await Promise.all(categories.map(async (category) => { const categoryDetails = await CategoryDetailModel.findAll({ where: { STATUS: 1, CATE_ID: category.ID } }); console.log("CATEGORY DETAIL ", categoryDetails); // Return a new object merging the category data with its details return { ...category.get({ plain: true }), // This assumes you're using Sequelize CATEGORY_DETAIL: categoryDetails.map(detail => detail.get({ plain: true })) }; })); res.status(200).json({ success: true, data: categoriesWithDetails }); } catch (e) { console.log("ERROR : ", e) return next(new ErrorResponse(e, 500)); } }) exports.createBrand = asyncHandler(async (req, res, next) => { console.log("REQ FILES ", req.files) if (!req.files) { return next(new ErrorResponse("Please upload a file", 400)) } if (!req.body.data) { return next(new ErrorResponse("Please provide data", 400)) } var data = JSON.parse(req.body.data) var date = dateFormat(new Date(), "yyyy-mm-dd") if (!data.DESC_KH || !data.DESC_EN) { return next(new ErrorResponse("Please provide all required fields", 400)) } try { const category = await CategoryModel.create({ DB_CODE: req.user.DB_CODE, ANALYSIS_ID: "", DESC_KH: data.DESC_KH, DESC_EN: data.DESC_EN, DESC_CN: data.DESC_CN, CREATED_DATE: date, CREATED_BY: req.user.USER_ID, STATUS: 1 }) const fileName = Location + req.user.DB_CODE + "_" + category.ID + ".jpg"; if (req.files.photo) { fs.copyFileSync(req.files.photo[0].path, fileName); fs.unlinkSync(req.files.photo[0].path); } res.send(new SuccessResponse("Operation Successful")); } catch (e) { console.log("ERROR : ", e) if (req.files.photo) { fs.unlinkSync(req.files.photo[0].path); } return next(new ErrorResponse(`Server Error ${e}`, 500)); } }) exports.getCategoryDetailByID = asyncHandler(async (req, res, next) => { try { const { CATE_ID } = req.body; if (!CATE_ID) return next(new ErrorResponse("Please provide brand information", 400)); var categoryDetails = await CategoryDetailModel.findAll({ where: { STATUS: 1, CATE_ID: CATE_ID } }) if (categoryDetails.length == 0) return next(new ErrorResponse("No data found", 400)); const categoryDetailData = categoryDetails.map(categoryDetail => { let categoryDetailObject = categoryDetail.get({ plain: true }); categoryDetailObject.image = `${Detail_Location}${req.user.DB_CODE}_${categoryDetail.ID}.jpg`; return categoryDetailObject; }); res.status(200).json({ success: true, data: categoryDetailData }) } catch (e) { console.log("ERROR : ", e) return next(new ErrorResponse(e, 500)); } }) exports.getAllCategory = asyncHandler(async (req, res, next) => { try { const categories = await CategoryModel.findAll({ where: { DB_CODE: req.user.DB_CODE } }); if (!categories || categories.length === 0) { return next(new ErrorResponse("No data found", 400)); } // Process each category to attach details with additional brand description const categoriesWithDetails = await Promise.all(categories.map(async (category) => { const categoryDetails = await CategoryDetailModel.findAll({ where: { STATUS: 1, CATE_ID: category.ID } }); console.log("CATEGORY DETAIL ", categoryDetails); // Map each detail to include the brand's English description const detailedCategory = categoryDetails.map(detail => { return { ...detail.get({ plain: true }), // Get plain detail object BRAND_DESC_EN: category.get({ plain: true }).DESC_EN // Add brand description from the parent category }; }); return detailedCategory; // Return an array of detailed categories })); // Flatten the array of arrays to a single array of all details const flatDetails = categoriesWithDetails.flat(); res.status(200).json({ success: true, data: flatDetails }); } catch (e) { console.log("ERROR : ", e); return next(new ErrorResponse("Server error", 500)); } }); exports.createCategoryDetail = asyncHandler(async (req, res, next) => { // if (!req.files || !req.files.photo) // return next(new ErrorResponse("Please input photo file", 403)); if (!req.body.data) return next(new ErrorResponse("Please input data json", 403)); var data = JSON.parse(req.body.data); var date = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"); try { // check whether CATE_ID is exist or not const category = await CategoryModel.findOne({ where: { ID: data.CATE_ID } }) if (!category) { return next(new ErrorResponse("Brand not found", 404)); } const cateDetail = await CategoryDetailModel.create( { DB_CODE: req.user.DB_CODE, CATE_ID: data.CATE_ID, ANALYSIS_ID: "", DESC_KH: data.DESC_KH, DESC_EN: data.DESC_EN, DESC_CN: data.DESC_CN, CREATED_DATE: date, CREATED_BY: req.user.USER_ID, STATUS: 1 } ); const fileName = Detail_Location + req.user.DB_CODE + "_" + cateDetail.ID + ".jpg"; // chcek if req file pass some value if (req.files.photo) { fs.copyFileSync(req.files.photo[0].path, fileName); fs.unlinkSync(req.files) } // if (req.files || req.files.photo) { // fs.copyFileSync(req.files.photo[0].path, fileName); // fs.unlinkSync(req.files.photo[0].path); // } res.send(new SuccessResponse("Operation Successful")); } catch (e) { console.log("ERROR : ", e) if (req.files.photo) { fs.unlinkSync(req.files.photo[0].path); } return next(new ErrorResponse(e, 500)); } })