Commit 7255726e authored by Vadym Gidulian's avatar Vadym Gidulian

Merge branch 'dev'

parents c02986b5 5efb7818
openapi: 3.0.2
info:
title: Mailer service
version: 0.2.0
paths:
/{template}:
post:
summary: Send message with specified template
parameters:
- name: template
in: path
description: Template name
schema:
type: string
required: true
requestBody:
description: Message to be sent
required: true
content:
application/json:
schema:
type: object
required: true
properties:
to:
description: Recipient's address
type: string
format: email
required: true
subject:
description: Message subject
type: string
required: true
vars:
description: Map between variables used in template and their values
type: object
required: false
default: {}
example:
{
"to": "john.doe@example.com",
"subject": "Hello John!",
"vars": {
"name": "John Doe"
}
}
responses:
200:
description: Message has been sent to server
content:
application/json:
schema:
description: Result of sending
type: object
required: true
properties:
messageId:
description: Most servers *should* return the final Message-Id value used
type: string
accepted:
description: Recipients' addresses that were accepted by server
type: array
items:
description: Recipient's address
type: string
rejected:
description: Recipients' addresses that were rejected by server
type: array
items:
description: Recipient's address
type: string
message:
description: Sent message
type: object
properties:
from:
description: Sender's address
type: string
format: email
to:
description: Recipient's address
type: string
format: email
subject:
description: Message subject
type: string
html:
description: Sent message text, specified template compiled with provided variables
type: string
example:
{
"messageId": "<b6cb2a56-03b3-e782-acaf-480865b8d8b5@example.com>",
"accepted": ["john.doe@example.com"],
"rejected": [],
"message": {
"from": "mail@example.com",
"to": "john.doe@example.com",
"subject": "Hello John!",
"html": "Hello John Doe!"
}
}
400:
description: Invalid template name or parameters
...@@ -24,7 +24,7 @@ app.use((req, res, next) => { ...@@ -24,7 +24,7 @@ app.use((req, res, next) => {
app.post('/:template', async (req, res) => { app.post('/:template', async (req, res) => {
try { try {
req.params.template = jsvv(req.params.template, templateNameSchema); req.params.template = jsvv(req.params.template, templateNameSchema);
req.body = jsvv(req.body, requestSchema); req.body = jsvv(req.body, requestSchema);
} catch (e) { } catch (e) {
return res.status(400).send(e.message); return res.status(400).send(e.message);
} }
......
...@@ -41,18 +41,20 @@ transporter.verify((err) => { ...@@ -41,18 +41,20 @@ transporter.verify((err) => {
const sendMail = promisify(transporter.sendMail).bind(transporter); const sendMail = promisify(transporter.sendMail).bind(transporter);
module.exports = { module.exports = {
async sendMessage(template, request) { async sendMessage(template, request) {
const message = handlebars.compile(templatesModel.getTemplate(template))(request.vars || {}); const text = handlebars.compile(templatesModel.getTemplate(template))(request.vars);
const mailer = { const message = {
from: SMTP_SENDER, from: SMTP_SENDER,
to: request.to, to: request.to,
subject: request.subject, subject: request.subject,
html: message html: text
}; };
const {messageId, accepted, rejected} = await sendMail(mailer); const {messageId, accepted, rejected} = await sendMail(message);
return {messageId, accepted, rejected}; return {messageId, accepted, rejected, message};
} }
}; };
...@@ -9,7 +9,9 @@ module.exports = { ...@@ -9,7 +9,9 @@ module.exports = {
to: stringRequiredSchema, to: stringRequiredSchema,
subject: stringRequiredSchema, subject: stringRequiredSchema,
vars: { vars: {
type: Object type: Object,
required: true,
default: () => ({})
} }
} }
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment