diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 4bc0427976..40f5f05b70 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -123,7 +123,7 @@ const getConfigParams = ({ type, group, user }, otherProps = {}) => { * @param {Object} scopes - the type, group and userID scopes of the configuration. * @returns The most granular configuration document based on the scope. */ -const determineScopedConfig = async function (db, { type, user, group }) { +const getScopedFullConfig = async function (db, { type, user, group }) { const response = await db.allDocs( getConfigParams( { type, user, group }, @@ -160,6 +160,12 @@ const determineScopedConfig = async function (db, { type, user, group }) { return scopedConfig.doc } +async function getScopedConfig(db, params) { + const configDoc = await getScopedFullConfig(db, params) + return configDoc && configDoc.config ? configDoc.config : configDoc +} + +exports.getScopedConfig = getScopedConfig exports.generateConfigID = generateConfigID exports.getConfigParams = getConfigParams -exports.determineScopedConfig = determineScopedConfig +exports.getScopedFullConfig = getScopedFullConfig diff --git a/packages/worker/src/api/controllers/admin/auth.js b/packages/worker/src/api/controllers/admin/auth.js index 04c835a2e7..598e43e8ad 100644 --- a/packages/worker/src/api/controllers/admin/auth.js +++ b/packages/worker/src/api/controllers/admin/auth.js @@ -93,7 +93,7 @@ exports.logout = async ctx => { */ exports.googlePreAuth = async (ctx, next) => { const db = new CouchDB(GLOBAL_DB) - const config = await authPkg.db.determineScopedConfig(db, { + const config = await authPkg.db.getScopedConfig(db, { type: Configs.GOOGLE, group: ctx.query.group, }) @@ -107,7 +107,7 @@ exports.googlePreAuth = async (ctx, next) => { exports.googleAuth = async (ctx, next) => { const db = new CouchDB(GLOBAL_DB) - const config = await authPkg.db.determineScopedConfig(db, { + const config = await authPkg.db.getScopedConfig(db, { type: Configs.GOOGLE, group: ctx.query.group, }) diff --git a/packages/worker/src/api/controllers/admin/configs.js b/packages/worker/src/api/controllers/admin/configs.js index 790fbcbe35..121cfa1c0b 100644 --- a/packages/worker/src/api/controllers/admin/configs.js +++ b/packages/worker/src/api/controllers/admin/configs.js @@ -3,7 +3,7 @@ const { generateConfigID, StaticDatabases, getConfigParams, - determineScopedConfig, + getScopedFullConfig, } = require("@budibase/auth").db const { Configs } = require("../../../constants") const email = require("../../../utilities/email") @@ -16,7 +16,7 @@ exports.save = async function (ctx) { // Config does not exist yet if (!ctx.request.body._id) { - config._id = generateConfigID({ + ctx.request.body._id = generateConfigID({ type, group, user, @@ -31,7 +31,7 @@ exports.save = async function (ctx) { } try { - const response = await db.put(config) + const response = await db.put(ctx.request.body) ctx.body = { type, _id: response.id, @@ -73,7 +73,7 @@ exports.find = async function (ctx) { try { // Find the config with the most granular scope based on context - const scopedConfig = await determineScopedConfig(db, { + const scopedConfig = await getScopedFullConfig(db, { type: ctx.params.type, user: userId, group: groupId, diff --git a/packages/worker/src/utilities/email.js b/packages/worker/src/utilities/email.js index f7b9284402..e06b98a31e 100644 --- a/packages/worker/src/utilities/email.js +++ b/packages/worker/src/utilities/email.js @@ -1,6 +1,6 @@ const nodemailer = require("nodemailer") const CouchDB = require("../db") -const { StaticDatabases, determineScopedConfig } = require("@budibase/auth").db +const { StaticDatabases, getScopedConfig } = require("@budibase/auth").db const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../constants") const { getTemplateByPurpose } = require("../constants/templates") const { getSettingsTemplateContext } = require("./templates") @@ -97,7 +97,7 @@ async function getSmtpConfiguration(db, groupId = null) { if (groupId) { params.group = groupId } - return determineScopedConfig(db, params) + return getScopedConfig(db, params) } /** diff --git a/packages/worker/src/utilities/templates.js b/packages/worker/src/utilities/templates.js index e5d94859f2..ea03f2727b 100644 --- a/packages/worker/src/utilities/templates.js +++ b/packages/worker/src/utilities/templates.js @@ -1,5 +1,5 @@ const CouchDB = require("../db") -const { determineScopedConfig, StaticDatabases } = require("@budibase/auth").db +const { getScopedConfig, StaticDatabases } = require("@budibase/auth").db const { Configs, TemplateBindings, @@ -15,7 +15,7 @@ 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 - const settings = await determineScopedConfig(db, { type: Configs.SETTINGS }) + const settings = await getScopedConfig(db, { type: Configs.SETTINGS }) if (!settings.platformUrl) { settings.platformUrl = LOCAL_URL }