462 lines
11 KiB
JavaScript
462 lines
11 KiB
JavaScript
const {body, validationResult} = require('express-validator');
|
|
const {_sr} = require('../plugins/serverResponses');
|
|
const {res404, res500} = require('../plugins/controllersHelperFunctions');
|
|
const FeedBack = require('../models/FeedBack');
|
|
const Captcha = require('../models/Captcha');
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const limit = 20;
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////// admin
|
|
module.exports.updateFeedBack = [
|
|
[
|
|
body('name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('description')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
|
|
body('twitter').notEmpty()
|
|
.withMessage(_sr['fa'].required.Twitter),
|
|
|
|
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('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,
|
|
twitter,
|
|
email,
|
|
nationalCode
|
|
} = req.body;
|
|
|
|
/**
|
|
* get id
|
|
*/
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* check meeting exist or not
|
|
*/
|
|
var targetFeedBack = await FeedBack.findById(meetingId);
|
|
|
|
if (!targetFeedBack) return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
|
|
|
|
/**
|
|
* make update object
|
|
*/
|
|
targetFeedBack.name = name.trim();
|
|
targetFeedBack.subject = subject.trim();
|
|
targetFeedBack.description = description.trim();
|
|
targetFeedBack.email = email;
|
|
targetFeedBack.twitter = twitter.trim()
|
|
targetFeedBack.phoneNumber = phoneNumber;
|
|
targetFeedBack.nationalCode = nationalCode;
|
|
|
|
await targetFeedBack.save();
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetFeedBack);
|
|
|
|
} catch (error) {
|
|
return res500(res, error?.message);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.deletOneFeedBack = [
|
|
async (req, res) => {
|
|
try {
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* check meeting
|
|
*/
|
|
let targetFeedBack = await FeedBack.findByIdAndDelete(meetingId);
|
|
|
|
if (!targetFeedBack) {
|
|
return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
}
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetFeedBack);
|
|
|
|
} catch (error) {
|
|
/**
|
|
* if we got unknown error show to client
|
|
*/
|
|
res500(res, error);
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getOneFeedBack = [
|
|
async (req, res) => {
|
|
try {
|
|
const meetingId = req.params.id;
|
|
|
|
/**
|
|
* get meeting
|
|
*/
|
|
const targetFeedBack = await FeedBack.findOne({_id: meetingId});
|
|
|
|
/**
|
|
* if not exist
|
|
*/
|
|
if (!targetFeedBack) {
|
|
return res404(res, `درخواست مورد نظر پیدا نشد`);
|
|
}
|
|
|
|
if (!targetFeedBack.read) {
|
|
targetFeedBack.read = true;
|
|
|
|
await targetFeedBack.save();
|
|
}
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json(targetFeedBack);
|
|
} 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 FeedBack.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 FeedBack.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 FeedBack.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.addFeedBack = [
|
|
[
|
|
body('name')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('description')
|
|
.notEmpty().withMessage(_sr['fa'].required.description),
|
|
body('twitter')
|
|
.notEmpty().withMessage(_sr['fa'].required.Twitter),
|
|
|
|
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('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 {
|
|
const {
|
|
name,
|
|
description,
|
|
twitter,
|
|
phoneNumber,
|
|
email
|
|
} = req.body;
|
|
|
|
const data = {
|
|
name,
|
|
description,
|
|
twitter,
|
|
phoneNumber,
|
|
email
|
|
};
|
|
|
|
const createFeedBack = await FeedBack.create(data);
|
|
|
|
if (!createFeedBack) return res500(res, `مشکلی در ثبت درخواست پیش آمده است`)
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
host: "mail.ostan-mr.ir",
|
|
port: 465,
|
|
secure: true, // true for 465, false for other ports
|
|
auth: {
|
|
user: 'feedback@ostan-mr.ir', // generated ethereal user
|
|
pass: 'danak@ostan1400', // generated ethereal password
|
|
},
|
|
});
|
|
|
|
let info = await transporter.sendMail({
|
|
from: 'feedback@ostan-mr.ir', // sender address
|
|
to: "feedback@ostan-mr.ir", // list of receivers
|
|
subject: "نظر و پیشنهاد", // Subject line
|
|
text: data.description, // plain text body
|
|
html: `<html lang="en" dir="ltr">
|
|
<head>
|
|
<title>نظر و پیشنهاد</title>
|
|
|
|
<style>
|
|
.view-form{
|
|
justify-content: center;
|
|
/*display: flex;*/
|
|
flex-direction: column;
|
|
align-items: center;
|
|
width: 100%;
|
|
max-width: 600px;
|
|
background-color: #fff;
|
|
border-radius: 10px;
|
|
padding: 30px 40px;
|
|
box-shadow: 0 5px 20px #999;
|
|
direction: rtl;
|
|
}
|
|
|
|
.formRow{
|
|
margin-bottom: 10px;
|
|
text-align: right;
|
|
}
|
|
|
|
.input{
|
|
width: 100%;
|
|
margin: 10px 0;
|
|
padding: 15px;
|
|
box-sizing: border-box;
|
|
border: 1px solid #ffffff;
|
|
background-color: #f4f4f4;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="view-form">
|
|
<div class="formRow">
|
|
<label for="name">نام و نام خانوادگی:</label>
|
|
<input class="input" value="${data.name}" type="text" id="name" disabled/>
|
|
</div>
|
|
<div class="formRow">
|
|
<label for="email">ایمیل:</label>
|
|
<input class="input" value="${data.email}" type="email" id="email" disabled/>
|
|
</div>
|
|
<div class="formRow">
|
|
<label for="phone">شماره تماس:</label>
|
|
<input class="input" value="${data.phoneNumber}" type="text" id="phone" disabled/>
|
|
</div>
|
|
<div class="formRow">
|
|
<label for="description">توضیحات:</label>
|
|
<textarea class="input" id="description" type="text" disabled>
|
|
${data.description}
|
|
</textarea>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>`, // html body
|
|
});
|
|
|
|
/**
|
|
* show result
|
|
*/
|
|
return res.json({message:_sr['fa'].response.success_save});
|
|
} catch (error) {
|
|
/**
|
|
* if we got unknown error show to client
|
|
*/
|
|
return res500(res, error?.message);
|
|
}
|
|
}
|
|
]
|
|
|