strapi-plugin-config-sync/server/types/admin-role.js

84 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-10-15 14:54:58 +02:00
const { sanitizeConfig } = require('../utils');
const ConfigType = require("../services/type");
const AdminRolePermissionsConfigType = class AdminRolePermissionsConfigType extends ConfigType {
/**
* Import a single role-permissions config file into the db.
*
* @param {string} configName - The name of the config file.
* @param {string} configContent - The JSON content of the config file.
* @returns {void}
*/
importSingle = async (configName, configContent) => {
// Check if the config should be excluded.
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${this.configPrefix}.${configName}`);
if (shouldExclude) return;
const queryAPI = strapi.query(this.queryString);
const existingConfig = await queryAPI
.findOne({ where: { [this.uid]: configName } });
if (existingConfig && configContent === null) {
2021-10-27 13:45:55 +02:00
await queryAPI.delete({
where: { [this.uid]: configName },
populate: this.relations.map(({ relationName }) => relationName),
});
2021-10-15 14:54:58 +02:00
return;
}
if (!existingConfig) {
2021-10-27 13:45:55 +02:00
// Format JSON fields.
2021-10-15 14:54:58 +02:00
const query = { ...configContent };
this.jsonFields.map((field) => query[field] = JSON.stringify(configContent[field]));
2021-10-27 13:45:55 +02:00
this.relations.map(({ queryString }) => {
const queryAPI = strapi.query(queryString);
// Compare relations
// Make changes to the db
});
await queryAPI.create({ data: query });
2021-10-15 14:54:58 +02:00
} else {
const query = { ...configContent };
this.jsonFields.map((field) => query[field] = JSON.stringify(configContent[field]));
2021-10-27 13:45:55 +02:00
await queryAPI.update({ where: { [this.uid]: configName }, data: { ...query } });
2021-10-15 14:54:58 +02:00
}
}
/**
* Get all role-permissions config from the db.
*
* @returns {object} Object with key value pairs of configs.
*/
getAllFromDatabase = async () => {
const AllConfig = await strapi.query(this.queryString).findMany({ limit: 0 });
const configs = {};
await Promise.all(Object.values(AllConfig).map(async (config) => {
// Check if the config should be excluded.
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${this.configPrefix}.${config[this.uid]}`);
if (shouldExclude) return;
config = sanitizeConfig(config);
const existingPermissions = await strapi.admin.services.permission.findMany({
where: { role: { code: config.code } },
});
existingPermissions.map((permission) => sanitizeConfig(permission));
const formattedObject = { ...config, permissions: existingPermissions };
this.jsonFields.map((field) => formattedObject[field] = JSON.parse(config[field]));
configs[`${this.configPrefix}.${config[this.uid]}`] = formattedObject;
}));
return configs;
}
};
module.exports = AdminRolePermissionsConfigType;