budibase/packages/worker/src/utilities/templates.js

50 lines
1.8 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../db")
const { getScopedConfig, StaticDatabases } = require("@budibase/auth").db
2021-05-05 13:11:31 +02:00
const {
Configs,
InternalTemplateBindings,
2021-05-05 13:11:31 +02:00
LOGO_URL,
EmailTemplatePurpose,
} = require("../constants")
const { checkSlashesInUrl } = require("./index")
const LOCAL_URL = `http://localhost:10000`
const BASE_COMPANY = "Budibase"
exports.getSettingsTemplateContext = async (purpose, code = null) => {
const db = new CouchDB(StaticDatabases.GLOBAL.name)
// 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 })) || {}
if (!settings || !settings.platformUrl) {
2021-04-23 19:54:12 +02:00
settings.platformUrl = LOCAL_URL
}
2021-04-23 19:54:12 +02:00
const URL = settings.platformUrl
const context = {
[InternalTemplateBindings.LOGO_URL]:
checkSlashesInUrl(`${URL}/${settings.logoUrl}`) || LOGO_URL,
[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/",
[InternalTemplateBindings.LOGIN_URL]: checkSlashesInUrl(`${URL}/login`),
[InternalTemplateBindings.CURRENT_DATE]: new Date().toISOString(),
[InternalTemplateBindings.CURRENT_YEAR]: new Date().getFullYear(),
}
// attach purpose specific context
switch (purpose) {
case EmailTemplatePurpose.PASSWORD_RECOVERY:
context[InternalTemplateBindings.RESET_CODE] = code
context[InternalTemplateBindings.RESET_URL] = checkSlashesInUrl(
`${URL}/builder/auth/reset?code=${code}`
)
break
case EmailTemplatePurpose.INVITATION:
context[InternalTemplateBindings.INVITE_CODE] = code
context[InternalTemplateBindings.INVITE_URL] = checkSlashesInUrl(
2021-05-18 12:18:06 +02:00
`${URL}/builder/invite?code=${code}`
)
break
}
return context
2021-04-22 18:58:14 +02:00
}