248 lines
6.7 KiB
JavaScript
248 lines
6.7 KiB
JavaScript
const Project = require('../models/Project')
|
|
const ProjectCategory = require('../models/ProjectCategory')
|
|
const {body, validationResult} = require('express-validator')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
|
const mongoose = require('mongoose')
|
|
|
|
|
|
module.exports.create = [
|
|
[
|
|
body('fa_name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return ProjectCategory.findOne({$or: [{'locale.fa.name': value}, {'locale.en.name': value}]})
|
|
.then(product => {
|
|
if (product) return Promise.reject(_sr['fa'].duplicated.title)
|
|
else return true
|
|
})
|
|
}),
|
|
|
|
body('en_name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return ProjectCategory.findOne({$or: [{'locale.fa.name': value}, {'locale.en.name': value}]})
|
|
.then(product => {
|
|
if (product) return Promise.reject(_sr['fa'].duplicated.title)
|
|
else return true
|
|
})
|
|
}),
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {fa_name, en_name} = req.body
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
name: fa_name
|
|
},
|
|
en: {
|
|
name: en_name
|
|
}
|
|
}
|
|
}
|
|
const projectCategory = new ProjectCategory(data)
|
|
projectCategory.save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(data)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
ProjectCategory.find({}).exec((err, projects) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(projects)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
ProjectCategory.findById(req.params.id, (err, projectCategory) => {
|
|
if (err || !projectCategory) return res404(res, err)
|
|
else return res.json(projectCategory)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.update = [
|
|
[
|
|
body('fa_name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return ProjectCategory.findOne({$or: [{'locale.fa.name': value}, {'locale.en.name': value}]})
|
|
.then(product => {
|
|
if (product && product._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.title)
|
|
else return true
|
|
})
|
|
}),
|
|
|
|
body('en_name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return ProjectCategory.findOne({$or: [{'locale.fa.name': value}, {'locale.en.name': value}]})
|
|
.then(product => {
|
|
if (product && product._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.title)
|
|
else return true
|
|
})
|
|
})
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {
|
|
fa_name,
|
|
en_name
|
|
} = req.body
|
|
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
name: fa_name
|
|
},
|
|
en: {
|
|
name: en_name
|
|
}
|
|
}
|
|
}
|
|
|
|
ProjectCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.delete = [
|
|
async (req, res) => {
|
|
const projects = await Project.find({category: req.params.id})
|
|
if (projects.length) return res.status(403).json({message: 'نمیتوان دسته بندی هایی که دارای پروژه هستند را حذف کرد.'})
|
|
|
|
ProjectCategory.findByIdAndRemove(req.params.id, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
return res.json({message: _sr['fa'].response.success_remove})
|
|
})
|
|
}
|
|
]
|
|
|
|
////////////////////////////
|
|
|
|
module.exports.addTagToCategory = [
|
|
[
|
|
body('fa_tagName')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('en_tagName')
|
|
.notEmpty().withMessage(_sr['fa'].required.title)
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {fa_tagName, en_tagName} = req.body
|
|
ProjectCategory.findById(req.params.categoryId, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
data.tags.push({
|
|
locale: {
|
|
fa: {
|
|
name: fa_tagName
|
|
},
|
|
en: {
|
|
name: en_tagName
|
|
}
|
|
}
|
|
})
|
|
data.save(err => {
|
|
if (err) console.log(err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.removeTagFromCategory = [
|
|
async (req, res) => {
|
|
const projects = await Project.find({tag: req.params.id})
|
|
if (projects.length) return res.status(403).json({message: 'نمیتوان دسته بندی هایی که دارای پروژه هستند را حذف کرد.'})
|
|
|
|
ProjectCategory.findOne({'tags._id': req.params.tagId}, (err, project) => {
|
|
if (err || !project) return res404(res, err)
|
|
const tag = project.tags.id(req.params.tagId)
|
|
tag.remove(err => {
|
|
if (err) console.log(err)
|
|
})
|
|
project.save(err => {
|
|
if (err) console.log(err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
})
|
|
}
|
|
]
|
|
|
|
|
|
// module.exports.getAll = [
|
|
// (req, res) => {
|
|
// return res.json([
|
|
// {
|
|
// id: 'drilling',
|
|
// locale: {
|
|
// fa: {
|
|
// name: 'صنعت حفاری',
|
|
// },
|
|
// en: {
|
|
// name: 'Drilling industry'
|
|
// }
|
|
// }
|
|
// },
|
|
// {
|
|
// id: 'piping',
|
|
// locale: {
|
|
// fa: {
|
|
// name: 'صنعت خطوط لوله و اتصالات',
|
|
// },
|
|
// en: {
|
|
// name: 'Pipeline and fittings industry'
|
|
// }
|
|
// }
|
|
// },
|
|
// {
|
|
// id: 'ward',
|
|
// locale: {
|
|
// fa: {
|
|
// name: 'صنعت پتروشیمی',
|
|
// },
|
|
// en: {
|
|
// name: 'Petrochemical industry'
|
|
// }
|
|
// }
|
|
// },
|
|
// // {
|
|
// // id: 'srp',
|
|
// // locale: {
|
|
// // fa: {
|
|
// // name: 'پمپ های درون چاهی SRP',
|
|
// // },
|
|
// // en: {
|
|
// // name: 'Manufacturer of SRP well pumps'
|
|
// // }
|
|
// // }
|
|
// // },
|
|
// // {
|
|
// // id: 'sm',
|
|
// // locale: {
|
|
// // fa: {
|
|
// // name: 'طراحی و ساخت unit های فرآورش و نمک زدایی پیش ساخته Skid Mounted',
|
|
// // },
|
|
// // en: {
|
|
// // name: 'Skid Mounted'
|
|
// // }
|
|
// // }
|
|
// // },
|
|
// ])
|
|
// }
|
|
// ]
|
|
|