397 lines
11 KiB
JavaScript
397 lines
11 KiB
JavaScript
const {body, validationResult} = require('express-validator');
|
|
const {_sr} = require('../plugins/serverResponses');
|
|
const {res404, res500} = require('../plugins/controllersHelperFunctions');
|
|
const Meeting = require('../models/Meeting');
|
|
const Captcha = require('../models/Captcha');
|
|
|
|
|
|
const limit = 20;
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////// admin
|
|
module.exports.updateMeeting = [
|
|
[
|
|
body('name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('subject')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
|
|
body('description')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
|
|
body('email')
|
|
.optional().isEmail().withMessage(_sr['fa'].format.email),
|
|
|
|
body('phoneNumber')
|
|
.if((value, {req}) => {
|
|
return req.body.phoneNumber
|
|
})
|
|
.custom((value, {req}) => {
|
|
if (!_.isEmpty(value) && !lodash.isNumeric(value)) {
|
|
return Promise.reject(_sr['fa'].format.number);
|
|
} else return true;
|
|
}),
|
|
|
|
body('nationalCode')
|
|
.notEmpty().withMessage(_sr['fa'].format.national_code)
|
|
|
|
// body('cptcha')
|
|
// .notEmpty().withMessage(_sr['fa'].required.field)
|
|
// .custom((value , {req}) => {
|
|
// console.log(req.session);
|
|
// if(value != req.session.captcha){
|
|
// return Promise.reject(`کپچا وارد شده صحیح نمی باشد`);
|
|
// }else{
|
|
// return true
|
|
// }
|
|
// }),
|
|
|
|
],
|
|
async (req, res) => {
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) return res.status(422).json({
|
|
validation: errors.mapped()
|
|
});
|
|
|
|
try {
|
|
|
|
/**
|
|
* get data from input
|
|
*/
|
|
const {
|
|
name,
|
|
subject,
|
|
description,
|
|
phoneNumber,
|
|
email,
|
|
nationalCode
|
|
} = req.body;
|
|
|
|
/**
|
|
* get id
|
|
*/
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* check meeting exist or not
|
|
*/
|
|
var targetMeeting = await Meeting.findById(meetingId);
|
|
|
|
if (!targetMeeting) return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
|
|
|
|
/**
|
|
* make update object
|
|
*/
|
|
targetMeeting.name = name.trim();
|
|
targetMeeting.subject = subject.trim();
|
|
targetMeeting.description = description.trim();
|
|
targetMeeting.email = email;
|
|
targetMeeting.phoneNumber = phoneNumber;
|
|
targetMeeting.nationalCode = nationalCode;
|
|
|
|
await targetMeeting.save();
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetMeeting);
|
|
|
|
} catch (error) {
|
|
return res500(res, error?.message);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.deletOneMeeting = [
|
|
async (req, res) => {
|
|
try {
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* check meeting
|
|
*/
|
|
let targetMeeting = await Meeting.findByIdAndDelete(meetingId);
|
|
|
|
if (!targetMeeting) {
|
|
return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
}
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetMeeting);
|
|
|
|
} catch (error) {
|
|
/**
|
|
* if we got unknown error show to client
|
|
*/
|
|
res500(res, error);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getOneMeeting = [
|
|
async (req, res) => {
|
|
try {
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* get meeting
|
|
*/
|
|
const targetMeeting = await Meeting.findOne({_id: meetingId});
|
|
|
|
/**
|
|
* if not exist
|
|
*/
|
|
if (!targetMeeting) {
|
|
return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
}
|
|
|
|
if (!targetMeeting.read) {
|
|
targetMeeting.read = true;
|
|
|
|
await targetMeeting.save();
|
|
}
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetMeeting);
|
|
} catch (error) {
|
|
/**
|
|
* if we got unknown error show to client
|
|
*/
|
|
res500(res, error);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
async (req, res) => {
|
|
try {
|
|
|
|
let findQuery = {};
|
|
|
|
/**
|
|
* get all meeting
|
|
*/
|
|
const getAll = await Meeting.paginate(findQuery,
|
|
{page: req.query.page || 1, limit, sort: {created_at: -1}});
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(getAll);
|
|
} catch (error) {
|
|
return res500(res, error.message);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAllForCount = [
|
|
async (req, res) => {
|
|
try {
|
|
|
|
/**
|
|
* get all meeting
|
|
*/
|
|
const getAll = await Meeting.find({});
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(getAll);
|
|
} catch (error) {
|
|
return res500(res, error.message);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.search = [
|
|
async (req, res) => {
|
|
try {
|
|
let findQuery = {};
|
|
findQuery['$and'] = [];
|
|
findQuery['$or'] = [];
|
|
|
|
const {
|
|
name,
|
|
email,
|
|
terms,
|
|
phoneNumber,
|
|
nationalCode,
|
|
read,
|
|
date
|
|
} = req.body;
|
|
|
|
findQuery.modelName = req.params.model;
|
|
|
|
/** search by term */
|
|
if (terms && !_.isEmpty(terms)) {
|
|
findQuery['$or'].push(
|
|
{
|
|
subject: {
|
|
$regex: '.*' + name + '.*'
|
|
},
|
|
description: {
|
|
$regex: '.*' + name + '.*'
|
|
},
|
|
}
|
|
);
|
|
}
|
|
/** search by name */
|
|
if (name && !_.isEmpty(name)) {
|
|
findQuery['$or'].push(
|
|
{
|
|
name: {
|
|
$regex: '.*' + name + '.*'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
/** search by email */
|
|
if (email && !_.isEmpty(email)) {
|
|
findQuery['$and'].push({
|
|
email
|
|
});
|
|
}
|
|
/** search by phoneNumber */
|
|
if (phoneNumber && !_.isEmpty(phoneNumber)) {
|
|
findQuery['$and'].push({
|
|
phoneNumber
|
|
});
|
|
}
|
|
/** search by nationalCode */
|
|
if (nationalCode && !_.isEmpty(nationalCode)) {
|
|
findQuery['$and'].push({
|
|
nationalCode
|
|
});
|
|
}
|
|
/** search by read */
|
|
if (!_.isUndefined(read) && _.isBoolean(read)) {
|
|
findQuery['$and'].push({
|
|
read
|
|
});
|
|
}
|
|
|
|
/** search by date */
|
|
if (date && !_.isEmpty(date)) {
|
|
if (date.length === 1) {
|
|
findQuery['$and'].push({
|
|
created_at: {
|
|
$gte: moment(date[0]).startOf('days'),
|
|
$lt: moment(date[0]).endOf('days')
|
|
}
|
|
});
|
|
} else {
|
|
findQuery['$and'].push({
|
|
created_at: {
|
|
$gte: new Date(date[0]),
|
|
$lt: new Date(date[1])
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
_.isEmpty(findQuery['$and']) && delete findQuery['$and'];
|
|
_.isEmpty(findQuery['$or']) && delete findQuery['$or'];
|
|
|
|
/** get meetings */
|
|
var meetings = await Meeting.paginate(findQuery, {
|
|
page: req.query.page,
|
|
limit,
|
|
sort: {created_at: -1}
|
|
});
|
|
|
|
/** show result */
|
|
return res.json(meetings);
|
|
} catch (error) {
|
|
return res500(res, error?.message)
|
|
}
|
|
}
|
|
]
|
|
|
|
/////////////////////////////////////////////////////////////////////// public
|
|
module.exports.addMeeting = [
|
|
[
|
|
body('name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('subject')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
|
|
body('description')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
|
|
body('email')
|
|
.optional().isEmail().withMessage(_sr['fa'].format.email),
|
|
|
|
body('phoneNumber')
|
|
.custom((value, {req}) => {
|
|
if (_.isEmpty(value)) return Promise.reject(_sr['fa'].required.phone_number);
|
|
if (!_.isEmpty(value) && !lodash.isNumeric(value)) {
|
|
return Promise.reject(_sr['fa'].format.number);
|
|
} else return true;
|
|
}),
|
|
|
|
body('nationalCode')
|
|
.notEmpty().withMessage(_sr['fa'].format.national_code),
|
|
|
|
body('captcha')
|
|
.notEmpty().withMessage(_sr['fa'].required.field)
|
|
.custom((value, {req}) => {
|
|
return Captcha.findOne({code: value}).then(data => {
|
|
if (!data) return Promise.reject(`عبارت وارد شده صحیح نمیباشد`)
|
|
else return true
|
|
})
|
|
}),
|
|
|
|
],
|
|
async (req, res) => {
|
|
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) return res.status(422).json({
|
|
validation: errors.mapped()
|
|
});
|
|
|
|
try {
|
|
let {
|
|
name,
|
|
subject,
|
|
description,
|
|
phoneNumber,
|
|
email,
|
|
nationalCode
|
|
} = req.body;
|
|
|
|
const data = {
|
|
name,
|
|
subject,
|
|
description,
|
|
phoneNumber,
|
|
email,
|
|
nationalCode
|
|
};
|
|
|
|
const createMeeting = await Meeting.create(data);
|
|
|
|
if (!createMeeting) {
|
|
return res500(res, `مشکلی در ثبت درخواست پیش آمده است`);
|
|
}
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(createMeeting);
|
|
} catch (error) {
|
|
/**
|
|
* if we got unknown error show to client
|
|
*/
|
|
return res500(res, error?.message);
|
|
}
|
|
}
|
|
]
|
|
|