transfer project in github
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
const FoodCategory = require('../models/FoodCategory')
|
||||
const Food = require('../models/Food')
|
||||
const MenuType = require('../models/MenuType')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const {_sr} = require('../plugins/serverResponses')
|
||||
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
||||
const fs = require('fs')
|
||||
const sharp = require('sharp')
|
||||
const StoreInfo = require("../models/StoreInfo");
|
||||
|
||||
// variables
|
||||
const coverWidth = 500
|
||||
const coverHeight = 500
|
||||
const coverQuality = 100
|
||||
const limit = 20
|
||||
|
||||
module.exports.create = [
|
||||
[
|
||||
body('index')
|
||||
.notEmpty().withMessage(_sr['fa'].required.index)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.name)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return FoodCategory.findOne({name: value, branchId: req?.body?.branchId})
|
||||
.then(foodCategory => {
|
||||
if (foodCategory) return Promise.reject(_sr['fa'].duplicated.name)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('type')
|
||||
.notEmpty().withMessage('نوع منو را انتخاب کنید')
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return MenuType.findOne({_id: value})
|
||||
.then(menuType => {
|
||||
if (!menuType) return Promise.reject(_sr['fa'].not_found.item_id)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('branchId')
|
||||
.notEmpty().withMessage(_sr['fa'].required.field)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return StoreInfo.findOne({_id: value})
|
||||
.then(store => {
|
||||
if (!store) return Promise.reject(_sr['fa'].not_found.item_id)
|
||||
else return true
|
||||
})
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
if (!req.files?.cover) return res.status(422).json({validation: {cover: {msg: _sr['fa'].required.cover}}})
|
||||
if (!_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
||||
try {
|
||||
const {name, type, index , branchId} = req.body
|
||||
const data = {name, type, index , branchId}
|
||||
|
||||
const cover = req.files.cover
|
||||
const coverName = 'category_' + Date.now() + '.png'
|
||||
data.cover = coverName
|
||||
|
||||
const target = await sharp(cover.data)
|
||||
await target
|
||||
.resize(coverWidth, coverHeight, {fit: 'cover'})
|
||||
.toFormat('png')
|
||||
.png({quality: coverQuality})
|
||||
.toFile(`./static/uploads/images/foodCategories/${coverName}`)
|
||||
|
||||
const foodCategory = new FoodCategory(data)
|
||||
foodCategory.save(err => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.get_all_for_panel = [
|
||||
(req, res) => {
|
||||
const filter = {branchId: req?.params?.branchId}
|
||||
FoodCategory.paginate(filter, {
|
||||
page: req.query.page || 1,
|
||||
limit,
|
||||
populate : 'type',
|
||||
sort : {index : 1}
|
||||
} , (err, data) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.get_all_for_web = [
|
||||
(req, res) => {
|
||||
const branchId = req?.params?.branchId
|
||||
const query = {}
|
||||
if (branchId) query.branchId = branchId
|
||||
FoodCategory.find(query).populate('type').sort({index: 1}).exec((err, data) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.get_one = [
|
||||
(req, res) => {
|
||||
FoodCategory.findById(req.params.id, (err, data) => {
|
||||
if (err || !data) return res404(res, err)
|
||||
else return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.update = [
|
||||
[
|
||||
body('index')
|
||||
.notEmpty().withMessage(_sr['fa'].required.index)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.name)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return FoodCategory.findOne({name: value, branchId: req?.body?.branchId})
|
||||
.then(foodCategory => {
|
||||
if (foodCategory && foodCategory._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.name)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('type')
|
||||
.notEmpty().withMessage('نوع منو را انتخاب کنید')
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return MenuType.findOne({_id: value})
|
||||
.then(menuType => {
|
||||
if (!menuType) return Promise.reject(_sr['fa'].not_found.item_id)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('branchId')
|
||||
.notEmpty().withMessage(_sr['fa'].required.field)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return StoreInfo.findOne({_id: value})
|
||||
.then(store => {
|
||||
if (!store) return Promise.reject(_sr['fa'].not_found.item_id)
|
||||
else return true
|
||||
})
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
if (req.files?.cover && !_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
||||
|
||||
try {
|
||||
const {name, type, index , branchId} = req.body
|
||||
const data = {name, type, index, branchId}
|
||||
|
||||
if (req.files?.cover) {
|
||||
const cover = req?.files?.cover
|
||||
const coverName = 'category_' + Date.now() + '.png'
|
||||
data.cover = coverName
|
||||
|
||||
const target = await sharp(cover.data)
|
||||
await target
|
||||
.resize(coverWidth, coverHeight, {fit: 'cover'})
|
||||
.toFormat('png')
|
||||
.png({quality: coverQuality})
|
||||
.toFile(`./static/uploads/images/foodCategories/${coverName}`)
|
||||
}
|
||||
|
||||
|
||||
FoodCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
||||
if (err || !oldData) return res404(res, err)
|
||||
if (req.files?.cover) {
|
||||
fs.unlink(`./static/${oldData.cover}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.delete = [
|
||||
(req, res) => {
|
||||
Food.findOne({category: req.params.id}, (err, food) => {
|
||||
if (err) return res500(res, err)
|
||||
if (food) return res.status(403).json({message: 'نمیتوان دسته بندی هایی که دارای زیر مجموعه هستند را حذف کرد.'})
|
||||
else {
|
||||
FoodCategory.findByIdAndDelete(req.params.id, (err, data) => {
|
||||
if (err || !data) return res404(res, err)
|
||||
else return res.json({message: _sr['fa'].response.success_remove})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
////////////////////////////////////////////// user
|
||||
|
||||
Reference in New Issue
Block a user