|
|
|
@ -98,7 +98,7 @@ exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => { |
|
|
|
* Generates a new configuration ID. |
|
|
|
* @returns {string} The new configuration ID which the config doc can be stored under. |
|
|
|
*/ |
|
|
|
exports.generateConfigID = ({ type, group, user }) => { |
|
|
|
const generateConfigID = ({ type, group, user }) => { |
|
|
|
const scope = [type, group, user].filter(Boolean).join(SEPARATOR) |
|
|
|
|
|
|
|
return `${DocumentTypes.CONFIG}${SEPARATOR}${scope}` |
|
|
|
@ -107,7 +107,7 @@ exports.generateConfigID = ({ type, group, user }) => { |
|
|
|
/** |
|
|
|
* Gets parameters for retrieving configurations. |
|
|
|
*/ |
|
|
|
exports.getConfigParams = ({ type, group, user }, otherProps = {}) => { |
|
|
|
const getConfigParams = ({ type, group, user }, otherProps = {}) => { |
|
|
|
const scope = [type, group, user].filter(Boolean).join(SEPARATOR) |
|
|
|
|
|
|
|
return { |
|
|
|
@ -116,3 +116,48 @@ exports.getConfigParams = ({ type, group, user }, otherProps = {}) => { |
|
|
|
endkey: `${DocumentTypes.CONFIG}${SEPARATOR}${scope}${UNICODE_MAX}`, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Returns the most granular configuration document from the DB based on the type, group and userID passed. |
|
|
|
* @param {*} db - db instance to quer |
|
|
|
* @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 response = await db.allDocs( |
|
|
|
getConfigParams( |
|
|
|
{ type, user, group }, |
|
|
|
{ |
|
|
|
include_docs: true, |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
const configs = response.rows.map(row => row.doc) |
|
|
|
|
|
|
|
// Find the config with the most granular scope based on context
|
|
|
|
const scopedConfig = configs.find(config => { |
|
|
|
// Config is specific to a user and a group
|
|
|
|
if (config._id.includes(generateConfigID({ type, user, group }))) { |
|
|
|
return config |
|
|
|
} |
|
|
|
|
|
|
|
// Config is specific to a user
|
|
|
|
if (config._id.includes(generateConfigID({ type, user }))) { |
|
|
|
return config |
|
|
|
} |
|
|
|
|
|
|
|
// Config is specific to a group only
|
|
|
|
if (config._id.includes(generateConfigID({ type, group }))) { |
|
|
|
return config |
|
|
|
} |
|
|
|
|
|
|
|
// Config specific to a config type only
|
|
|
|
return config |
|
|
|
}) |
|
|
|
|
|
|
|
return scopedConfig |
|
|
|
} |
|
|
|
|
|
|
|
exports.generateConfigID = generateConfigID |
|
|
|
exports.getConfigParams = getConfigParams |
|
|
|
exports.determineScopedConfig = determineScopedConfig |
|
|
|
|