UNPKG

tm-notification

Version:

This is the package exposing methods for the notification service

204 lines (181 loc) 5.72 kB
"use strict"; const axios = require("axios"); class EmailBuilder { constructor(clientId) { clientId = clientId || process.env.CLIENT_ID; this._axios = axios.create({ baseURL: process.env.API_GATEWAY_URL, headers: { "client-id": clientId } }); this.startBuild(); this.props["sender"] = process.env.PROJECT_NAME; this.props["attachments"] = []; } startBuild() { this.props = {}; this.props["attachments"] = []; return this; } setClientId(clientId) { this.props["clientId"] = clientId; return this; } setProvider(provider) { this.props["provider"] = provider; return this; } setType(type) { this.props["type"] = type; return this; } setHTML(html){ this.props["html"] = html; return this; } setFrom(from) { this.props["from"] = from; return this; } setSubject(subject) { this.props["subject"] = subject; return this; } setRecipients(recipients) { if (!Array.isArray(recipients)) recipients = [recipients]; this.props["recipients"] = recipients; return this; } setHeader(header = {}) { this.props["header"] = this.props.header || header; let that = this; return { /** * @param {String} title * @return {setTitle} */ setTitle(title) { that.props.header["title"] = title; return this; }, /** * @param {String} bgColor * @return {setTitle} */ setBGColor(bgColor) { that.props.header["bgColor"] = bgColor; return this; }, setAppLogo(appLogo) { that.props.header["appLogo"] = appLogo; return this; }, setAppUrl(appUrl) { that.props.header["appUrl"] = appUrl; return this; }, setAppName(appName) { that.props.header["appName"] = appName; return this; } } } setSenderId(senderId) { this.props["sender_id"] = senderId; return this; } setBody(body) { this.props["content"] = body; this.props["body"] = this.props.body || {}; this.props.body["introLines"] = this.props.body["introLines"] || []; this.props.body["outroLines"] = this.props.body["outroLines"] || []; let that = this; return { setContent(content) { that.props["content"] = content; that.props.body["content"] = content; return this; }, setGreeting(greeting) { that.props.body["greeting"] = greeting; return this; }, setIntroLine(lines) { if (!Array.isArray(lines)) { that.props.body["introLines"].push(lines); } else { that.props.body["introLines"] = that.props.body["introLines"].concat(lines); } return this; }, setOutroLine(lines) { if (!Array.isArray(lines)) { that.props.body["outroLines"].push(lines); } else { that.props.body["outroLines"] = that.props.body["outroLines"].concat(lines); } return this; } } } setButton() { this.props["button"] = this.props.button || {}; let that = this; return { setLevel(level) { that.props.button["level"] = level; return this; }, setActionUrl(actionLink) { that.props.button["actionUrl"] = actionLink; return this; }, setActionText(actionText) { that.props.button["actionText"] = actionText; return this; } } } setReplyTo(replyTo) { this.props["replyTo"] = replyTo; return this; } /** * * @return {EmailBuilder} * @param {[String] | String} attachments */ setAttachments(attachments) { if (!Array.isArray(attachments)) { this.props["attachments"].push(attachments); } else { this.props["attachments"] = this.props["attachments"].concat(attachments); } return this; } build() { return this.props; } async send() { try { const config = { headers: { "client-id": this.props.clientId || process.env.CLIENT_ID } }; if (process.env.NOTIFICATION_SERVICE_URL){ console.log("Sending Request to --------> " + process.env.NOTIFICATION_SERVICE_URL); return {data: (await this._axios.post(`${process.env.NOTIFICATION_SERVICE_URL}/v1/email`, this.build(), config)).data.data}; } console.log("Sending Request to --------> " + process.env.API_GATEWAY_URL); return {data: (await this._axios.post(`notifications/v1/email`, this.build(), config)).data.data}; } catch (e) { const error = resolveAxiosError(e); logger(e.message, (new Error(e)).stack, error); return {error: error?.raw?.message || error}; } } } module.exports = EmailBuilder;