Removed obsolete dir

Numeric conversions for all ids coming in as route req params.
Infrastructure for proper error message when dependencies prevent entity deletion.
This commit is contained in:
Tomas Bures 2018-09-29 13:30:29 +02:00
parent 2b57396a5d
commit 0a08088893
636 changed files with 291 additions and 73346 deletions

View file

@ -378,6 +378,13 @@ async function _createTx(tx, context, entity, content) {
await tx('campaign_lists').insert(entity.lists.map(x => ({campaign: id, ...x})));
if (entity.source === CampaignSource.TEMPLATE) {
await tx('template_dep_campaigns').insert({
campaign: id,
template: entity.data.sourceTemplate
});
}
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'campaign', entityId: id });
if (copyFilesFrom) {
@ -436,6 +443,12 @@ async function updateWithConsistencyCheck(context, entity, content) {
if (content === Content.ALL || content === Content.WITHOUT_SOURCE_CUSTOM) {
await tx('campaign_lists').where('campaign', entity.id).del();
await tx('campaign_lists').insert(entity.lists.map(x => ({campaign: entity.id, ...x})));
if (existing.source === CampaignSource.TEMPLATE) {
await tx('template_dep_campaigns')
.where('campaign', entity.id)
.update('template', entity.data.sourceTemplate);
}
}
filteredEntity.data = JSON.stringify(filteredEntity.data);
@ -458,6 +471,10 @@ async function remove(context, id) {
await triggers.removeAllByCampaignIdTx(tx, context, id);
await tx('template_dep_campaigns')
.where('campaign', entity.id)
.del();
await tx('campaigns').where('id', id).del();
});
}

View file

@ -305,6 +305,19 @@ async function copyAllTx(tx, context, fromType, fromSubType, fromEntityId, toTyp
}
}
async function removeAllTx(tx, context, type, subType, entityId) {
enforceTypePermitted(type, subType);
await shares.enforceEntityPermissionTx(tx, context, type, entityId, getFilesPermission(type, subType, 'manage'));
const rows = await tx(getFilesTable(type, subType)).where({entity: entityId});
for (const row of rows) {
const filePath = getFilePath(type, subType, entityId, row.filename);
await fs.removeAsync(filePath);
}
await tx(getFilesTable(type, subType)).where('entity', entityId).del();
}
module.exports.filesDir = filesDir;
module.exports.listDTAjax = listDTAjax;
@ -319,4 +332,5 @@ module.exports.removeFile = removeFile;
module.exports.getFileUrl = getFileUrl;
module.exports.getFilePath = getFilePath;
module.exports.copyAllTx = copyAllTx;
module.exports.removeAllTx = removeAllTx;
module.exports.ReplacementBehavior = ReplacementBehavior;

View file

@ -83,14 +83,21 @@ async function updateWithConsistencyCheck(context, entity) {
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 deps = [];
const tmpls = await tx('templates').where('type', 'mosaico').select(['id', 'name', 'data']);
for (const row of tmpls) {
const data = JSON.parse(row.data);
if (data.template === id) {
throw new interoperableErrors.DependencyPresentError();
if (data.mosaicoTemplate === id) {
deps.push({ entityTypeId: 'template', name: row.name, link: `templates/${row.id}` });
}
}
if (deps.length > 0) {
throw new interoperableErrors.DependencyPresentError('', {
dependencies: deps
});
}
await shares.enforceEntityPermissionTx(tx, context, 'mosaicoTemplate', id, 'delete');
await tx('mosaico_templates').where('id', id).del();

View file

@ -8,6 +8,7 @@ const interoperableErrors = require('../shared/interoperable-errors');
const namespaceHelpers = require('../lib/namespace-helpers');
const shares = require('./shares');
const reports = require('./reports');
const files = require('./files');
const allowedKeys = new Set(['name', 'description', 'type', 'data', 'html', 'text', 'namespace']);
@ -96,7 +97,19 @@ async function remove(context, id) {
await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'template', id, 'delete');
// FIXME - deal with deletion of dependent entities (files, etc.)
const depCampaigns = await tx('template_dep_campaigns')
.where('template_dep_campaigns.template', id)
.innerJoin('campaigns', 'template_dep_campaigns.campaign', 'campaigns.id')
.limit(interoperableErrors.defaultNoOfDependenciesReported)
.select(['campaigns.id', 'campaigns.name']);
if (depCampaigns.length > 0) {
throw new interoperableErrors.DependencyPresentError('', {
dependencies: depCampaigns.map(row => ({ entityTypeId: 'campaign', name: row.name, link: `campaigns/${row.id}` }))
});
}
await files.removeAllTx(tx, context, 'template', 'file', id);
await tx('templates').where('id', id).del();
});