126 lines
4.3 KiB
JavaScript
126 lines
4.3 KiB
JavaScript
const ContactUs = require('../models/ContactUs')
|
|
const {body, validationResult} = require('express-validator')
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
const nodemailer = require('nodemailer')
|
|
|
|
module.exports.create = [
|
|
[
|
|
body('locale')
|
|
.isLength({min: 2}).withMessage('locale is required')
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return value === 'fa' || value === 'en' ? true : Promise.reject('locale is not correct')
|
|
}),
|
|
|
|
body('name')
|
|
.notEmpty().withMessage((value, {req}) => _sr[req.body.locale].required.name)
|
|
.bail()
|
|
.isLength({min: 2}).withMessage((value, {req}) => _sr[req.body.locale].min_char.min2),
|
|
|
|
body('email')
|
|
.notEmpty().withMessage((value, {req}) => _sr[req.body.locale].required.email)
|
|
.bail()
|
|
.isEmail().withMessage((value, {req}) => _sr[req.body.locale].format.email),
|
|
|
|
body('subject')
|
|
.notEmpty().withMessage((value, {req}) => _sr[req.body.locale].required.subject)
|
|
.bail()
|
|
.isLength({min: 4}).withMessage((value, {req}) => _sr[req.body.locale].min_char.min4),
|
|
|
|
body('message')
|
|
.notEmpty().withMessage((value, {req}) => _sr[req.body.locale].required.message)
|
|
.bail()
|
|
.isLength({min: 10}).withMessage((value, {req}) => _sr[req.body.locale].min_char.min10)
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {locale, name, email, subject, message} = req.body
|
|
|
|
const data = {locale, name, email, subject, message}
|
|
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
host: 'mail.negareh-demo2.ir',
|
|
port: 587,
|
|
secure: false, // true for 465, false for other ports
|
|
auth: {
|
|
user: 'client@negareh-demo2.ir',
|
|
pass: 'JSAT}Q-RB_s0'
|
|
},
|
|
tls: {
|
|
rejectUnauthorized: false
|
|
}
|
|
})
|
|
|
|
const emailHTMLTemplate = `
|
|
<div style="direction: rtl;background-color: #ed1d24;overflow: hidden;">
|
|
<div style="width: 50%;border-radius: 10px;margin: 150px auto;background: #fff;text-align: center;padding: 20px;box-shadow: 0 15px 30px #000;">
|
|
|
|
|
|
<h2 style="color: #000;font-size: 22px;font-weight: bold;">
|
|
عنوان پیام:
|
|
</h2>
|
|
<p style="color: #000;margin-bottom: 20px;">${subject}</p>
|
|
|
|
|
|
<h2 style="color: #000;font-size: 22px;font-weight: bold;">
|
|
متن پیام:
|
|
</h2>
|
|
<p style="color: #000;margin-bottom: 20px;">${message}</p>
|
|
|
|
|
|
<div style="text-align: center;margin-top: 20px;">
|
|
<h2 style="color: #000;font-size: 22px;font-weight: bold;">
|
|
مشخصات فرستنده:
|
|
</h2>
|
|
<span>${name}</span>
|
|
<span>-</span>
|
|
<span>${email}</span>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
<span style="opacity: 0">${Date.now()}</span>
|
|
</div>
|
|
`
|
|
|
|
// send mail with defined transport object
|
|
transporter.sendMail({
|
|
from: '"OrisOxin Website" <client@negareh-demo2.ir>', // sender address
|
|
to: 'info@orisoxin.com', // list of receivers
|
|
subject: subject,
|
|
text: message,
|
|
html: emailHTMLTemplate
|
|
})
|
|
|
|
|
|
new ContactUs(data).save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
return res.json({message: _sr[req.body.locale].response.success_save})
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
ContactUs.find({}, (err, data) => {
|
|
if (err) return res500(res, err)
|
|
return res.json(data)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
ContactUs.findById(req.params.id, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
data.read = true
|
|
data.save((err, message) => {
|
|
if (err) console.log(err)
|
|
return res.json(message)
|
|
})
|
|
})
|
|
}
|
|
]
|