2018-08-03 16:07:46 +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-08-03 16:07:46 +00:00
|
|
|
const shares = require('./shares');
|
2018-11-18 14:38:52 +00:00
|
|
|
const {EntityVals, EventVals, Entity} = require('../../shared/triggers');
|
2018-08-03 16:07:46 +00:00
|
|
|
const campaigns = require('./campaigns');
|
|
|
|
|
2018-09-23 20:28:58 +00:00
|
|
|
const allowedKeys = new Set(['name', 'description', 'entity', 'event', 'seconds', 'enabled', 'source_campaign']);
|
2018-08-03 16:07:46 +00:00
|
|
|
|
|
|
|
function hash(entity) {
|
|
|
|
return hasher.hash(filterObject(entity, allowedKeys));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getById(context, campaignId, id) {
|
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', campaignId, 'viewTriggers');
|
|
|
|
|
|
|
|
const entity = await tx('triggers').where({campaign: campaignId, id}).first();
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listByCampaignDTAjax(context, campaignId, params) {
|
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', campaignId, 'viewTriggers');
|
|
|
|
|
|
|
|
return await dtHelpers.ajaxListTx(
|
|
|
|
tx,
|
|
|
|
params,
|
|
|
|
builder => builder
|
|
|
|
.from('triggers')
|
2018-08-04 09:30:37 +00:00
|
|
|
.innerJoin('campaigns', 'campaigns.id', 'triggers.campaign')
|
2018-08-03 16:07:46 +00:00
|
|
|
.where('triggers.campaign', campaignId),
|
2018-09-23 20:28:58 +00:00
|
|
|
[ 'triggers.id', 'triggers.name', 'triggers.description', 'triggers.entity', 'triggers.event', 'triggers.seconds', 'triggers.enabled' ]
|
2018-08-03 16:07:46 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listByListDTAjax(context, listId, params) {
|
|
|
|
return await dtHelpers.ajaxListWithPermissions(
|
|
|
|
context,
|
2018-08-04 09:30:37 +00:00
|
|
|
[{ entityTypeId: 'campaign', requiredOperations: ['viewTriggers'] }],
|
2018-08-03 16:07:46 +00:00
|
|
|
params,
|
|
|
|
builder => builder
|
|
|
|
.from('triggers')
|
2018-08-04 09:30:37 +00:00
|
|
|
.innerJoin('campaigns', 'campaigns.id', 'triggers.campaign')
|
2018-09-02 18:17:42 +00:00
|
|
|
.innerJoin('campaign_lists', 'campaign_lists.campaign', 'campaigns.id')
|
|
|
|
.where('campaign_lists.list', listId),
|
2018-09-23 20:28:58 +00:00
|
|
|
[ 'triggers.id', 'triggers.name', 'triggers.description', 'campaigns.name', 'triggers.entity', 'triggers.event', 'triggers.seconds', 'triggers.enabled', 'triggers.campaign' ]
|
2018-08-03 16:07:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _validateAndPreprocess(tx, context, campaignId, entity) {
|
2018-09-23 20:28:58 +00:00
|
|
|
enforce(Number.isInteger(entity.seconds));
|
|
|
|
enforce(entity.seconds >= 0, 'Seconds must not be negative');
|
2018-08-03 16:07:46 +00:00
|
|
|
enforce(entity.entity in EntityVals, 'Invalid entity');
|
2018-08-04 09:30:37 +00:00
|
|
|
enforce(entity.event in EventVals[entity.entity], 'Invalid event');
|
2018-08-03 16:07:46 +00:00
|
|
|
|
|
|
|
if (entity.entity === Entity.CAMPAIGN) {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', entity.source_campaign, 'view');
|
|
|
|
}
|
|
|
|
|
|
|
|
await campaigns.enforceSendPermissionTx(tx, context, campaignId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function create(context, campaignId, entity) {
|
|
|
|
return await knex.transaction(async tx => {
|
2018-08-05 04:47:05 +00:00
|
|
|
shares.enforceGlobalPermission(context, 'setupAutomation');
|
2018-08-03 16:07:46 +00:00
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', campaignId, 'manageTriggers');
|
|
|
|
|
|
|
|
await _validateAndPreprocess(tx, context, campaignId, entity);
|
|
|
|
|
|
|
|
const filteredEntity = filterObject(entity, allowedKeys);
|
|
|
|
filteredEntity.campaign = campaignId;
|
2018-11-20 22:41:10 +00:00
|
|
|
filteredEntity.last_check = new Date(); // This is to prevent processing subscriptions that predate this trigger.
|
2018-08-03 16:07:46 +00:00
|
|
|
|
|
|
|
const ids = await tx('triggers').insert(filteredEntity);
|
|
|
|
const id = ids[0];
|
|
|
|
|
|
|
|
return id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateWithConsistencyCheck(context, campaignId, entity) {
|
|
|
|
await knex.transaction(async tx => {
|
2018-08-05 04:47:05 +00:00
|
|
|
shares.enforceGlobalPermission(context, 'setupAutomation');
|
2018-08-03 16:07:46 +00:00
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', campaignId, 'manageTriggers');
|
|
|
|
|
2018-08-04 09:30:37 +00:00
|
|
|
const existing = await tx('triggers').where({campaign: campaignId, id: entity.id}).first();
|
2018-08-03 16:07:46 +00:00
|
|
|
if (!existing) {
|
|
|
|
throw new interoperableErrors.NotFoundError();
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingHash = hash(existing);
|
|
|
|
if (existingHash !== entity.originalHash) {
|
|
|
|
throw new interoperableErrors.ChangedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
await _validateAndPreprocess(tx, context, campaignId, entity);
|
|
|
|
|
|
|
|
await tx('triggers').where({campaign: campaignId, id: entity.id}).update(filterObject(entity, allowedKeys));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeTx(tx, context, campaignId, id) {
|
|
|
|
await shares.enforceEntityPermissionTx(tx, context, 'campaign', campaignId, 'manageTriggers');
|
|
|
|
|
2018-09-22 13:59:05 +00:00
|
|
|
const existing = await tx('triggers').where({campaign: campaignId, id}).first();
|
|
|
|
if (!existing) {
|
|
|
|
throw new interoperableErrors.NotFoundError();
|
|
|
|
}
|
2018-08-03 16:07:46 +00:00
|
|
|
|
2018-09-22 13:59:05 +00:00
|
|
|
await tx('trigger_messages').where({trigger: id}).del();
|
|
|
|
await tx('triggers').where('id', id).del();
|
2018-08-03 16:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(context, campaignId, id) {
|
|
|
|
await knex.transaction(async tx => {
|
|
|
|
await removeTx(tx, context, campaignId, id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAllByCampaignIdTx(tx, context, campaignId) {
|
2018-09-02 18:17:42 +00:00
|
|
|
const entities = await tx('triggers').where('campaign', campaignId).select(['id']);
|
2018-08-03 16:07:46 +00:00
|
|
|
for (const entity of entities) {
|
|
|
|
await removeTx(tx, context, campaignId, entity.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is to handle circular dependency with campaigns.js
|
2018-09-09 22:55:44 +00:00
|
|
|
module.exports.hash = hash;
|
|
|
|
module.exports.getById = getById;
|
|
|
|
module.exports.listByCampaignDTAjax = listByCampaignDTAjax;
|
|
|
|
module.exports.listByListDTAjax = listByListDTAjax;
|
|
|
|
module.exports.create = create;
|
|
|
|
module.exports.updateWithConsistencyCheck = updateWithConsistencyCheck;
|
|
|
|
module.exports.removeTx = removeTx;
|
|
|
|
module.exports.remove = remove;
|
|
|
|
module.exports.removeAllByCampaignIdTx = removeAllByCampaignIdTx;
|