2021-07-15 18:57:02 +02:00
|
|
|
const { getScopedConfig, getGlobalDB } = require("@budibase/auth/db")
|
2021-05-05 13:11:31 +02:00
|
|
|
const {
|
|
|
|
Configs,
|
2021-05-17 16:30:24 +02:00
|
|
|
InternalTemplateBindings,
|
2021-05-05 13:11:31 +02:00
|
|
|
LOGO_URL,
|
|
|
|
EmailTemplatePurpose,
|
|
|
|
} = require("../constants")
|
2021-04-22 18:57:38 +02:00
|
|
|
const { checkSlashesInUrl } = require("./index")
|
2021-05-21 15:38:46 +02:00
|
|
|
const env = require("../environment")
|
2021-04-22 18:57:38 +02:00
|
|
|
|
2021-05-21 15:38:46 +02:00
|
|
|
const LOCAL_URL = `http://localhost:${env.CLUSTER_PORT || 10000}`
|
2021-04-22 18:57:38 +02:00
|
|
|
const BASE_COMPANY = "Budibase"
|
|
|
|
|
2021-07-22 23:36:16 +02:00
|
|
|
function addTenantToUrl(url, tenantId) {
|
|
|
|
if (env.MULTI_TENANCY) {
|
|
|
|
const char = url.indexOf("?") === -1 ? "?" : "&"
|
|
|
|
url += `${char}tenantId=${tenantId}`
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:57:02 +02:00
|
|
|
exports.getSettingsTemplateContext = async (tenantId, purpose, code = null) => {
|
2021-07-22 23:36:16 +02:00
|
|
|
const db = getGlobalDB(tenantId)
|
2021-05-05 19:00:22 +02:00
|
|
|
// TODO: use more granular settings in the future if required
|
2021-05-11 16:24:17 +02:00
|
|
|
let settings = (await getScopedConfig(db, { type: Configs.SETTINGS })) || {}
|
2021-05-11 16:08:59 +02:00
|
|
|
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-17 16:30:24 +02:00
|
|
|
[InternalTemplateBindings.LOGO_URL]:
|
2021-05-07 15:30:37 +02:00
|
|
|
checkSlashesInUrl(`${URL}/${settings.logoUrl}`) || LOGO_URL,
|
2021-05-17 16:30:24 +02:00
|
|
|
[InternalTemplateBindings.PLATFORM_URL]: URL,
|
|
|
|
[InternalTemplateBindings.COMPANY]: settings.company || BASE_COMPANY,
|
|
|
|
[InternalTemplateBindings.DOCS_URL]:
|
2021-04-26 15:16:05 +02:00
|
|
|
settings.docsUrl || "https://docs.budibase.com/",
|
2021-07-22 23:37:20 +02:00
|
|
|
[InternalTemplateBindings.LOGIN_URL]: checkSlashesInUrl(
|
|
|
|
addTenantToUrl(`${URL}/login`, tenantId)
|
|
|
|
),
|
2021-05-17 16:30:24 +02:00
|
|
|
[InternalTemplateBindings.CURRENT_DATE]: new Date().toISOString(),
|
|
|
|
[InternalTemplateBindings.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:
|
2021-05-17 16:30:24 +02:00
|
|
|
context[InternalTemplateBindings.RESET_CODE] = code
|
|
|
|
context[InternalTemplateBindings.RESET_URL] = checkSlashesInUrl(
|
2021-07-22 23:36:16 +02:00
|
|
|
addTenantToUrl(`${URL}/builder/auth/reset?code=${code}`, tenantId)
|
2021-05-05 13:11:06 +02:00
|
|
|
)
|
|
|
|
break
|
|
|
|
case EmailTemplatePurpose.INVITATION:
|
2021-05-17 16:30:24 +02:00
|
|
|
context[InternalTemplateBindings.INVITE_CODE] = code
|
|
|
|
context[InternalTemplateBindings.INVITE_URL] = checkSlashesInUrl(
|
2021-07-22 23:37:20 +02:00
|
|
|
addTenantToUrl(
|
|
|
|
`${URL}/builder/invite?code=${code}&tenantId=${tenantId}`,
|
|
|
|
tenantId
|
|
|
|
)
|
2021-05-05 13:11:06 +02:00
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return context
|
2021-04-22 18:58:14 +02:00
|
|
|
}
|