2018-04-02 17:05:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const knex = require('../lib/knex');
|
|
|
|
const hasher = require('node-object-hash')();
|
|
|
|
const { enforce, filterObject } = require('../lib/helpers');
|
|
|
|
const dtHelpers = require('../lib/dt-helpers');
|
2018-11-18 14:38:52 +00:00
|
|
|
const interoperableErrors = require('../../shared/interoperable-errors');
|
2018-04-02 17:05:22 +00:00
|
|
|
const namespaceHelpers = require('../lib/namespace-helpers');
|
|
|
|
const shares = require('./shares');
|
2018-09-29 18:08:49 +00:00
|
|
|
const files = require('./files');
|
|
|
|
const dependencyHelpers = require('../lib/dependency-helpers');
|
2019-07-03 09:58:58 +00:00
|
|
|
const { allTagLanguages } = require('../../shared/templates');
|
2018-04-02 17:05:22 +00:00
|
|
|
|
2019-07-03 09:58:58 +00:00
|
|
|
const allowedKeys = new Set(['name', 'description', 'type', 'tag_language', 'data', 'namespace']);
|
2018-04-02 17:05:22 +00:00
|
|
|
|
|
|
|
function hash(entity) {
|
|
|
|
return hasher.hash(filterObject(entity, allowedKeys));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getById(context, id) {
|
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'mosaicoTemplate', id, 'view');
|
|
|
|
const entity = await tx('mosaico_templates').where('id', id).first();
|
|
|
|
entity.data = JSON.parse(entity.data);
|
|
|
|
entity.permissions = await shares.getPermissionsTx(tx, context, 'mosaicoTemplate', id);
|
|
|
|
return entity;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listDTAjax(context, params) {
|
|
|
|
return await dtHelpers.ajaxListWithPermissions(
|
|
|
|
context,
|
|
|
|
[{ entityTypeId: 'mosaicoTemplate', requiredOperations: ['view'] }],
|
|
|
|
params,
|
|
|
|
builder => builder.from('mosaico_templates').innerJoin('namespaces', 'namespaces.id', 'mosaico_templates.namespace'),
|
2019-07-03 09:58:58 +00:00
|
|
|
[ 'mosaico_templates.id', 'mosaico_templates.name', 'mosaico_templates.description', 'mosaico_templates.type', 'mosaico_templates.tag_language', 'mosaico_templates.created', 'namespaces.name' ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listByTagLanguageDTAjax(context, tagLanguage, params) {
|
|
|
|
return await dtHelpers.ajaxListWithPermissions(
|
|
|
|
context,
|
|
|
|
[{ entityTypeId: 'mosaicoTemplate', requiredOperations: ['view'] }],
|
|
|
|
params,
|
|
|
|
builder => builder.from('mosaico_templates')
|
|
|
|
.innerJoin('namespaces', 'namespaces.id', 'mosaico_templates.namespace')
|
|
|
|
.where('mosaico_templates.tag_language', tagLanguage),
|
|
|
|
[ 'mosaico_templates.id', 'mosaico_templates.name', 'mosaico_templates.description', 'mosaico_templates.type', 'mosaico_templates.tag_language', 'mosaico_templates.created', 'namespaces.name' ]
|
2018-04-02 17:05:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _validateAndPreprocess(tx, entity) {
|
2019-07-03 09:58:58 +00:00
|
|
|
enforce(allTagLanguages.includes(entity.tag_language), `Invalid tag language '${entity.tag_language}'`);
|
|
|
|
|
2018-04-02 17:05:22 +00:00
|
|
|
entity.data = JSON.stringify(entity.data);
|
2018-04-22 07:00:04 +00:00
|
|
|
await namespaceHelpers.validateEntity(tx, entity);
|
2018-04-02 17:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function create(context, entity) {
|
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'namespace', entity.namespace, 'createMosaicoTemplate');
|
|
|
|
|
|
|
|
await _validateAndPreprocess(tx, entity);
|
|
|
|
|
|
|
|
const ids = await tx('mosaico_templates').insert(filterObject(entity, allowedKeys));
|
|
|
|
const id = ids[0];
|
|
|
|
|
|
|
|
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'mosaicoTemplate', entityId: id });
|
|
|
|
|
|
|
|
return id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateWithConsistencyCheck(context, entity) {
|
|
|
|
await knex.transaction(async tx => {
|
|
|
|
await shares.enforceGlobalPermission(context, 'createJavascriptWithROAccess');
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'mosaicoTemplate', entity.id, 'edit');
|
|
|
|
|
|
|
|
const existing = await tx('mosaico_templates').where('id', entity.id).first();
|
|
|
|
if (!existing) {
|
|
|
|
throw new interoperableErrors.NotFoundError();
|
|
|
|
}
|
|
|
|
|
|
|
|
existing.data = JSON.parse(existing.data);
|
|
|
|
|
|
|
|
const existingHash = hash(existing);
|
|
|
|
if (existingHash !== entity.originalHash) {
|
|
|
|
throw new interoperableErrors.ChangedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
await _validateAndPreprocess(tx, entity);
|
|
|
|
|
|
|
|
await namespaceHelpers.validateMove(context, entity, existing, 'mosaicoTemplate', 'createMosaicoTemplate', 'delete');
|
|
|
|
|
|
|
|
await tx('mosaico_templates').where('id', entity.id).update(filterObject(entity, allowedKeys));
|
|
|
|
|
|
|
|
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'mosaicoTemplate', entityId: entity.id });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(context, id) {
|
|
|
|
await knex.transaction(async tx => {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'mosaicoTemplate', id, 'delete');
|
|
|
|
|
2018-09-29 18:08:49 +00:00
|
|
|
await dependencyHelpers.ensureNoDependencies(tx, context, id, [
|
|
|
|
{
|
|
|
|
entityTypeId: 'template',
|
|
|
|
rows: async (tx, limit) => {
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
const tmpls = await tx('templates').where('type', 'mosaico').select(['id', 'name', 'data']);
|
|
|
|
for (const tmpl of tmpls) {
|
|
|
|
const data = JSON.parse(tmpl.data);
|
|
|
|
if (data.mosaicoTemplate === id) {
|
|
|
|
result.push(tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
limit -= 1;
|
|
|
|
if (limit <= 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
await files.removeAllTx(tx, context, 'mosaicoTemplate', 'file', id);
|
|
|
|
await files.removeAllTx(tx, context, 'mosaicoTemplate', 'block', id);
|
|
|
|
|
2018-04-02 17:05:22 +00:00
|
|
|
await tx('mosaico_templates').where('id', id).del();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-09 22:55:44 +00:00
|
|
|
module.exports.hash = hash;
|
|
|
|
module.exports.getById = getById;
|
|
|
|
module.exports.listDTAjax = listDTAjax;
|
2019-07-03 09:58:58 +00:00
|
|
|
module.exports.listByTagLanguageDTAjax = listByTagLanguageDTAjax;
|
2018-09-09 22:55:44 +00:00
|
|
|
module.exports.create = create;
|
|
|
|
module.exports.updateWithConsistencyCheck = updateWithConsistencyCheck;
|
|
|
|
module.exports.remove = remove;
|