parent
89477bb0c2
commit
cad8ce36af
20 changed files with 600 additions and 55 deletions
@ -1 +1,2 @@ |
||||
node_modules |
||||
node_modules |
||||
uploads |
@ -0,0 +1,12 @@ |
||||
const CATEGORY = require('../Model/CategoryModel'); |
||||
const CATEGORY_DETAIL = require('../Model/CategoryDetailModel'); |
||||
|
||||
module.exports = async () => { |
||||
|
||||
CATEGORY.hasMany(CATEGORY_DETAIL, { |
||||
foreignKey: 'CATE_ID' |
||||
}); |
||||
CATEGORY_DETAIL.belongsTo(CATEGORY_DETAIL, { |
||||
foreignKey: 'CATE_ID' |
||||
}); |
||||
} |
@ -0,0 +1,117 @@ |
||||
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)); |
||||
} |
||||
}) |
@ -0,0 +1,52 @@ |
||||
const asyncHandler = require("../Middleware/async"); |
||||
const sequelize = require('sequelize') |
||||
const db = require('../Config/db') |
||||
const SupplierModel = require('../Model/SupplierModel') |
||||
const ErrorResponse = require('../utils/ErrorResponse') |
||||
const fs = require('fs'); |
||||
|
||||
|
||||
const dateFormat = require('dateformat'); |
||||
|
||||
exports.getAllSupplier = asyncHandler(async (req, res, next) => { |
||||
try { |
||||
const supplier = await SupplierModel.findAll({ |
||||
where: { |
||||
DB_CODE: req.user.DB_CODE |
||||
} |
||||
} |
||||
) |
||||
res.status(200).send({ |
||||
success: true, |
||||
data: supplier |
||||
}) |
||||
} catch (e) { |
||||
throw new ErrorResponse(e, 400) |
||||
} |
||||
}) |
||||
|
||||
exports.createSupplier = asyncHandler(async (req, res, next) => { |
||||
const data = JSON.parse(req.body.data) |
||||
|
||||
if (!data.ADD_NAME || !data.ADD_CODE || !data.phoneNumber) { |
||||
throw new ErrorResponse('Please provide all required fields', 400) |
||||
} |
||||
|
||||
try { |
||||
data.DB_CODE = req.user.DB_CODE |
||||
const supplier = await SupplierModel.create({ |
||||
DB_CODE: req.user.DB_CODE, |
||||
USER_CODE: req.user.USER_NAME, |
||||
ADD_NAME: data.ADD_NAME, |
||||
ADD_CODE: data.ADD_CODE, |
||||
ADD_LINE_1: data.addressLine1, |
||||
|
||||
}) |
||||
res.status(200).send({ |
||||
success: true, |
||||
data: supplier |
||||
}) |
||||
} catch (e) { |
||||
throw new ErrorResponse(e, 400) |
||||
} |
||||
}) |
@ -0,0 +1,43 @@ |
||||
const sequelize = require('sequelize') |
||||
const db = require("../Config/db") |
||||
|
||||
const CategoryDetail = db.define( |
||||
"TD_CATEGORY_DETAIL", |
||||
{ |
||||
ID: { |
||||
type: sequelize.INTEGER, |
||||
primaryKey: true, |
||||
autoIncrement: true, |
||||
}, |
||||
CATE_ID: { |
||||
type: sequelize.INTEGER, |
||||
}, |
||||
ANALYSIS_ID: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
DESC_KH: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
DESC_EN: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
DESC_CN: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
CREATED_DATE: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
CREATED_BY: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
STATUS: { |
||||
type: sequelize.STRING, |
||||
}, |
||||
}, |
||||
{ |
||||
timestamps: false, |
||||
freezeTableName: true, |
||||
} |
||||
); |
||||
CategoryDetail.removeAttribute("id"); |
||||
module.exports = CategoryDetail; |
@ -0,0 +1,45 @@ |
||||
const sequelize = require('sequelize') |
||||
const db = require("../Config/db") |
||||
|
||||
const Category = db.define( |
||||
"TD_CATEGORY", |
||||
{ |
||||
ID: { |
||||
type: sequelize.INTEGER, |
||||
primaryKey: true, |
||||
autoIncrement: true |
||||
}, |
||||
DB_CODE: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ANALYSIS_ID: { |
||||
type: sequelize.STRING |
||||
}, |
||||
DESC_KH: { |
||||
type: sequelize.STRING |
||||
|
||||
}, |
||||
DESC_EN: { |
||||
type: sequelize.STRING |
||||
}, |
||||
DESC_CN: { |
||||
type: sequelize.STRING |
||||
}, |
||||
CREATED_DATE: { |
||||
type: sequelize.STRING |
||||
}, |
||||
CREATED_BY: { |
||||
type: sequelize.STRING |
||||
}, |
||||
STATUS: { |
||||
type: sequelize.INTEGER |
||||
} |
||||
|
||||
}, { |
||||
timestamps: false, |
||||
freezeTableName: true, |
||||
} |
||||
); |
||||
|
||||
Category.removeAttribute('id'); |
||||
module.exports = Category; |
@ -0,0 +1,83 @@ |
||||
const sequelize = require('sequelize') |
||||
const db = require('../Config/db') |
||||
|
||||
const SupplierModel = db.define( |
||||
'TDADDR', |
||||
{ |
||||
ADD_CODE: { |
||||
type: sequelize.STRING, |
||||
primaryKey: true |
||||
}, |
||||
ADD_LOOKUP: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_NAME: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_LINE_1: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_LINE_2: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_LINE_3: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_LINE_4: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_LINE_5: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_TEL: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_FAX: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_EMAIL: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_WEB: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_CONT: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_COM_1: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_COM_2: { |
||||
type: sequelize.STRING |
||||
}, |
||||
ADD_STAT: { |
||||
type: sequelize.CHAR |
||||
|
||||
}, |
||||
ADD_TYPE: { |
||||
type: sequelize.CHAR |
||||
}, |
||||
TRANS_PRES: { |
||||
type: sequelize.CHAR |
||||
|
||||
}, |
||||
USER_CREA: { |
||||
type: sequelize.STRING |
||||
}, |
||||
USER_UPDT: { |
||||
type: sequelize.STRING |
||||
|
||||
}, |
||||
USER_CODE: { |
||||
type: sequelize.STRING |
||||
}, |
||||
}, |
||||
{ |
||||
timestamps: false, |
||||
freezeTableName: true, |
||||
} |
||||
) |
||||
|
||||
|
||||
SupplierModel.removeAttribute('id') |
||||
module.exports = SupplierModel; |
@ -0,0 +1,27 @@ |
||||
const express = require("express"); |
||||
const route = express.Router(); |
||||
const controller = require("../Controller/Category"); |
||||
const { protect } = require("../Middleware/auth"); |
||||
const multer = require("multer"); |
||||
const moment = require("moment"); |
||||
var storage = multer.diskStorage({ |
||||
destination: function (req, file, cb) { |
||||
if (file.fieldname == "photo") cb(null, "uploads/item_category/tmp_post"); |
||||
}, |
||||
|
||||
filename: function (req, file, cb) { |
||||
cb( |
||||
null, |
||||
moment(Date.now()).format("YYYY-MM-DD_HH-mm-ss_") + file.originalname |
||||
); |
||||
}, |
||||
}); |
||||
var upload = multer({ storage: storage }); |
||||
|
||||
route.route("/").get(protect, controller.getAllCategoryOfStore); |
||||
|
||||
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); |
||||
|
||||
module.exports = route; |
@ -0,0 +1,9 @@ |
||||
const express = require("express"); |
||||
const route = express.Router(); |
||||
const controller = require("../Controller/Supplier"); |
||||
const { protectAtlogin, protect } = require("../Middleware/auth"); |
||||
|
||||
route.route('/').get(protect, controller.getAllSupplier) |
||||
// route.route('/id').post(protect, controller.getSubscriptionByID)
|
||||
|
||||
module.exports = route; |
Before Width: | Height: | Size: 102 KiB |
Loading…
Reference in new issue