From 189638364cf88b68ab69e4cab855de36886274e8 Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Sun, 22 Jul 2018 00:01:28 +0530 Subject: [PATCH] Added migration for campaigns --- client/src/send-configurations/CUD.js | 3 + client/src/templates/mosaico/CUD.js | 4 +- models/send-configurations.js | 2 +- ...20180401120444_create_mosaico_templates.js | 4 +- .../20180414120444_transform_settings.js | 4 + .../20180718220444_upgrade_campaigns.js | 160 ++++++++++++++++++ shared/mosaico-templates.js | 6 +- 7 files changed, 177 insertions(+), 6 deletions(-) create mode 100644 setup/knex/migrations/20180718220444_upgrade_campaigns.js diff --git a/client/src/send-configurations/CUD.js b/client/src/send-configurations/CUD.js index 59d74422..eee11890 100644 --- a/client/src/send-configurations/CUD.js +++ b/client/src/send-configurations/CUD.js @@ -87,6 +87,7 @@ export default class CUD extends Component { namespace: mailtrainConfig.user.namespace, from_email_overridable: false, from_name_overridable: false, + reply_to_overridable: false, subject_overridable: false, verpEnabled: false, verp_hostname: '', @@ -195,6 +196,8 @@ export default class CUD extends Component { + + diff --git a/client/src/templates/mosaico/CUD.js b/client/src/templates/mosaico/CUD.js index 2367df45..79749348 100644 --- a/client/src/templates/mosaico/CUD.js +++ b/client/src/templates/mosaico/CUD.js @@ -26,7 +26,7 @@ import { } from '../../lib/namespace'; import {DeleteModalDialog} from "../../lib/modals"; -import {versafix} from "../../../../shared/mosaico-templates"; +import {getVersafix} from "../../../../shared/mosaico-templates"; import { getTemplateTypes, getTemplateTypesOrder @@ -77,7 +77,7 @@ export default class CUD extends Component { description: '', namespace: mailtrainConfig.user.namespace, type: 'html', - html: versafix + html: getVersafix() }); } else { diff --git a/models/send-configurations.js b/models/send-configurations.js index ce5d5263..bc22bc36 100644 --- a/models/send-configurations.js +++ b/models/send-configurations.js @@ -10,7 +10,7 @@ const namespaceHelpers = require('../lib/namespace-helpers'); const {MailerType, getSystemSendConfigurationId} = require('../shared/send-configurations'); const contextHelpers = require('../lib/context-helpers'); -const allowedKeys = new Set(['name', 'description', 'from_email', 'from_email_overridable', 'from_name', 'from_name_overridable', 'subject', 'subject_overridable', 'verp_hostname', 'mailer_type', 'mailer_settings', 'namespace']); +const allowedKeys = new Set(['name', 'description', 'from_email', 'from_email_overridable', 'from_name', 'from_name_overridable', 'reply_to', 'reply_to_overridable', 'subject', 'subject_overridable', 'verp_hostname', 'mailer_type', 'mailer_settings', 'namespace']); const allowedMailerTypes = new Set(Object.values(MailerType)); diff --git a/setup/knex/migrations/20180401120444_create_mosaico_templates.js b/setup/knex/migrations/20180401120444_create_mosaico_templates.js index de8bf347..b3fe5b4d 100644 --- a/setup/knex/migrations/20180401120444_create_mosaico_templates.js +++ b/setup/knex/migrations/20180401120444_create_mosaico_templates.js @@ -35,7 +35,7 @@ exports.up = (knex, Promise) => (async() => { table.string('encoding'); table.integer('size'); table.timestamp('created').defaultTo(knex.fn.now()); - table.index(['entity', 'originalname']) + table.index(['entity', 'originalname']); }); const versafix = { @@ -44,7 +44,7 @@ exports.up = (knex, Promise) => (async() => { type: 'html', namespace: 1, data: JSON.stringify({ - html: mosaicoTemplates.versafix + html: mosaicoTemplates.getVersafix() }) }; diff --git a/setup/knex/migrations/20180414120444_transform_settings.js b/setup/knex/migrations/20180414120444_transform_settings.js index 31836b74..30f8a1e7 100644 --- a/setup/knex/migrations/20180414120444_transform_settings.js +++ b/setup/knex/migrations/20180414120444_transform_settings.js @@ -10,6 +10,8 @@ exports.up = (knex, Promise) => (async() => { table.boolean('from_email_overridable').defaultTo(false); table.string('from_name'); table.boolean('from_name_overridable').defaultTo(false); + table.string('reply_to'); + table.boolean('reply_to_overridable').defaultTo(false); table.string('subject'); table.boolean('subject_overridable').defaultTo(false); table.string('verp_hostname'); // VERP is not used if verp_hostname is null @@ -94,6 +96,8 @@ exports.up = (knex, Promise) => (async() => { from_email_overridable: true, from_name: settings.defaultFrom, from_name_overridable: true, + reply_to: settings.defaultAddress, + reply_to_overridable: true, subject: settings.defaultSubject, subject_overridable: true, verp_hostname: settings.verpUse ? settings.verpHostname : null, diff --git a/setup/knex/migrations/20180718220444_upgrade_campaigns.js b/setup/knex/migrations/20180718220444_upgrade_campaigns.js new file mode 100644 index 00000000..81887936 --- /dev/null +++ b/setup/knex/migrations/20180718220444_upgrade_campaigns.js @@ -0,0 +1,160 @@ +/* +This is how we refactor the original campaigns table. + + +-------------------------+---------------------+------+-----+-------------------+----------------+ + | Field | Type | Null | Key | Default | Extra | + +-------------------------+---------------------+------+-----+-------------------+----------------+ +OK | id | int(10) unsigned | NO | PRI | NULL | auto_increment | +OK | cid | varchar(255) | NO | UNI | NULL | | +OK | type | tinyint(4) unsigned | NO | MUL | 1 | | +OK | parent | int(10) unsigned | YES | MUL | NULL | | +OK | name | varchar(255) | NO | MUL | | | +OK | description | text | YES | | NULL | | +OK | list | int(10) unsigned | NO | | NULL | | +OK | segment | int(10) unsigned | YES | | NULL | | +X | template | int(10) unsigned | NO | | NULL | | +X | source_url | varchar(255) | YES | | NULL | | +X | editor_name | varchar(50) | YES | | | | +X | editor_data | longtext | YES | | NULL | | +OK | last_check | timestamp | YES | MUL | NULL | | +X | check_status | varchar(255) | YES | | NULL | | +OK | from -> from_name_override | varchar(255) | YES | | | | +OK | address -> from_email_override | varchar(255) | YES | | | | +OK | reply_to -> reply_to_override | varchar(255) | YES | | | | +OK | subject -> subject_override | varchar(255) | YES | | | | +X | html | longtext | YES | | NULL | | +X | html_prepared | longtext | YES | | NULL | | +X | text | longtext | YES | | NULL | | +OK | status | tinyint(4) unsigned | NO | MUL | 1 | | +OK | scheduled | timestamp | YES | MUL | NULL | | +X | status_change | timestamp | YES | | NULL | | +OK | delivered | int(11) unsigned | NO | | 0 | | +OK | blacklisted | int(11) unsigned | NO | | 0 | | +OK | opened | int(11) unsigned | NO | | 0 | | +OK | clicks | int(11) unsigned | NO | | 0 | | +OK | unsubscribed | int(11) unsigned | NO | | 0 | | +OK | bounced | int(1) unsigned | NO | | 0 | | +OK | complained | int(1) unsigned | NO | | 0 | | +OK | created | timestamp | NO | | CURRENT_TIMESTAMP | | +OK | open_tracking_disabled | tinyint(4) unsigned | NO | | 0 | | +OK | click_tracking_disabled | tinyint(4) unsigned | NO | | 0 | | +OK | namespace | int(10) unsigned | NO | MUL | NULL | | + +-------------------------+---------------------+------+-----+-------------------+----------------+ + +New columns: + +-------------------------+---------------------+------+-----+-------------------+----------------+ + | data | longtext | NO | | NULL | | + | source_type | int(10) unsigned | NO | | | | + | send_configuration | int(10) unsigned | NO | | | | + +-------------------------+---------------------+------+-----+-------------------+----------------+ + +list - we will probably need some strategy how to consistently treat stats when list/segment changes +parent - used only for campaign type RSS +last_check - used only for campaign type RSS +scheduled - used only for campaign type NORMAL + */ + +const { getSystemSendConfigurationId } = require('../../../shared/send-configurations'); + +const CampaignSource = { + TEMPLATE: 1, + CUSTOM: 2, + URL: 3, + RSS: 4 +}; + +const CampaignType = { + NORMAL: 1, + RSS: 2, + RSS_ENTRY: 3, + TRIGGERED: 4 +}; + +const CampaignStatus = { + // For campaign types: NORMAL, RSS_ENTRY + IDLE: 1, + SCHEDULED: 2, + FINISHED: 3, + PAUSED: 4, + + // For campaign types: RSS, TRIGGERED + INACTIVE: 5, + ACTIVE: 6 +}; + +exports.up = (knex, Promise) => (async() => { + + await knex.schema.table('campaigns', table => { + table.text('data', 'longtext'); + table.integer('source_type').unsigned().notNullable(); + + // Add a default values, such that the new column has some valid non-null value + table.integer('send_configuration').unsigned().notNullable().references(`send_configurations.id`).defaultTo(getSystemSendConfigurationId()); + }); + + const campaigns = await knex('campaigns'); + + for (const campaign of campaigns) { + const data = {}; + + if (campaign.type === CampaignType.NORMAL || campaign.type === CampaignType.RSS_ENTRY || campaign.type === CampaignType.NORMAL || campaign.type === CampaignType.TRIGGERED) { + if (campaign.template) { + let editorType = campaign.editor_name; + const editorData = JSON.parse(campaign.editor_data || '{}'); + + if (editorType == 'summernote') { + editorType = 'ckeditor'; + } + + campaign.source_type = CampaignSource.CUSTOM; + data.source = { + type: editorType, + data: JSON.stringify(editorData), + html: campaign.html, + text: campaign.text, + htmlPrepared: campaign.html_prepared + }; + } else { + campaign.source_type = CampaignSource.URL; + data.source = { + url: campaign.source_url + }; + } + + } else if (campaign.type === CampaignType.RSS) { + campaign.source_type = CampaignSource.RSS; + data.source = { + url: campaign.source_url + }; + + data.checkStatus = campaign.checkStatus; + } + + campaign.data = JSON.stringify(data); + + await knex('campaigns').where('id', campaign.id).update(campaign); + } + + await knex.schema.table('campaigns', table => { + table.dropColumn('template'); + table.dropColumn('source_url'); + table.dropColumn('editor_name'); + table.dropColumn('editor_data'); + table.dropColumn('check_status'); + table.dropColumn('status_change'); + table.dropColumn('html'); + table.dropColumn('html_prepared'); + table.dropColumn('text'); + table.renameColumn('from', 'from_name_override'); + table.renameColumn('address', 'from_email_override'); + table.renameColumn('reply_to', 'reply_to_override'); + table.renameColumn('subject', 'subject_override'); + + // Remove the default value + table.integer('send_configuration').unsigned().notNullable().alter(); + }); + +})(); + +exports.down = (knex, Promise) => (async() => { +})(); diff --git a/shared/mosaico-templates.js b/shared/mosaico-templates.js index 21aed124..722a8df2 100644 --- a/shared/mosaico-templates.js +++ b/shared/mosaico-templates.js @@ -1532,6 +1532,10 @@ const versafix = '\n' + '\n'; +function getVersafix() { + return versafix; +} + module.exports = { - versafix + getVersafix }; \ No newline at end of file