111 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict';
2021-02-10 14:29:54 +01:00
const { upperFirst, has, prop, pick, getOr } = require('lodash/fp');
const pluralize = require('pluralize');
2021-04-29 13:51:12 +02:00
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
const { isMediaAttribute } = require('@strapi/utils').contentTypes;
const dtoFields = [
'uid',
'isDisplayed',
'apiID',
'kind',
'category',
'info',
'options',
'pluginOptions',
'attributes',
'pluginOptions',
];
module.exports = {
toContentManagerModel(contentType) {
return {
...contentType,
2021-06-30 22:52:12 +02:00
options: {
...contentType.options,
timestamps: [],
},
apiID: contentType.modelName,
2021-02-10 14:29:54 +01:00
isDisplayed: isVisible(contentType),
info: {
...contentType.info,
label: formatContentTypeLabel(contentType),
},
attributes: {
id: {
2021-06-30 22:52:12 +02:00
type: 'integer',
},
...formatAttributes(contentType),
},
};
},
toDto: pick(dtoFields),
};
const formatContentTypeLabel = contentType => {
const name = prop('info.name', contentType) || contentType.modelName;
try {
return contentTypesUtils.isSingleType(contentType)
? upperFirst(name)
: upperFirst(pluralize(name));
} catch (error) {
// in case pluralize throws cyrillic characters
return upperFirst(name);
}
};
const formatAttributes = model => {
const { getVisibleAttributes } = contentTypesUtils;
// only get attributes that can be seen in the auto generated Edit view or List view
return getVisibleAttributes(model).reduce((acc, key) => {
acc[key] = formatAttribute(key, model.attributes[key], { model });
return acc;
}, {});
};
2021-06-22 17:13:11 +02:00
// FIXME: not needed
const formatAttribute = (key, attribute, { model }) => {
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
// if (has('type', attribute)) return attribute;
// if (isMediaAttribute(attribute)) {
// return toMedia(attribute);
// }
// const relation = (model.associations || []).find(assoc => assoc.alias === key);
// return toRelation(attribute, relation);
};
2021-06-22 17:13:11 +02:00
// FIXME: not needed
const toMedia = attribute => {
return {
type: 'media',
multiple: attribute.collection ? true : false,
required: attribute.required ? true : false,
allowedTypes: attribute.allowedTypes,
pluginOptions: attribute.pluginOptions,
};
};
2021-06-22 17:13:11 +02:00
// FIXME: not needed
2021-06-30 22:52:12 +02:00
const toRelation = attribute => {
return {
...attribute,
type: 'relation',
2021-06-30 22:52:12 +02:00
targetModel: attribute.target,
relationType: attribute.relation,
pluginOptions: attribute.pluginOptions,
};
};
2021-02-10 14:29:54 +01:00
const isVisible = model => getOr(true, 'pluginOptions.content-manager.visible', model) === true;