Add groups schemas

This commit is contained in:
Alexandre Bodin 2019-07-19 18:14:13 +02:00
parent e7f384afc7
commit ffc0a2ebd0
8 changed files with 203 additions and 36 deletions

View File

@ -2,7 +2,7 @@
const {
generalSettingsSchema,
createContentTypeConfigurationSchema,
createModelConfigurationSchema,
} = require('./validation');
module.exports = {
@ -164,14 +164,11 @@ module.exports = {
let input;
try {
input = await createContentTypeConfigurationSchema(contentType).validate(
body,
{
abortEarly: false,
stripUnknown: true,
strict: true,
}
);
input = await createModelConfigurationSchema(contentType).validate(body, {
abortEarly: false,
stripUnknown: true,
strict: true,
});
} catch (error) {
return ctx.badRequest(null, {
name: 'validationError',

View File

@ -1,10 +1,15 @@
'use strict';
const { createModelConfigurationSchema } = require('./validation');
module.exports = {
/**
* Returns the list of available groups
*/
listGroups() {},
async listGroups(ctx) {
const data = Object.keys(strapi.groups).map(uid => ({ uid }));
ctx.body = { data };
},
/**
* Returns a group configuration.
* It includes
@ -13,7 +18,26 @@ module.exports = {
* - content-manager settings
* - content-manager metadata (placeholders, description, label...)
*/
findGroup() {},
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 };
},
/**
* Updates a group configuration
* You can only update the content-manager settings: (use the content-type-builder to update attributes)
@ -21,5 +45,42 @@ module.exports = {
* - content-manager settings
* - content-manager metadata (placeholders, description, label...)
*/
updateGroup() {},
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 };
},
};

View File

@ -1,4 +1,4 @@
module.exports = {
generalSettingsSchema: require('./general-setting'),
createContentTypeConfigurationSchema: require('./content-type-configuration'),
createModelConfigurationSchema: require('./model-configuration'),
};

View File

@ -5,25 +5,23 @@ const yup = require('yup');
/**
* Creates the validation schema for content-type configurations
*/
module.exports = contentType =>
module.exports = model =>
yup
.object()
.shape({
settings: createContentTypeSettingsSchema(contentType),
metadatas: createContentTypeMetadasSchema(contentType),
layouts: createContentTypeLayoutsSchema(contentType),
settings: createSettingsSchema(model),
metadatas: createMetadasSchema(model),
layouts: createLayoutsSchema(model),
})
.noUnknown();
// TODO: do sth to clean the keys configurable, private etc
const createContentTypeSettingsSchema = contentType => {
const validAttributes = Object.keys(contentType.allAttributes).filter(key => {
const createSettingsSchema = model => {
const validAttributes = Object.keys(model.allAttributes).filter(key => {
return (
contentType.allAttributes[key].type &&
!['json', 'password', 'group'].includes(
contentType.allAttributes[key].type
)
model.allAttributes[key].type &&
!['json', 'password', 'group'].includes(model.allAttributes[key].type)
);
});
const attrs = ['id'].concat(validAttributes);
@ -58,9 +56,9 @@ const createContentTypeSettingsSchema = contentType => {
.noUnknown();
};
const createContentTypeMetadasSchema = contentType => {
const createMetadasSchema = model => {
return yup.object().shape(
['id'].concat(Object.keys(contentType.allAttributes)).reduce((acc, key) => {
['id'].concat(Object.keys(model.allAttributes)).reduce((acc, key) => {
acc[key] = yup
.object()
.shape({
@ -98,19 +96,17 @@ const ARRAY_TEST = {
test: val => Array.isArray(val),
};
const createContentTypeLayoutsSchema = contentType => {
const validAttributes = Object.keys(contentType.allAttributes).filter(key => {
const createLayoutsSchema = model => {
const validAttributes = Object.keys(model.allAttributes).filter(key => {
return (
contentType.allAttributes[key].type &&
!['json', 'password', 'group'].includes(
contentType.allAttributes[key].type
)
model.allAttributes[key].type &&
!['json', 'password', 'group'].includes(model.allAttributes[key].type)
);
});
const attrs = ['id'].concat(validAttributes);
const relationAttributes = Array.isArray(contentType.associations)
? contentType.associations.map(assoc => assoc.alias)
const relationAttributes = Array.isArray(model.associations)
? model.associations.map(assoc => assoc.alias)
: [];
return yup.object().shape({
@ -123,7 +119,11 @@ const createContentTypeLayoutsSchema = contentType => {
.shape({
name: yup
.string()
.oneOf(Object.keys(contentType.attributes))
.oneOf(
Object.keys(model.allAttributes).filter(
key => model.allAttributes[key].type
)
)
.required(),
size: yup
.number()

View File

@ -41,7 +41,7 @@ module.exports = {
const metadatas = await getContentTypeMetadatas(storeKey);
return {
settings: settings,
settings,
metadatas,
layouts,
};

View File

@ -0,0 +1,98 @@
'use strict';
const keys = {
GROUP_SETTINGS: 'groups_settings',
GROUP_METADATAS: 'groups_metadatas',
GROUP_LAYOUTS: 'groups_layouts',
};
const getStore = () => {
return strapi.store({
environment: '',
type: 'plugin',
name: 'content_manager',
});
};
module.exports = {
async getConfiguration(uid) {
const settings = await getGroupSettings(uid);
const layouts = await getGroupLayouts(uid);
const metadatas = await getGroupMetadatas(uid);
return {
settings,
metadatas,
layouts,
};
},
async setConfiguration(uid, input) {
const { settings, metadatas, layouts } = input;
if (settings) await setGroupSettings(uid, settings);
if (layouts) await setGroupLayouts(uid, layouts);
if (metadatas) await setGroupMetadatas(uid, metadatas);
},
formatGroupSchema(group) {
const { associations, schema, allAttributes } = group;
return {
...schema,
attributes: Object.keys(allAttributes).reduce((acc, key) => {
const attr = allAttributes[key];
const assoc = associations.find(assoc => assoc.alias === key);
if (assoc) {
acc[key] = {
...attr,
type: 'relation',
targetModel: attr.model || attr.collection,
relationType: assoc.nature,
};
} else {
acc[key] = attr;
}
return acc;
}, {}),
};
},
};
const getGroupSettings = async key => {
const value = await getStore().get({
key: `${keys.GROUP_SETTINGS}_${key}`,
});
return value || {};
};
const getGroupLayouts = async key => {
const value = await getStore().get({
key: `${keys.GROUP_LAYOUTS}_${key}`,
});
return value || {};
};
const getGroupMetadatas = async key => {
const value = await getStore().get({
key: `${keys.GROUP_METADATAS}_${key}`,
});
return value || {};
};
const setGroupSettings = (key, value) =>
getStore().set({
key: `${keys.GROUP_SETTINGS}_${key}`,
value,
});
const setGroupLayouts = (key, value) =>
getStore().set({
key: `${keys.GROUP_LAYOUTS}_${key}`,
value,
});
const setGroupMetadatas = (key, value) =>
getStore().set({
key: `${keys.GROUP_METADATAS}_${key}`,
value,
});

View File

@ -51,6 +51,7 @@ module.exports = function(strapi) {
throw new Error(`Group ${key} is missing a collectionName attribute`);
return Object.assign(group, {
schema: _.clone(group),
globalId: group.globalId || _.upperFirst(_.camelCase(`group_${key}`)),
});
});

View File

@ -1,3 +1,7 @@
'use strict';
const _ = require('lodash');
const initMap = groups => {
const map = new Map();
@ -12,7 +16,13 @@ const initMap = groups => {
map.set(key, {
uid: key,
schema: { name, connection, collectionName, description, attributes },
schema: {
name: _.upperFirst(name || key),
connection,
collectionName,
description,
attributes,
},
});
});