76 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
const { pick, getOr } = require('lodash/fp');
2021-04-29 13:51:12 +02:00
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
const dtoFields = [
'uid',
'isDisplayed',
'apiID',
'kind',
'category',
'info',
'options',
'pluginOptions',
'attributes',
'pluginOptions',
];
2021-07-13 18:46:36 +02:00
module.exports = () => ({
toContentManagerModel(contentType) {
return {
...contentType,
apiID: contentType.modelName,
2021-02-10 14:29:54 +01:00
isDisplayed: isVisible(contentType),
attributes: {
id: {
2021-06-30 22:52:12 +02:00
type: 'integer',
},
...formatAttributes(contentType),
},
};
},
toDto: pick(dtoFields),
2021-07-13 18:46:36 +02:00
});
2022-08-08 23:33:39 +02:00
const formatAttributes = (contentType) => {
const { getVisibleAttributes, getTimestamps } = contentTypesUtils;
// only get attributes that can be seen in the auto generated Edit view or List view
return getVisibleAttributes(contentType)
.concat(getTimestamps(contentType))
.reduce((acc, key) => {
const attribute = contentType.attributes[key];
// ignore morph until they are handled in the front
if (attribute.type === 'relation' && attribute.relation.toLowerCase().includes('morph')) {
return acc;
}
acc[key] = formatAttribute(key, attribute);
return acc;
}, {});
};
2021-06-22 17:13:11 +02:00
// FIXME: not needed
const formatAttribute = (key, attribute) => {
2021-06-30 22:52:12 +02:00
if (attribute.type === 'relation') {
return toRelation(attribute);
}
2021-06-30 22:52:12 +02:00
return attribute;
};
2021-06-22 17:13:11 +02:00
// FIXME: not needed
2022-08-08 23:33:39 +02:00
const toRelation = (attribute) => {
return {
...attribute,
type: 'relation',
2021-06-30 22:52:12 +02:00
targetModel: attribute.target,
relationType: attribute.relation,
};
};
2022-08-08 23:33:39 +02:00
const isVisible = (model) => getOr(true, 'pluginOptions.content-manager.visible', model) === true;