diff --git a/Controller/Category.js b/Controller/Category.js index 7c2ab78..1615436 100644 --- a/Controller/Category.js +++ b/Controller/Category.js @@ -13,7 +13,7 @@ const Location = process.env.ITEM_CATEGORY_LOCATION; const Detail_Location = process.env.ITEM_CATEGORY_DETAIL_LOCATION; -exports.getAllCategoryOfStore = asyncHandler(async (req, res, next) => { +exports.getAllBrand = asyncHandler(async (req, res, next) => { try { var categories = await CategoryModel.findAll({ where: { @@ -27,18 +27,37 @@ exports.getAllCategoryOfStore = asyncHandler(async (req, res, next) => { 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: categoryData - }) + data: categoriesWithDetails + }); + + } catch (e) { console.log("ERROR : ", e) return next(new ErrorResponse(e, 500)); } }) -exports.createCategory = asyncHandler(async (req, res, next) => { + +exports.createBrand = asyncHandler(async (req, res, next) => { if (!req.files) { return next(new ErrorResponse("Please upload a file", 400)) @@ -47,7 +66,6 @@ exports.createCategory = asyncHandler(async (req, res, next) => { return next(new ErrorResponse("Please provide data", 400)) } var data = JSON.parse(req.body.data) - console.log("DATA : ", data) var date = dateFormat(new Date(), "yyyy-mm-dd") if (!data.DESC_KH || !data.DESC_EN) { @@ -55,8 +73,6 @@ exports.createCategory = asyncHandler(async (req, res, next) => { } try { - - const category = await CategoryModel.create({ DB_CODE: req.user.DB_CODE, ANALYSIS_ID: "", @@ -69,28 +85,123 @@ exports.createCategory = asyncHandler(async (req, res, next) => { }) 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); + } - 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) - fs.unlinkSync(req.files.photo[0].path); - return next(new ErrorResponse("Server Error", 500)); + 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.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, @@ -106,12 +217,23 @@ exports.createCategoryDetail = asyncHandler(async (req, res, next) => { ); const fileName = Detail_Location + req.user.DB_CODE + "_" + cateDetail.ID + ".jpg"; - fs.copyFileSync(req.files.photo[0].path, fileName); - fs.unlinkSync(req.files.photo[0].path); + + // 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) - fs.unlinkSync(req.files.photo[0].path); + if (req.files.photo) { + fs.unlinkSync(req.files.photo[0].path); + } return next(new ErrorResponse(e, 500)); } }) \ No newline at end of file diff --git a/Controller/Items.js b/Controller/Items.js index 4787c4e..f537dca 100644 --- a/Controller/Items.js +++ b/Controller/Items.js @@ -10,6 +10,7 @@ const fs = require('fs') const dateFormat = require("dateformat"); const path = require('path'); const { log } = require("console"); +const CategoryDetail = require("../Model/CategoryDetailModel"); exports.getAllItemsByStore = asyncHandler(async (req, res, next) => { @@ -32,10 +33,10 @@ exports.getAllItemsByStore = asyncHandler(async (req, res, next) => { try { // Get category name - var category = await CategoryModel.findOne({ + var category = await CategoryDetail.findOne({ where: { ID: item.CAT_CODE, - DB_CODE: req.user.DB_CODE + // DB_CODE: req.user.DB_CODE } }); if (category) { diff --git a/Controller/Warehouse.js b/Controller/Warehouse.js index aab215a..31480e0 100644 --- a/Controller/Warehouse.js +++ b/Controller/Warehouse.js @@ -49,7 +49,7 @@ exports.createWarehouse = asyncHandler(async (req, res, next) => { USER_CODE: req.user.USER_CODE, WAR_STAT: 'A' }, { transaction: t }) - res.status(200).json(new SuccessResponse(200, warehouse)) + res.status(200).json(new SuccessResponse("Operation Successful", warehouse)) }) } catch (e) { console.log("ERROR CREATING WAREHOUSE ", e) diff --git a/routes/category.js b/routes/category.js index 710c57d..daad30e 100644 --- a/routes/category.js +++ b/routes/category.js @@ -18,9 +18,11 @@ var storage = multer.diskStorage({ }); var upload = multer({ storage: storage }); -route.route("/").get(protect, controller.getAllCategoryOfStore); +route.route("/").get(protect, controller.getAllBrand); +route.route("/").post(protect, upload.fields([{ name: "photo", maxCount: 1 }]), controller.createBrand); + +route.route("/category_detail").get(protect, controller.getAllCategory); -route.route("/").post(protect, upload.fields([{ name: "photo", maxCount: 1 }]), controller.createCategory); route.route("/category_detail").post(protect, upload.fields([{ name: "photo", maxCount: 1 }]), controller.createCategoryDetail);