Add send endpoint

This commit is contained in:
Evan Morikawa 2016-07-14 11:34:23 -07:00
parent b58c11605c
commit 6ce54c2a34
4 changed files with 70 additions and 2 deletions

View file

@ -15,6 +15,7 @@
"hapi-swagger": "6.1.0",
"inert": "4.0.0",
"joi": "8.4.2",
"nodemailer": "2.5.0",
"nylas-core": "0.x.x",
"nylas-sync": "0.x.x",
"nylas-metrics": "0.x.x",

View file

@ -24,10 +24,9 @@ function inflateTransactions(db, transactionModels = []) {
return ModelKlass.findAll({where: {id: ids}, include: includes})
.then((models = []) => {
for (const model of models) {
model.dataValues.object = object
const tsForId = byObjectIds[model.id];
if (!tsForId || tsForId.length === 0) { continue; }
for (const t of tsForId) { t.attributes = model.dataValues; }
for (const t of tsForId) { t.attributes = model.toJSON(); }
}
})
})).then(() => transactions.map(JSON.stringify).join("\n"))

View file

@ -0,0 +1,53 @@
const Joi = require('joi');
const nodemailer = require('nodemailer');
const {DatabaseConnector} = require('nylas-core');
function toParticipant(payload) {
return payload.map((p) => `${p.name} <${p.email}>`).join(',')
}
module.exports = (server) => {
server.route({
method: 'POST',
path: '/send',
config: {
validate: {
payload: {
subject: Joi.string(),
reply_to_message_id: Joi.number().integer(),
from: Joi.array(),
reply_to: Joi.array(),
to: Joi.array(),
cc: Joi.array(),
bcc: Joi.array(),
body: Joi.string(),
file_ids: Joi.array(),
},
},
},
handler: (request, reply) => { DatabaseConnector.forShared().then((db) => {
const accountId = request.auth.credentials.id;
db.Account.findById(accountId).then((account) => {
const sender = nodemailer.createTransport(account.smtpConfig());
const data = request.payload;
const msg = {}
for (key of ['from', 'to', 'cc', 'bcc']) {
if (data[key]) msg[key] = toParticipant(data[key])
}
msg.subject = data.subject,
msg.html = data.body,
console.log("------------------------------------------------")
console.log(msg)
sender.sendMail(msg, (error, info) => {
console.log("DONE ===========================================");
console.log(error)
console.log(info)
if (error) { reply(error).code(400) }
else { reply(info.response) }
});
})
})},
});
};

View file

@ -67,6 +67,21 @@ module.exports = (sequelize, Sequelize) => {
return null;
}
},
smtpConfig: function smtpConfig() {
if (this.provider !== "imap") {
throw new Error("Non IMAP not yet supported")
}
const {smtp_username, smtp_password} = this.decryptedCredentials();
const {smtp_host, smtp_port, ssl_required} = this.connectionSettings;
return {
port: smtp_port, host: smtp_host, secure: ssl_required,
auth: { user: smtp_username, pass: smtp_password, },
}
}
},
});