437 lines
14 KiB
JavaScript
437 lines
14 KiB
JavaScript
const Category = require('../models/Category');
|
|
const {body, validationResult} = require('express-validator');
|
|
const {_sr} = require('../plugins/serverResponses');
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions');
|
|
const Polls = require('../models/Polls');
|
|
const fs = require('fs')
|
|
|
|
|
|
// variables
|
|
const titleFilterRegex = new RegExp('/', 'g')
|
|
const limit = 20;
|
|
|
|
|
|
//// functions
|
|
const _checkSpecPermission = async (req, category) => {
|
|
return !req.user.specialPermissions.includes(category._id) && //check parent id exist in special permissions or not
|
|
!req.user.specialPermissions.some(item => category?.treeCat?.join().includes(item));
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////// admin
|
|
module.exports.addSurvey = [
|
|
[
|
|
body('title')
|
|
.notEmpty().withMessage(_sr['fa'].required.field),
|
|
|
|
body('catId')
|
|
.isMongoId().withMessage(_sr['fa'].required.field)
|
|
.if((value, {req}) => {
|
|
return req.body.catId
|
|
})
|
|
.custom((value) => {
|
|
return Category.findOne({
|
|
_id: value
|
|
})
|
|
.then(category => {
|
|
if (!category) return Promise.reject(_sr['fa'].not_found.category);
|
|
else return true
|
|
})
|
|
}),
|
|
|
|
// body('index')
|
|
// .isNumeric().withMessage(`لطفا ترتیب نمایش را مشخص کنید`),
|
|
|
|
body('surveyType')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
.bail()
|
|
.isIn(['check', 'radio']).withMessage(`${_sr['fa'].optional.onlyValid} => 'check' , 'radio'`),
|
|
|
|
body('startDate')
|
|
.notEmpty().withMessage(`لطفا یک تاریخ شروع تعیین کنید`),
|
|
|
|
body('endDate')
|
|
.notEmpty().withMessage(`لطفا یک تاریخ پایان تعیین کنید`),
|
|
|
|
body('publish')
|
|
.isBoolean().withMessage(_sr['fa'].format.boolean),
|
|
|
|
body('options')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
.bail()
|
|
.isArray().withMessage(`اطلاعات را باید به صورت آرایه بفرستید`),
|
|
|
|
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
try {
|
|
|
|
const {
|
|
title,
|
|
catId,
|
|
description,
|
|
// index,
|
|
surveyType,
|
|
startDate,
|
|
endDate,
|
|
options,
|
|
publish
|
|
} = req.body;
|
|
|
|
const data = {
|
|
title: title.trim(),
|
|
catId,
|
|
description,
|
|
// index,
|
|
surveyType,
|
|
startDate,
|
|
endDate,
|
|
options,
|
|
publish,
|
|
_creator: req.user._id,
|
|
_updatedBy: req.user._id
|
|
}
|
|
|
|
|
|
const category = await Category.findOne({
|
|
_id: catId
|
|
}).populate({
|
|
path: 'parent',
|
|
populate: {
|
|
path: 'parent',
|
|
model: 'Category'
|
|
}
|
|
});
|
|
|
|
if (!category) return res404(res, 'دسته بندی مورد نظر پیدا نشد');
|
|
|
|
/** check special permission */
|
|
if (req.user.specialPermissions.length && await _checkSpecPermission(req, category)) {
|
|
return res.status(403).json({
|
|
message: `شما نمیتوانید این دسته بندی را انتخاب کنید`
|
|
});
|
|
}
|
|
|
|
data.allCat = category.treeCat;
|
|
data.rootParent = category.rootParent || category._id;
|
|
|
|
const createPoll = await Polls.create(data);
|
|
|
|
if (!createPoll) return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`);
|
|
|
|
return res.json({message: _sr['fa'].response.success_save});
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.updateSurvey = [
|
|
[
|
|
body('title')
|
|
.notEmpty().withMessage(_sr['fa'].required.field),
|
|
|
|
body('catId')
|
|
.isMongoId().withMessage(_sr['fa'].required.field)
|
|
.if((value, {req}) => {
|
|
return req.body.catId
|
|
})
|
|
.custom((value) => {
|
|
return Category.findOne({
|
|
_id: value
|
|
})
|
|
.then(category => {
|
|
if (!category) return Promise.reject(_sr['fa'].not_found.category);
|
|
else return true
|
|
})
|
|
}),
|
|
|
|
// body('index')
|
|
// .isNumeric().withMessage(`لطفا ترتیب نمایش را مشخص کنید`),
|
|
|
|
body('surveyType')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
.bail()
|
|
.isIn(['check', 'radio']).withMessage(`${_sr['fa'].optional.onlyValid} => 'check' , 'radio'`),
|
|
|
|
body('startDate')
|
|
.notEmpty().withMessage(`لطفا یک تاریخ شروع تعیین کنید`),
|
|
|
|
body('endDate')
|
|
.notEmpty().withMessage(`لطفا یک تاریخ پایان تعیین کنید`),
|
|
|
|
body('options')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
.bail()
|
|
.isArray().withMessage(`اطلاعات را باید به صورت آرایه بفرستید`),
|
|
|
|
body('publish')
|
|
.isBoolean().withMessage(_sr['fa'].format.boolean),
|
|
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
try {
|
|
|
|
const pollId = req.params.id;
|
|
|
|
const {
|
|
title,
|
|
surveyType,
|
|
description,
|
|
catId,
|
|
// index,
|
|
startDate,
|
|
endDate,
|
|
options,
|
|
publish
|
|
} = req.body;
|
|
|
|
//// check poll
|
|
const targetPoll = await Polls.findById(pollId);
|
|
|
|
if (!targetPoll) return res404(res, `نظر سنجی مورد نظر پیدا نشد`);
|
|
|
|
|
|
if (catId && targetPoll.catId !== catId) {
|
|
const category = await Category.findOne({
|
|
_id: catId
|
|
}).populate({
|
|
path: 'parent',
|
|
populate: {
|
|
path: 'parent',
|
|
model: 'Category'
|
|
}
|
|
});
|
|
|
|
if (!category) return res404(res, 'دسته بندی مورد نظر پیدا نشد');
|
|
|
|
/** check special permission */
|
|
if (req.user.specialPermissions.length && await _checkSpecPermission(req, category)) {
|
|
return res.status(403).json({
|
|
message: `شما نمیتوانید این دسته بندی را انتخاب کنید`
|
|
});
|
|
}
|
|
|
|
targetPoll.catId = catId;
|
|
targetPoll.allCat = category.treeCat;
|
|
targetPoll.rootParent = category.rootParent || category._id;
|
|
}
|
|
|
|
targetPoll.title = title;
|
|
targetPoll.surveyType = surveyType;
|
|
targetPoll.description = description;
|
|
// targetPoll.index = index;
|
|
targetPoll.startDate = startDate;
|
|
targetPoll.endDate = endDate;
|
|
targetPoll.options = options;
|
|
targetPoll.publish = publish;
|
|
targetPoll._updatedBy = req.user._id;
|
|
|
|
await targetPoll.save();
|
|
|
|
return res.json({message: _sr['fa'].response.success_save});
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAllSurvey = [
|
|
async (req, res) => {
|
|
try {
|
|
let findQuery = {};
|
|
|
|
if (req.user.specialPermissions.length) {
|
|
findQuery.catId = {$in: req.user.specialPermissions}
|
|
}
|
|
|
|
const allPolls = await Polls.paginate(findQuery, {
|
|
page: req.query.page || 1,
|
|
populate: {path: 'catId'},
|
|
limit,
|
|
sort: {created_at: -1}
|
|
});
|
|
|
|
if (!allPolls) return res500(res, `مشکلی در گرفتن اطلاعات پیش آمده است`);
|
|
|
|
return res.json(allPolls);
|
|
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getOneSurvey = [
|
|
async (req, res) => {
|
|
try {
|
|
const pollId = req.params.id;
|
|
|
|
const findQuery = {
|
|
_id: pollId
|
|
};
|
|
|
|
const poll = await Polls.findOne(findQuery).populate('catId');
|
|
|
|
if (!poll) return res404(res, `اطلاعات مورد نظر پیدا نشد`);
|
|
|
|
if (req.user.specialPermissions.length && await _checkSpecPermission(req, poll.catId)) {
|
|
return res.status(403).json({
|
|
message: `شما مجاز به دیدن اطلاعات این نظرسنجی نمی باشید`
|
|
});
|
|
}
|
|
|
|
return res.json(poll);
|
|
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.deletOneSurvey = [
|
|
async (req, res) => {
|
|
try {
|
|
const pollId = req.params.id;
|
|
|
|
const deletePoll = await Polls.findById(pollId).populate('catId');
|
|
|
|
if (!deletePoll) return res404(res, `نظرسنجی مورد نظر پیدا نشد`);
|
|
|
|
if (req.user.specialPermissions.length && await _checkSpecPermission(req, deletePoll.catId)) {
|
|
return res.status(403).json({
|
|
message: `شما اجازه حذف این نظرسنجی را ندارید`
|
|
});
|
|
}
|
|
|
|
await deletePoll.deleteOne();
|
|
|
|
return res.json({message: _sr['fa'].response.success_remove});
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.search = [
|
|
async (req, res) => {
|
|
try {
|
|
let query = {};
|
|
let populate = [{
|
|
path: '_creator',
|
|
select: {firstName: 1, lastName: 1, profilePic: 1}
|
|
}];
|
|
|
|
|
|
query['$and'] = [];
|
|
|
|
const {
|
|
term,
|
|
catId,
|
|
publish,
|
|
date
|
|
} = req.body;
|
|
|
|
/** search by term */
|
|
if (term && !_.isEmpty(term)) {
|
|
query['$and'].push({
|
|
$or: [{
|
|
title: {
|
|
$regex: '.*' + term + '.*'
|
|
}
|
|
}]
|
|
});
|
|
}
|
|
|
|
if (req.user.specialPermissions.length) {
|
|
query.catId = {$in: req.user.specialPermissions}
|
|
} else if (catId && !_.isEmpty(catId)) {
|
|
query['$and'].push({
|
|
catId
|
|
});
|
|
}
|
|
|
|
/** search by publish */
|
|
if (!_.isUndefined(publish) && _.isBoolean(publish)) {
|
|
query['$and'].push({
|
|
publish
|
|
});
|
|
}
|
|
|
|
/** search by date */
|
|
if (date && !_.isEmpty(date)) {
|
|
if (date.length === 1) {
|
|
query['$and'].push({
|
|
startDate: {$lte: new Date(date[0])},
|
|
endDate: {$gte: new Date(date[0])}
|
|
});
|
|
} else {
|
|
query['$and'].push({
|
|
startDate: {$lte: new Date(date[0])},
|
|
endDate: {$gte: new Date(date[1])}
|
|
});
|
|
}
|
|
}
|
|
|
|
_.isEmpty(query['$and']) && delete query['$and'];
|
|
// _.isEmpty(input['$or']) && delete input['$or'];
|
|
|
|
/** get polls */
|
|
const polls = await Polls.paginate(query, {
|
|
page: req.query.page || 1,
|
|
limit,
|
|
populate,
|
|
sort: {_id: -1}
|
|
});
|
|
|
|
/** show result */
|
|
return res.json(polls);
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|
|
/////////////////////////////////////////////////////////////////////// public
|
|
|
|
module.exports.answer = [
|
|
[
|
|
body('surveyId')
|
|
.notEmpty().withMessage(_sr['fa'].required.field),
|
|
|
|
body('option')
|
|
.notEmpty().withMessage(_sr['fa'].required.field),
|
|
|
|
body('pollToken')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
try {
|
|
const {
|
|
surveyId,
|
|
option,
|
|
pollToken
|
|
} = req.body;
|
|
|
|
//// check category
|
|
const targetPoll = await Polls.findById(surveyId);
|
|
|
|
if (!targetPoll) return res404(res, `نظر سنجی مورد نظر پیدا نشد`);
|
|
|
|
for (const item of option) {
|
|
targetPoll.answers.push(item);
|
|
}
|
|
targetPoll.userIds.push(pollToken);
|
|
|
|
await targetPoll.save();
|
|
|
|
return res.json({message: _sr['fa'].response.success_save});
|
|
} catch (error) {
|
|
return res500(res, `مشکلی در اجرای درخواست شما بوجود آمده است`);
|
|
}
|
|
}
|
|
]
|
|
|