36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
|
import { join } from 'lodash';
|
||
|
import { permission } from '../authApi/permissions';
|
||
|
import { appDefinitionFile } from '../common';
|
||
|
import { validateAll } from './validate';
|
||
|
import { apiWrapper } from '../common/apiWrapper';
|
||
|
import { events } from '../common/events';
|
||
|
|
||
|
export const saveApplicationHierarchy = app => async hierarchy => apiWrapper(
|
||
|
app,
|
||
|
events.templateApi.saveApplicationHierarchy,
|
||
|
permission.writeTemplates.isAuthorized,
|
||
|
{ hierarchy },
|
||
|
_saveApplicationHierarchy, app.datastore, hierarchy,
|
||
|
);
|
||
|
|
||
|
|
||
|
export const _saveApplicationHierarchy = async (datastore, hierarchy) => {
|
||
|
const validationErrors = await validateAll(hierarchy);
|
||
|
if (validationErrors.length > 0) {
|
||
|
throw new Error(`Hierarchy is invalid: ${join(
|
||
|
validationErrors.map(e => `${e.item.nodeKey ? e.item.nodeKey() : ''} : ${e.error}`),
|
||
|
',',
|
||
|
)}`);
|
||
|
}
|
||
|
|
||
|
if (await datastore.exists(appDefinitionFile)) {
|
||
|
const appDefinition = await datastore.loadJson(appDefinitionFile);
|
||
|
appDefinition.hierarchy = hierarchy;
|
||
|
await datastore.updateJson(appDefinitionFile, appDefinition);
|
||
|
} else {
|
||
|
await datastore.createFolder('/.config');
|
||
|
const appDefinition = { actions: [], triggers: [], hierarchy };
|
||
|
await datastore.createJson(appDefinitionFile, appDefinition);
|
||
|
}
|
||
|
};
|