From 8a705e91b98ec6857d1d1ecd87ff78289112dbc1 Mon Sep 17 00:00:00 2001 From: Boaz Poolman Date: Wed, 24 Mar 2021 18:36:16 +0100 Subject: [PATCH] Add getAllFromDatabase service for each config type --- services/core-store.js | 16 ++++++++++++++++ services/role-permissions.js | 33 +++++++++++++++++++++++++++++++++ services/webhooks.js | 16 ++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/services/core-store.js b/services/core-store.js index da97c3a..e363651 100644 --- a/services/core-store.js +++ b/services/core-store.js @@ -43,6 +43,22 @@ module.exports = { } }, + /** + * Get all core-store config from the db. + * + * @returns {object} Object with key value pairs of configs. + */ + getAllFromDatabase: async () => { + const coreStore = await strapi.query(coreStoreQueryString).find({ _limit: -1 }); + let configs = {}; + + Object.values(coreStore).map( ({ id, value, ...config }) => { + configs[`${configPrefix}.${config.key}`] = { value: JSON.parse(value), ...config }; + }); + + return configs; + }, + /** * Import all core-store config files into the db. * diff --git a/services/role-permissions.js b/services/role-permissions.js index 598e488..18234b4 100644 --- a/services/role-permissions.js +++ b/services/role-permissions.js @@ -63,6 +63,39 @@ module.exports = { } }, + /** + * Get all role-permissions config from the db. + * + * @returns {object} Object with key value pairs of configs. + */ + getAllFromDatabase: async () => { + const service = + strapi.plugins['users-permissions'].services.userspermissions; + + const [roles, plugins] = await Promise.all([ + service.getRoles(), + service.getPlugins(), + ]); + + const rolesWithPermissions = await Promise.all( + roles.map(async role => service.getRole(role.id, plugins)) + ); + + const sanitizedRolesArray = rolesWithPermissions.map(role => + sanitizeEntity(role, { + model: strapi.plugins['users-permissions'].models.role, + }) + ); + + let configs = {}; + + Object.values(sanitizedRolesArray).map((config) => { + configs[`${configPrefix}.${config.type}`] = config; + }); + + return configs; + }, + /** * Import all role-permissions config files into the db. * diff --git a/services/webhooks.js b/services/webhooks.js index 082e1f1..e8068a5 100644 --- a/services/webhooks.js +++ b/services/webhooks.js @@ -40,6 +40,22 @@ module.exports = { } }, + /** + * Get all webhook config from the db. + * + * @returns {object} Object with key value pairs of configs. + */ + getAllFromDatabase: async () => { + const webhooks = await strapi.query(webhookQueryString).find({ _limit: -1 }); + let configs = {}; + + Object.values(webhooks).map( (config) => { + configs[`${configPrefix}.${config.id}`] = config; + }); + + return configs; + }, + /** * Import all webhook config files into the db. *