Basic support for Mosaico templates.
TODO: - Allow choosing a mosaico template in a mosaico-based template - Integrate the custom mosaico templates with templates (endpoint for retrieving a mosaico template, replacement of URL_BASE and PLACEHOLDER tags - Implement support for MJML-based Mosaico templates - Implement support for MJML-based templates - Implement support for GrapeJS-based templates
This commit is contained in:
parent
7b5642e911
commit
6706d93bc1
21 changed files with 2192 additions and 26 deletions
|
@ -7,21 +7,26 @@ const shares = require('./shares');
|
|||
const fs = require('fs-extra-promise');
|
||||
const path = require('path');
|
||||
const interoperableErrors = require('../shared/interoperable-errors');
|
||||
const permissions = require('../lib/permissions');
|
||||
|
||||
const entityTypes = permissions.getEntityTypes();
|
||||
|
||||
const filesDir = path.join(__dirname, '..', 'files');
|
||||
|
||||
const permittedTypes = new Set(['template']);
|
||||
function enforceTypePermitted(type) {
|
||||
enforce(type in entityTypes && entityTypes[type].filesTable);
|
||||
}
|
||||
|
||||
function getFilePath(type, entityId, filename) {
|
||||
return path.join(path.join(filesDir, type, entityId.toString()), filename);
|
||||
}
|
||||
|
||||
function getFilesTable(type) {
|
||||
return 'files_' + type;
|
||||
return entityTypes[type].filesTable;
|
||||
}
|
||||
|
||||
async function listDTAjax(context, type, entityId, params) {
|
||||
enforce(permittedTypes.has(type));
|
||||
enforceTypePermitted(type);
|
||||
await shares.enforceEntityPermission(context, type, entityId, 'manageFiles');
|
||||
return await dtHelpers.ajaxList(
|
||||
params,
|
||||
|
@ -38,7 +43,7 @@ async function list(context, type, entityId) {
|
|||
}
|
||||
|
||||
async function getFileById(context, type, id) {
|
||||
enforce(permittedTypes.has(type));
|
||||
enforceTypePermitted(type);
|
||||
const file = await knex.transaction(async tx => {
|
||||
const file = await tx(getFilesTable(type)).where('id', id).first();
|
||||
await shares.enforceEntityPermissionTx(tx, context, type, file.entity, 'manageFiles');
|
||||
|
@ -57,7 +62,7 @@ async function getFileById(context, type, id) {
|
|||
}
|
||||
|
||||
async function getFileByFilename(context, type, entityId, name) {
|
||||
enforce(permittedTypes.has(type));
|
||||
enforceTypePermitted(type);
|
||||
const file = await knex.transaction(async tx => {
|
||||
await shares.enforceEntityPermissionTx(tx, context, type, entityId, 'view');
|
||||
const file = await tx(getFilesTable(type)).where({entity: entityId, filename: name}).first();
|
||||
|
@ -76,7 +81,7 @@ async function getFileByFilename(context, type, entityId, name) {
|
|||
}
|
||||
|
||||
async function createFiles(context, type, entityId, files, dontReplace = false) {
|
||||
enforce(permittedTypes.has(type));
|
||||
enforceTypePermitted(type);
|
||||
if (files.length == 0) {
|
||||
// No files uploaded
|
||||
return {uploaded: 0};
|
||||
|
|
110
models/mosaico-templates.js
Normal file
110
models/mosaico-templates.js
Normal file
|
@ -0,0 +1,110 @@
|
|||
'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');
|
||||
const interoperableErrors = require('../shared/interoperable-errors');
|
||||
const namespaceHelpers = require('../lib/namespace-helpers');
|
||||
const shares = require('./shares');
|
||||
|
||||
const allowedKeys = new Set(['name', 'description', 'type', 'data', 'namespace']);
|
||||
|
||||
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'),
|
||||
[ 'mosaico_templates.id', 'mosaico_templates.name', 'mosaico_templates.description', 'mosaico_templates.type', 'mosaico_templates.created', 'namespaces.name' ]
|
||||
);
|
||||
}
|
||||
|
||||
async function _validateAndPreprocess(tx, entity) {
|
||||
entity.data = JSON.stringify(entity.data);
|
||||
}
|
||||
|
||||
async function create(context, entity) {
|
||||
return await knex.transaction(async tx => {
|
||||
await shares.enforceEntityPermissionTx(tx, context, 'namespace', entity.namespace, 'createMosaicoTemplate');
|
||||
|
||||
await _validateAndPreprocess(tx, entity);
|
||||
|
||||
await namespaceHelpers.validateEntity(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.validateEntity(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 => {
|
||||
const rows = await tx('templates').where('type', 'mosaico').select(['data']);
|
||||
for (const row of rows) {
|
||||
const data = JSON.parse(row.data);
|
||||
if (data.template === id) {
|
||||
throw new interoperableErrors.DependencyPresentError();
|
||||
}
|
||||
}
|
||||
|
||||
await shares.enforceEntityPermissionTx(tx, context, 'mosaicoTemplate', id, 'delete');
|
||||
|
||||
await tx('mosaico_templates').where('id', id).del();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
hash,
|
||||
getById,
|
||||
listDTAjax,
|
||||
create,
|
||||
updateWithConsistencyCheck,
|
||||
remove
|
||||
};
|
|
@ -36,7 +36,7 @@ async function listDTAjax(context, params) {
|
|||
);
|
||||
}
|
||||
|
||||
async function _validateAndPreprocess(tx, entity, isCreate) {
|
||||
async function _validateAndPreprocess(tx, entity) {
|
||||
entity.data = JSON.stringify(entity.data);
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ async function create(context, entity) {
|
|||
return await knex.transaction(async tx => {
|
||||
await shares.enforceEntityPermissionTx(tx, context, 'namespace', entity.namespace, 'createTemplate');
|
||||
|
||||
await _validateAndPreprocess(tx, entity, true);
|
||||
await _validateAndPreprocess(tx, entity);
|
||||
|
||||
await namespaceHelpers.validateEntity(tx, entity);
|
||||
|
||||
|
@ -73,7 +73,7 @@ async function updateWithConsistencyCheck(context, entity) {
|
|||
throw new interoperableErrors.ChangedError();
|
||||
}
|
||||
|
||||
await _validateAndPreprocess(tx, entity, false);
|
||||
await _validateAndPreprocess(tx, entity);
|
||||
|
||||
await namespaceHelpers.validateEntity(tx, entity);
|
||||
await namespaceHelpers.validateMove(context, entity, existing, 'template', 'createTemplate', 'delete');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue