From a071d7b365eb448952fe0fb111382cf614b8914d Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 22 Apr 2021 14:07:00 +0100 Subject: [PATCH] tidy up --- packages/auth/src/db/utils.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 2f34bc0c51..d80d1f0662 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -132,29 +132,28 @@ const determineScopedConfig = async function(db, { type, user, group }) { } ) ) - const configs = response.rows.map(row => row.doc) + const configs = response.rows.map(row => { + const config = 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.score = 4 + } else if (config._id.includes(generateConfigID({ type, user }))) { + // Config is specific to a user only + config.score = 3 + } else if (config._id.includes(generateConfigID({ type, group }))) { + // Config is specific to a group only + config.score = 2 + } else if (config._id.includes(generateConfigID({ type }))) { + // Config is specific to a type only + config.score = 1 } - - // 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 }) + // Find the config with the most granular scope based on context + const scopedConfig = configs.sort((a, b) => b.score - a.score)[0] + return scopedConfig }