putting together the bulk of the email sending/config options.
This commit is contained in:
parent
163035cac7
commit
f6e25c3792
|
@ -36,6 +36,7 @@
|
|||
"koa-session": "^5.12.0",
|
||||
"koa-static": "^5.0.0",
|
||||
"node-fetch": "^2.6.1",
|
||||
"nodemailer": "^6.5.0",
|
||||
"passport-google-oauth": "^2.0.0",
|
||||
"passport-jwt": "^4.0.0",
|
||||
"passport-local": "^1.0.0",
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
const { EmailTemplatePurpose, TemplateTypes } = require("../constants")
|
||||
const { getTemplateByPurpose } = require("../constants/templates")
|
||||
const CouchDB = require("../../../db")
|
||||
const { StaticDatabases, determineScopedConfig } = require("@budibase/auth").db
|
||||
const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../../../constants")
|
||||
const { getTemplateByPurpose } = require("../../../constants/templates")
|
||||
const { getSettingsTemplateContext } = require("../../../utilities/templates")
|
||||
const { processString } = require("@budibase/string-templates")
|
||||
const { getSettingsTemplateContext } = require("./templates")
|
||||
const nodemailer = require("nodemailer")
|
||||
|
||||
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
||||
const TYPE = TemplateTypes.EMAIL
|
||||
|
||||
const FULL_EMAIL_PURPOSES = [
|
||||
|
@ -10,6 +14,14 @@ const FULL_EMAIL_PURPOSES = [
|
|||
EmailTemplatePurpose.PASSWORD_RECOVERY,
|
||||
]
|
||||
|
||||
function createSMTPTransport(config) {
|
||||
const transport = nodemailer.createTransport({
|
||||
port: config.port,
|
||||
host: config.host,
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
exports.buildEmail = async (email, user, purpose) => {
|
||||
// this isn't a full email
|
||||
if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
|
||||
|
@ -37,3 +49,16 @@ exports.buildEmail = async (email, user, purpose) => {
|
|||
body,
|
||||
})
|
||||
}
|
||||
|
||||
exports.sendEmail = async ctx => {
|
||||
const { groupId, email, purpose } = ctx.request.body
|
||||
const db = new CouchDB(GLOBAL_DB)
|
||||
const params = {}
|
||||
if (groupId) {
|
||||
params.group = groupId
|
||||
}
|
||||
params.type = Configs.SMTP
|
||||
const config = await determineScopedConfig(db, params)
|
||||
const transport = createSMTPTransport(config)
|
||||
}
|
||||
|
|
@ -6,11 +6,52 @@ const { Configs } = require("../../../constants")
|
|||
|
||||
const router = Router()
|
||||
|
||||
function smtpValidation() {
|
||||
return Joi.object({
|
||||
port: Joi.number().required(),
|
||||
host: Joi.string().required(),
|
||||
from: Joi.string().email().required(),
|
||||
secure: Joi.boolean().optional(),
|
||||
selfSigned: Joi.boolean().optional(),
|
||||
auth: Joi.object({
|
||||
type: Joi.string().valid("login", "oauth2", null),
|
||||
user: Joi.string().required(),
|
||||
pass: Joi.string().valid("", null),
|
||||
}).optional(),
|
||||
}).unknown(true)
|
||||
}
|
||||
|
||||
function settingValidation() {
|
||||
return Joi.object({
|
||||
url: Joi.string().valid("", null),
|
||||
logoUrl: Joi.string().valid("", null),
|
||||
company: Joi.string().required(),
|
||||
}).unknown(true)
|
||||
}
|
||||
|
||||
function googleValidation() {
|
||||
return Joi.object({
|
||||
clientID: Joi.string().required(),
|
||||
clientSecret: Joi.string().required(),
|
||||
callbackURL: Joi.string().required(),
|
||||
}).unknown(true)
|
||||
}
|
||||
|
||||
function buildConfigSaveValidation() {
|
||||
// prettier-ignore
|
||||
return joiValidator.body(Joi.object({
|
||||
type: Joi.string().valid(...Object.values(Configs)).required(),
|
||||
}).required().unknown(true))
|
||||
config: Joi.alternatives()
|
||||
.conditional("type", {
|
||||
switch: [
|
||||
{ is: Configs.SMTP, then: smtpValidation() },
|
||||
{ is: Configs.SETTINGS, then: settingValidation() },
|
||||
{ is: Configs.ACCOUNT, then: Joi.object().unknown(true) },
|
||||
{ is: Configs.GOOGLE, then: googleValidation() }
|
||||
],
|
||||
}),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
router
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
const Router = require("@koa/router")
|
||||
const controller = require("../../controllers/admin/email")
|
||||
const { EmailTemplatePurpose } = require("../../../constants")
|
||||
const joiValidator = require("../../../middleware/joi-validator")
|
||||
const Joi = require("joi")
|
||||
|
||||
const router = Router()
|
||||
|
||||
function buildEmailSendValidation() {
|
||||
// prettier-ignore
|
||||
return joiValidator.body(Joi.object({
|
||||
email: Joi.string().email(),
|
||||
groupId: Joi.string().allow("", null),
|
||||
purpose: Joi.string().allow(...Object.values(EmailTemplatePurpose)),
|
||||
}).required().unknown(true))
|
||||
}
|
||||
|
||||
router
|
||||
.post("/api/admin/email/send", buildEmailSendValidation(), controller.sendEmail)
|
||||
|
||||
module.exports = router
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue