95 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-19 09:58:38 +02:00
'use strict';
const _ = require('lodash');
const pluralize = require('pluralize');
2019-07-19 18:14:13 +02:00
const { createModelConfigurationSchema } = require('./validation');
2019-07-19 09:58:38 +02:00
module.exports = {
/**
* Returns the list of available groups
*/
2019-07-19 18:14:13 +02:00
async listGroups(ctx) {
const data = Object.keys(strapi.groups).map(uid => ({
uid,
source: null,
isDisplayed: true,
name: uid,
label: _.upperFirst(pluralize(uid)),
}));
2019-07-19 18:14:13 +02:00
ctx.body = { data };
},
2019-07-19 09:58:38 +02:00
/**
* Returns a group configuration.
* It includes
* - schema
* - content-manager layouts (list,edit)
* - content-manager settings
* - content-manager metadata (placeholders, description, label...)
*/
2019-07-19 18:14:13 +02:00
async findGroup(ctx) {
const { uid } = ctx.params;
const group = strapi.groups[uid];
if (!group) {
return ctx.notFound('group.notFound');
}
const groupService = strapi.plugins['content-manager'].services.groups;
const configurations = await groupService.getConfiguration(uid);
const data = {
uid,
schema: groupService.formatGroupSchema(group),
...configurations,
};
ctx.body = { data };
},
2019-07-19 09:58:38 +02:00
/**
* Updates a group configuration
* You can only update the content-manager settings: (use the content-type-builder to update attributes)
* - content-manager layouts (list,edit)
* - content-manager settings
* - content-manager metadata (placeholders, description, label...)
*/
2019-07-19 18:14:13 +02:00
async updateGroup(ctx) {
const { uid } = ctx.params;
const { body } = ctx.request;
const group = strapi.groups[uid];
if (!group) {
return ctx.notFound('group.notFound');
}
let input;
try {
input = await createModelConfigurationSchema(group).validate(body, {
abortEarly: false,
stripUnknown: true,
strict: true,
});
} catch (error) {
return ctx.badRequest(null, {
name: 'validationError',
errors: error.errors,
});
}
const groupService = strapi.plugins['content-manager'].services.groups;
await groupService.setConfiguration(uid, input);
const configurations = await groupService.getConfiguration(uid);
const data = {
uid,
schema: groupService.formatGroupSchema(group),
...configurations,
};
ctx.body = { data };
},
2019-07-19 09:58:38 +02:00
};