2021-04-23 19:07:39 +02:00
|
|
|
const CouchDB = require("../db")
|
2021-05-06 11:51:21 +02:00
|
|
|
const { getScopedConfig, 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)
|
2021-05-05 19:00:22 +02:00
|
|
|
// TODO: use more granular settings in the future if required
|
2021-05-11 16:08:59 +02:00
|
|
|
let settings = await getScopedConfig(db, { type: Configs.SETTINGS }) || {}
|
|
|
|
if (!settings || !settings.platformUrl) {
|
2021-04-23 19:54:12 +02:00
|
|
|
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-05-07 15:30:37 +02:00
|
|
|
[TemplateBindings.LOGO_URL]:
|
|
|
|
checkSlashesInUrl(`${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(
|
2021-05-05 19:13:59 +02:00
|
|
|
`${URL}/reset?code=${code}`
|
2021-05-05 13:11:06 +02:00
|
|
|
)
|
|
|
|
break
|
|
|
|
case EmailTemplatePurpose.INVITATION:
|
|
|
|
context[TemplateBindings.INVITE_CODE] = code
|
2021-05-10 16:05:06 +02:00
|
|
|
context[TemplateBindings.INVITE_URL] = checkSlashesInUrl(
|
2021-05-05 19:13:59 +02:00
|
|
|
`${URL}/invite?code=${code}`
|
2021-05-05 13:11:06 +02:00
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return context
|
2021-04-22 18:58:14 +02:00
|
|
|
}
|