2014-01-22 21:46:44 +01:00
|
|
|
config = require '../../server_config'
|
2014-02-04 23:08:20 +01:00
|
|
|
log = require 'winston'
|
2014-01-24 11:47:14 -08:00
|
|
|
mail = require '../commons/mail'
|
2014-04-10 14:59:32 -07:00
|
|
|
User = require '../users/User'
|
2014-01-03 10:32:13 -08:00
|
|
|
|
2014-02-04 22:29:13 +01:00
|
|
|
module.exports.setup = (app) ->
|
2014-01-03 10:32:13 -08:00
|
|
|
app.post '/contact', (req, res) ->
|
2014-02-24 20:27:38 -08:00
|
|
|
return res.end() unless req.user
|
2014-02-04 23:08:20 +01:00
|
|
|
log.info "Sending mail from #{req.body.email} saying #{req.body.message}"
|
2014-01-03 14:28:26 -08:00
|
|
|
if config.isProduction
|
2014-04-10 14:59:32 -07:00
|
|
|
createMailOptions req.body.email, req.body.message, req.user, req.body.recipientID, req.body.subject, (options) ->
|
|
|
|
mail.transport.sendMail options, (error, response) ->
|
|
|
|
if error
|
|
|
|
log.error "Error sending mail: #{error.message or error}"
|
|
|
|
else
|
|
|
|
log.info "Mail sent successfully. Response: #{response.message}"
|
2014-01-03 10:32:13 -08:00
|
|
|
return res.end()
|
|
|
|
|
2014-04-10 14:59:32 -07:00
|
|
|
createMailOptions = (sender, message, user, recipientID, subject, done) ->
|
2014-01-03 10:32:13 -08:00
|
|
|
# TODO: use email templates here
|
|
|
|
options =
|
|
|
|
from: config.mail.username
|
|
|
|
to: config.mail.username
|
|
|
|
replyTo: sender
|
2014-04-10 14:59:32 -07:00
|
|
|
subject: "[CodeCombat] #{subject ? ('Feedback - ' + sender)}"
|
2014-01-03 10:32:13 -08:00
|
|
|
text: "#{message}\n\nUsername: #{user.get('name') or 'Anonymous'}\nID: #{user._id}"
|
2014-04-10 14:59:32 -07:00
|
|
|
#html: message.replace '\n', '<br>\n'
|
|
|
|
|
2014-06-19 13:42:51 -07:00
|
|
|
if recipientID and (user.isAdmin() or ('employer' in (user.get('permissions') ? [])))
|
2014-04-10 14:59:32 -07:00
|
|
|
User.findById(recipientID, 'email').exec (err, document) ->
|
|
|
|
if err
|
|
|
|
log.error "Error looking up recipient to email from #{recipientID}: #{err}" if err
|
|
|
|
else
|
2014-06-24 14:35:28 -07:00
|
|
|
options.bcc = [options.to, sender]
|
2014-04-10 14:59:32 -07:00
|
|
|
options.to = document.get('email')
|
|
|
|
done options
|
|
|
|
else
|
2014-07-01 10:16:26 +08:00
|
|
|
done options
|