2019-08-05 16:39:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-10-27 11:27:17 +01:00
|
|
|
const { createModelConfigurationSchema } = require('../../../controllers/validation');
|
2019-07-23 10:54:44 +02:00
|
|
|
const { createDefaultSettings, syncSettings } = require('./settings');
|
|
|
|
const { createDefaultMetadatas, syncMetadatas } = require('./metadatas');
|
|
|
|
const { createDefaultLayouts, syncLayouts } = require('./layouts');
|
2019-08-06 12:02:13 +02:00
|
|
|
|
2020-10-22 16:43:51 +02:00
|
|
|
async function validateCustomConfig(schema) {
|
2019-08-06 12:02:13 +02:00
|
|
|
try {
|
2020-10-22 16:43:51 +02:00
|
|
|
await createModelConfigurationSchema(schema, {
|
2019-08-06 12:02:13 +02:00
|
|
|
allowUndefined: true,
|
2020-10-22 16:43:51 +02:00
|
|
|
}).validate(schema.config);
|
2019-08-06 12:02:13 +02:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(
|
2020-10-22 16:43:51 +02:00
|
|
|
`Invalid Model configuration for model ${schema.uid}. Verify your {{modelName}}.config.js(on) file:\n - ${error.message}\n`
|
2019-08-06 12:02:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 10:54:44 +02:00
|
|
|
|
2020-10-22 16:43:51 +02:00
|
|
|
async function createDefaultConfiguration(schema) {
|
|
|
|
await validateCustomConfig(schema);
|
2019-07-24 11:51:35 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
return {
|
2019-07-30 16:12:14 +02:00
|
|
|
settings: await createDefaultSettings(schema),
|
2019-07-24 11:51:35 +02:00
|
|
|
metadatas: await createDefaultMetadatas(schema),
|
|
|
|
layouts: await createDefaultLayouts(schema),
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:43:51 +02:00
|
|
|
async function syncConfiguration(conf, schema) {
|
|
|
|
await validateCustomConfig(schema);
|
2019-07-24 11:51:35 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
return {
|
2019-07-24 11:51:35 +02:00
|
|
|
settings: await syncSettings(conf, schema),
|
|
|
|
layouts: await syncLayouts(conf, schema),
|
|
|
|
metadatas: await syncMetadatas(conf, schema),
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createDefaultConfiguration,
|
|
|
|
syncConfiguration,
|
|
|
|
};
|