You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
3.8 KiB

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.getAllCategoryOfStore = 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;
});
res.status(200).json({
success: true,
data: categoryData
})
} catch (e) {
console.log("ERROR : ", e)
return next(new ErrorResponse(e, 500));
}
})
exports.createCategory = asyncHandler(async (req, res, next) => {
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)
console.log("DATA : ", 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";
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));
}
})
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 {
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";
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(e, 500));
}
})