2021-04-23 19:07:39 +02:00
|
|
|
const CouchDB = require("../db")
|
2021-04-22 18:57:38 +02:00
|
|
|
const { getConfigParams, StaticDatabases } = require("@budibase/auth").db
|
2021-05-05 13:11:31 +02:00
|
|
|
const {
|
|
|
|
Configs,
|
|
|
|
TemplateBindings,
|
|
|
|
LOGO_URL,
|
|
|
|
EmailTemplatePurpose,
|
|
|
|
} = require("../constants")
|
2021-04-22 18:57:38 +02:00
|
|
|
const { checkSlashesInUrl } = require("./index")
|
|
|
|
const env = require("../environment")
|
|
|
|
|
|
|
|
const LOCAL_URL = `http://localhost:${env.PORT}`
|
|
|
|
const BASE_COMPANY = "Budibase"
|
|
|
|
|
2021-05-05 13:11:06 +02:00
|
|
|
exports.getSettingsTemplateContext = async (purpose, code = null) => {
|
2021-04-22 18:57:38 +02:00
|
|
|
const db = new CouchDB(StaticDatabases.GLOBAL.name)
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getConfigParams(Configs.SETTINGS, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
let settings = response.rows.map(row => row.doc)[0] || {}
|
2021-04-23 19:54:12 +02:00
|
|
|
if (!settings.platformUrl) {
|
|
|
|
settings.platformUrl = LOCAL_URL
|
2021-04-22 18:57:38 +02:00
|
|
|
}
|
2021-04-23 19:54:12 +02:00
|
|
|
const URL = settings.platformUrl
|
2021-05-05 13:11:06 +02:00
|
|
|
const context = {
|
2021-04-22 18:57:38 +02:00
|
|
|
[TemplateBindings.LOGO_URL]: settings.logoUrl || LOGO_URL,
|
2021-04-23 19:54:12 +02:00
|
|
|
[TemplateBindings.PLATFORM_URL]: URL,
|
2021-04-22 18:57:38 +02:00
|
|
|
[TemplateBindings.COMPANY]: settings.company || BASE_COMPANY,
|
2021-04-26 15:16:05 +02:00
|
|
|
[TemplateBindings.DOCS_URL]:
|
|
|
|
settings.docsUrl || "https://docs.budibase.com/",
|
2021-04-26 15:14:51 +02:00
|
|
|
[TemplateBindings.LOGIN_URL]: checkSlashesInUrl(`${URL}/login`),
|
2021-04-26 15:24:07 +02:00
|
|
|
[TemplateBindings.CURRENT_DATE]: new Date().toISOString(),
|
|
|
|
[TemplateBindings.CURRENT_YEAR]: new Date().getFullYear(),
|
2021-04-22 18:57:38 +02:00
|
|
|
}
|
2021-05-05 13:11:06 +02:00
|
|
|
// attach purpose specific context
|
|
|
|
switch (purpose) {
|
|
|
|
case EmailTemplatePurpose.PASSWORD_RECOVERY:
|
|
|
|
context[TemplateBindings.RESET_CODE] = code
|
|
|
|
context[TemplateBindings.RESET_URL] = checkSlashesInUrl(
|
|
|
|
`${URL}/reset/${code}`
|
|
|
|
)
|
|
|
|
break
|
|
|
|
case EmailTemplatePurpose.INVITATION:
|
|
|
|
context[TemplateBindings.INVITE_CODE] = code
|
|
|
|
context[TemplateBindings.REGISTRATION_URL] = checkSlashesInUrl(
|
|
|
|
`${URL}/registration/${code}`
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return context
|
2021-04-22 18:58:14 +02:00
|
|
|
}
|