143 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const { getService } = require('../../../utils');
const {
isSortable,
isSearchable,
isVisible,
isRelation,
getDefaultMainField,
} = require('./attributes');
2019-07-24 11:51:35 +02:00
function createDefaultMetadatas(schema) {
return {
2019-07-24 11:51:35 +02:00
...Object.keys(schema.attributes).reduce((acc, name) => {
acc[name] = createDefaultMetadata(schema, name);
return acc;
}, {}),
id: {
edit: {},
list: {
label: 'Id',
searchable: true,
sortable: true,
},
},
};
}
2019-07-24 11:51:35 +02:00
function createDefaultMetadata(schema, name) {
const edit = {
2019-07-24 11:51:35 +02:00
label: _.upperFirst(name),
description: '',
placeholder: '',
2019-07-24 11:51:35 +02:00
visible: isVisible(schema, name),
editable: true,
};
2019-07-24 11:51:35 +02:00
if (isRelation(schema.attributes[name])) {
const { targetModel } = schema.attributes[name];
const targetSchema = getTargetSchema(targetModel);
if (targetSchema) {
edit.mainField = getDefaultMainField(targetSchema);
}
}
2019-08-06 14:04:16 +02:00
_.assign(
edit,
_.pick(_.get(schema, ['config', 'metadatas', name, 'edit'], {}), [
'label',
'description',
'placeholder',
'visible',
'editable',
'mainField',
])
);
const list = {
2019-07-24 11:51:35 +02:00
label: _.upperFirst(name),
searchable: isSearchable(schema, name),
sortable: isSortable(schema, name),
..._.pick(_.get(schema, ['config', 'metadatas', name, 'list'], {}), [
'label',
'searchable',
'sortable',
]),
};
return { edit, list };
}
/** Synchronisation functions */
2019-07-24 11:51:35 +02:00
async function syncMetadatas(configuration, schema) {
// clear all keys that do not exist anymore
if (_.isEmpty(configuration.metadatas)) {
return createDefaultMetadatas(schema);
}
// remove old keys
const metasWithValidKeys = _.pick(configuration.metadatas, Object.keys(schema.attributes));
// add new keys and missing fields
const metasWithDefaults = _.merge({}, createDefaultMetadatas(schema), metasWithValidKeys);
// clear the invalid mainFields
const updatedMetas = Object.keys(metasWithDefaults).reduce((acc, key) => {
2019-07-24 11:51:35 +02:00
const { edit, list } = metasWithDefaults[key];
const attr = schema.attributes[key];
let updatedMeta = { edit, list };
// update sortable attr
2019-07-24 11:51:35 +02:00
if (list.sortable && !isSortable(schema, key)) {
_.set(updatedMeta, ['list', 'sortable'], false);
_.set(acc, [key], updatedMeta);
}
2019-07-24 11:51:35 +02:00
if (list.searchable && !isSearchable(schema, key)) {
_.set(updatedMeta, ['list', 'searchable'], false);
_.set(acc, [key], updatedMeta);
}
if (!_.has(edit, 'mainField')) return acc;
// remove mainField if the attribute is not a relation anymore
2019-07-24 11:51:35 +02:00
if (!isRelation(attr)) {
_.set(updatedMeta, 'edit', _.omit(edit, ['mainField']));
_.set(acc, [key], updatedMeta);
return acc;
}
// if the mainField is id you can keep it
if (edit.mainField === 'id') return acc;
// check the mainField in the targetModel
const targetSchema = getTargetSchema(attr.targetModel);
2019-07-24 11:51:35 +02:00
if (!targetSchema) return acc;
2019-07-24 11:51:35 +02:00
if (!isSortable(targetSchema, edit.mainField)) {
_.set(updatedMeta, ['edit', 'mainField'], getDefaultMainField(targetSchema));
_.set(acc, [key], updatedMeta);
return acc;
}
return acc;
}, {});
return _.assign(metasWithDefaults, updatedMetas);
}
const getTargetSchema = targetModel => {
return getService('content-types').findContentType(targetModel);
2019-07-24 11:51:35 +02:00
};
module.exports = {
createDefaultMetadatas,
syncMetadatas,
};