Skip to content

Commit

Permalink
SBERDOMA-266 extract renderTemplate and _choseMessageTransport logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pahaz committed May 5, 2021
1 parent a5d436f commit 8e66027
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 64 deletions.
18 changes: 12 additions & 6 deletions apps/condo/domains/notification/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ async function _sendMessageByAdapter (transport, adapter, messageContext) {
return await adapter.send(messageContext)
}

async function _choseMessageTransport (message) {
// TODO(pahaz): we should chose the best transport for the message.
// We can chose transport depends on the message.type?
// or use something like message.user.profile.preferredNotificationTransport if user want to get messages from TG
return EMAIL_TRANSPORT
}

async function deliveryMessage (messageId) {
const { keystone } = await getSchemaCtx('Message')

const messages = await Message.getAll(keystone, { id: messageId })
if (messages.length !== 1) throw new Error('message id not found or found multiple results')

const message = messages[0]
if (message.id !== messageId) throw new Error('get message by id has wrong result')

// TODO(pahaz): extend this logic
// 1) we should chose the best transport for the message
// 2) then sending it by the transport
// For example: if we want to send invite to user and he wants to get receive messages by TG we should schedule
// TG transport sending
const transport = EMAIL_TRANSPORT
const transport = await _choseMessageTransport(message)

if (message.id !== messageId) throw new Error('get message by id wrong result')
if (message.status !== MESSAGE_SENDING_STATUS && message.status !== MESSAGE_RESENDING_STATUS) {
Expand Down
37 changes: 37 additions & 0 deletions apps/condo/domains/notification/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const conf = require('@core/config')

const { INVITE_NEW_EMPLOYEE_MESSAGE_TYPE, MESSAGE_TRANSPORTS } = require('./constants')

async function renderTemplate (transport, message) {
if (!MESSAGE_TRANSPORTS.includes(transport)) throw new Error('unexpected transport argument')

// TODO(pahaz): we need to decide where to store templates! HArDCODE!
// TODO(pahaz): write the logic here!
// 1) we should find message template by TYPE + LANG
// 2) we should render the template and return transport context

const serverUrl = conf.SERVER_URL
if (message.type === INVITE_NEW_EMPLOYEE_MESSAGE_TYPE) {
const { organizationName, inviteCode } = message.meta

if (message.lang === 'en') {
return {
subject: 'You are invited to join organization as employee',
text: `Organization "${organizationName}" invited you as employee.\n` +
`Click to the link to join: ${serverUrl}/auth/invite/${inviteCode}`,
}
} else if (message.lang === 'ru') {
return {
subject: 'Вас пригласили присоединиться к организации в качестве сотрудника',
text: `Администратор организации "${organizationName}" приглашает вас в качестве сотрудника.\n` +
`Перейдите по ссылке, чтобы присоединиться: ${serverUrl}/auth/invite/${inviteCode}`,
}
}
}

throw new Error('unknown template or lang')
}

module.exports = {
renderTemplate,
}
33 changes: 3 additions & 30 deletions apps/condo/domains/notification/transports/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,16 @@ const FormData = require('form-data')

const conf = require('@core/config')

const { INVITE_NEW_EMPLOYEE_MESSAGE_TYPE } = require('../constants')
const { EMAIL_TRANSPORT } = require('../constants')
const { renderTemplate } = require('../templates')

const EMAIL_API_CONFIG = (conf.EMAIL_API_CONFIG) ? JSON.parse(conf.EMAIL_API_CONFIG) : null

async function renderTemplate (message) {
// TODO(pahaz): we need to decide where to store templates! HArDCODE!
// TODO(pahaz): write the logic here!
// 1) we should find message template by TYPE + LANG
// 2) we should render the template and return transport context

const serverUrl = conf.SERVER_URL
if (message.type === INVITE_NEW_EMPLOYEE_MESSAGE_TYPE) {
const { organizationName, inviteCode } = message.meta

if (message.lang === 'en') {
return {
subject: 'You are invited to join organization as employee',
text: `Organization "${organizationName}" invited you as employee.\n` +
`Click to the link to join: ${serverUrl}/auth/invite/${inviteCode}`,
}
} else if (message.lang === 'ru') {
return {
subject: 'Вас пригласили присоединиться к организации в качестве сотрудника',
text: `Администратор организации "${organizationName}" приглашает вас в качестве сотрудника.\n` +
`Перейдите по ссылке, чтобы присоединиться: ${serverUrl}/auth/invite/${inviteCode}`,
}
}
}

throw new Error('unknown template or lang')
}

async function prepareMessageToSend (message) {
const email = message.email || (message.user && message.user.email) || null
if (!email) throw new Error('on email to send')

const { subject, text, html } = await renderTemplate(message)
const { subject, text, html } = await renderTemplate(EMAIL_TRANSPORT, message)

return { to: email, subject, text, html }
}
Expand Down
31 changes: 3 additions & 28 deletions apps/condo/domains/notification/transports/sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,16 @@ const fetch = require('node-fetch')

const conf = require('@core/config')

const { INVITE_NEW_EMPLOYEE_MESSAGE_TYPE } = require('../constants')
const { SMS_TRANSPORT } = require('../constants')
const { renderTemplate } = require('../templates')

const SMS_API_CONFIG = (conf.SMS_API_CONFIG) ? JSON.parse(conf.SMS_API_CONFIG) : null

async function renderTemplate (message) {
// TODO(pahaz): we need to decide where to store templates! HArDCODE!
// TODO(pahaz): write the logic here!
// 1) we should find message template by TYPE + LANG
// 2) we should render the template and return transport context

const serverUrl = conf.SERVER_URL
if (message.type === INVITE_NEW_EMPLOYEE_MESSAGE_TYPE) {
const { organizationName, inviteCode } = message.meta

if (message.lang === 'en') {
return {
text: `Organization "${organizationName}" invited you as employee.\n` +
`Follow the link: ${serverUrl}/auth/invite/${inviteCode}`,
}
} else if (message.lang === 'ru') {
return {
text: `Администратор организации "${organizationName}" приглашает вас в качестве сотрудника.\n` +
`Перейдите по ссылке: ${serverUrl}/auth/invite/${inviteCode}`,
}
}
}

throw new Error('unknown template or lang')
}

async function prepareMessageToSend (message) {
const phone = message.phone || (message.user && message.user.phone) || null
if (!phone) throw new Error('on phone to send')

const { text } = await renderTemplate(message)
const { text } = await renderTemplate(SMS_TRANSPORT, message)

return { phone, message: text }
}
Expand Down

0 comments on commit 8e66027

Please sign in to comment.