Add helper to format content type data

This commit is contained in:
soupette 2019-12-03 20:06:34 +01:00
parent 4d3a0f98e5
commit 7fb450ebfd
4 changed files with 69 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import { get, isEqual, omit } from 'lodash';
import { get, has, isEqual, omit } from 'lodash';
import makeUnique from '../../../utils/makeUnique';
const getCreatedAndModifiedComponents = (allComponents, initialComponents) => {
@ -41,6 +41,29 @@ const formatComponent = (component, mainDataUID, isCreatingData = false) => {
return formattedComponent;
};
const formatContentType = data => {
const isCreatingData = get(data, 'isTemporary', true);
const mainDataUID = get(data, 'uid', null);
const formattedAttributes = formatAttributes(
get(data, 'schema.attributes', {}),
mainDataUID,
isCreatingData,
false
);
const formattedContentType = Object.assign(
{},
omit(data.schema, 'attributes'),
{ attributes: formattedAttributes }
);
delete formattedContentType.uid;
delete formattedContentType.isTemporary;
return formattedContentType;
};
/**
*
* @param {Object} attributes
@ -57,18 +80,28 @@ const formatAttributes = (
return Object.keys(attributes).reduce((acc, current) => {
const currentAttribute = get(attributes, current, {});
const hasARelationWithMainDataUID = currentAttribute.target === mainDataUID;
const isRelationType = has(currentAttribute, 'nature');
const currentTargetAttribute = get(
currentAttribute,
'targetAttribute',
null
);
if (!hasARelationWithMainDataUID) {
acc[current] = currentAttribute;
if (isRelationType) {
const relationAttr = Object.assign({}, currentAttribute, {
targetAttribute: formatRelationTargetAttribute(
currentTargetAttribute
),
});
acc[current] = relationAttr;
} else {
acc[current] = currentAttribute;
}
}
if (hasARelationWithMainDataUID) {
const currentTargetAttribute = get(
currentAttribute,
'targetAttribute',
null
);
let target = currentTargetAttribute.target;
if (isCreatingMainData) {
@ -77,8 +110,7 @@ const formatAttributes = (
const formattedRelationAttribute = Object.assign({}, currentAttribute, {
target,
targetAttribute:
currentTargetAttribute === '-' ? null : currentTargetAttribute,
targetAttribute: formatRelationTargetAttribute(currentTargetAttribute),
});
acc[current] = formattedRelationAttribute;
@ -88,6 +120,9 @@ const formatAttributes = (
}, {});
};
const formatRelationTargetAttribute = targetAttribute =>
targetAttribute === '-' ? null : targetAttribute;
const getComponentsToPost = (
allComponents,
initialComponents,
@ -116,4 +151,5 @@ export {
formatComponent,
getComponentsToPost,
getCreatedAndModifiedComponents,
formatContentType,
};

View File

@ -1,5 +1,6 @@
import {
formatComponent,
formatContentType,
getComponentsToPost,
getCreatedAndModifiedComponents,
} from '../cleanData';
@ -68,6 +69,17 @@ describe('CleanData utils', () => {
});
});
describe('FormatContentType', () => {
it('should format the content type correctly', () => {
const {
rawData: { contentType },
} = contentTypeData;
const expected = expectedData.contentType;
expect(formatContentType(contentType)).toEqual(expected);
});
});
describe('GetComponentsToPost', () => {
it('should return an array containing all the formattedComponents', () => {
const {

View File

@ -152,6 +152,9 @@ const data = {
isTemporary: true,
schema: {
name: 'test content type',
collectionName: 'test-content-types',
connection: 'default',
description: '',
attributes: {
name: {
type: 'string',

View File

@ -1,6 +1,9 @@
const expectedData = {
contentType: {
name: 'test content type',
collectionName: 'test-content-types',
connection: 'default',
description: '',
attributes: {
name: {
type: 'string',
@ -45,6 +48,11 @@ const expectedData = {
repeatable: true,
component: 'default.metas',
},
quote: {
type: 'component',
repeatable: false,
component: 'blog.quote',
},
},
},
componentsToFormat: [