Files
hamid.zarghami1@gmail.com e644edbd65 transfer project in github
2024-05-12 15:29:27 +03:30

104 lines
2.7 KiB
JavaScript

const Job = require('../models/Job')
const {body, validationResult} = require('express-validator')
const {_sr} = require('../plugins/serverResponses')
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
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 Job.findOne({name: value})
.then(menuType => {
if (menuType) return Promise.reject(_sr['fa'].duplicated.name)
else return true
})
})
],
checkValidations(validationResult),
(req, res) => {
const job = new Job(req.body)
job.save((err, data) => {
if (err) return res500(res, err)
else return res.json({message: _sr['fa'].response.success_save})
})
}
]
module.exports.get_all_for_panel = [
(req, res) => {
const filter = {}
Job.paginate(filter, {
page: req.query.page,
limit,
sort: {index: 1}
}, (err, data) => {
if (err) return res500(res, err)
else return res.json(data)
})
}
]
module.exports.get_all_for_mobile = [
(req, res) => {
Job.find().sort({index: 1}).exec((err, data) => {
if (err) return res500(res, err)
else return res.json(data)
})
}
]
module.exports.get_one = [
(req, res) => {
Job.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 Job.findOne({name: value})
.then(menuType => {
if (menuType && menuType._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.name)
else return true
})
})
],
checkValidations(validationResult),
(req, res) => {
Job.findByIdAndUpdate(req.params.id, req.body, (err, oldData) => {
if (err || !oldData) return res404(res, err)
else return res.json({message: _sr['fa'].response.success_save})
})
}
]
module.exports.delete = [
(req, res) => {
Job.findByIdAndDelete(req.params.id, (err, data) => {
if (err || !data) return res404(res, err)
else return res.json({message: _sr['fa'].response.success_remove})
})
}
]