More or less all the functionality for selectable unsubscription process. Not tested yet!
Sending emails moved completely to controller. It felt strange to have some emails sent from the controller and some of them from the model. Confirmations refactored to an independent model that can be potentially used also for other actions that need an email confirmation.
This commit is contained in:
parent
32e2e61789
commit
bd4961366f
13 changed files with 672 additions and 488 deletions
|
@ -5,12 +5,9 @@ let shortid = require('shortid');
|
|||
let tools = require('../tools');
|
||||
let helpers = require('../helpers');
|
||||
let fields = require('./fields');
|
||||
let geoip = require('geoip-ultralight');
|
||||
let segments = require('./segments');
|
||||
let settings = require('./settings');
|
||||
let mailer = require('../mailer');
|
||||
let urllib = require('url');
|
||||
let log = require('npmlog');
|
||||
let _ = require('../translate')._;
|
||||
let util = require('util');
|
||||
let tableHelpers = require('../table-helpers');
|
||||
|
@ -88,150 +85,17 @@ module.exports.filter = (listId, request, columns, segmentId, callback) => {
|
|||
|
||||
};
|
||||
|
||||
module.exports.addConfirmation = (list, email, ip, data, callback) => {
|
||||
let cid = shortid.generate();
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'INSERT INTO confirmations (cid, list, email, ip, data) VALUES (?,?,?,?,?)';
|
||||
connection.query(query, [cid, list.id, email, ip, JSON.stringify(data || {})], (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!result || !result.affectedRows) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
if (data._skip) {
|
||||
log.info('Subscription', 'Confirmation message for %s marked to be skipped (%s)', email, JSON.stringify(data));
|
||||
return callback(null, cid);
|
||||
}
|
||||
|
||||
// FIXME - customize from the router
|
||||
const mailOpts = {
|
||||
ignoreDisableConfirmations: true
|
||||
};
|
||||
const relativeUrls = {
|
||||
confirmUrl: '/subscription/confirm/' + cid
|
||||
};
|
||||
module.exports.sendMail(list, email, 'confirm-subscription', _('%s: Please Confirm Subscription'), relativeUrls, mailOpts, data, (err) => {
|
||||
return callback(err, cid);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.processConfirmation = (cid, ip, callback) => {
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT * FROM confirmations WHERE cid=? LIMIT 1';
|
||||
connection.query(query, [cid], (err, rows) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!rows || !rows.length) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
let subscription;
|
||||
let listId = rows[0].list;
|
||||
let email = rows[0].email;
|
||||
try {
|
||||
subscription = JSON.parse(rows[0].data);
|
||||
} catch (E) {
|
||||
subscription = {};
|
||||
}
|
||||
|
||||
if (subscription._action === 'update') {
|
||||
if (!subscription.subscriber) { // Something went terribly wrong and we don't have data that we have originally provided
|
||||
return callback(new Error(_('Subscriber info corrupted or missing')));
|
||||
}
|
||||
|
||||
// update email address instead of adding new
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
let query = 'UPDATE `subscription__' + listId + '` SET `email`=? WHERE `id`=? LIMIT 1';
|
||||
let args = [email, subscription.subscriber];
|
||||
connection.query(query, args, err => {
|
||||
if (err) {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
}
|
||||
connection.query('DELETE FROM confirmations WHERE `cid`=? LIMIT 1', [cid], () => {
|
||||
connection.release();
|
||||
return module.exports.getById(listId, subscription.subscriber, (err, subscriptionData) => {
|
||||
return callback(err, subscriptionData, subscription._action);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
return;
|
||||
|
||||
} else if (subscription._action === 'unsubscribe') {
|
||||
// TODO
|
||||
return;
|
||||
|
||||
} else if (subscription._action === 'subscribe') {
|
||||
subscription.cid = cid;
|
||||
subscription.list = listId;
|
||||
subscription.email = email;
|
||||
|
||||
let optInCountry = geoip.lookupCountry(ip) || null;
|
||||
module.exports.insert(listId, {
|
||||
email,
|
||||
cid,
|
||||
optInIp: ip,
|
||||
optInCountry,
|
||||
status: 1
|
||||
}, subscription, (err, result) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!result.entryId) {
|
||||
return callback(new Error(_('Could not save subscription')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
connection.query('DELETE FROM confirmations WHERE `cid`=? LIMIT 1', [cid], () => {
|
||||
connection.release();
|
||||
// reload full data from db in case it was an update, not insert
|
||||
return module.exports.getById(listId, result.entryId, (err, subscriptionData) => {
|
||||
return callback(err, subscriptionData, subscription._action);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
} else {
|
||||
return callback(new Error(util.format(_('Subscription request corrupted - action: %s'), subscription._action)));
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.insert = (listId, meta, subscription, callback) => {
|
||||
|
||||
/*
|
||||
Adds a new subscription. Returns error if a subscription with the same email address is already present and is not unsubscribed.
|
||||
If it is unsubscribed, the existing subscription is changed based on the provided data.
|
||||
If meta.partial is true, it updates even an active subscription.
|
||||
*/
|
||||
module.exports.insert = (listId, meta, subscriptionData, callback) => {
|
||||
meta = tools.convertKeys(meta);
|
||||
subscription = tools.convertKeys(subscription);
|
||||
subscriptionData = tools.convertKeys(subscriptionData);
|
||||
|
||||
meta.email = meta.email || subscription.email;
|
||||
meta.email = meta.email || subscriptionData.email;
|
||||
meta.cid = meta.cid || shortid.generate();
|
||||
|
||||
fields.list(listId, (err, fieldList) => {
|
||||
|
@ -245,8 +109,8 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
let values = [];
|
||||
|
||||
let allowedKeys = ['first_name', 'last_name', 'tz', 'is_test'];
|
||||
Object.keys(subscription).forEach(key => {
|
||||
let value = subscription[key];
|
||||
Object.keys(subscriptionData).forEach(key => {
|
||||
let value = subscriptionData[key];
|
||||
key = tools.toDbKey(key);
|
||||
if (key === 'tz') {
|
||||
value = (value || '').toString().toLowerCase().trim();
|
||||
|
@ -260,8 +124,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
}
|
||||
});
|
||||
|
||||
// FIXME - see issue #218
|
||||
fields.getValues(fields.getRow(fieldList, subscription, true, true, !!meta.partial), true).forEach(field => {
|
||||
fields.getValues(fields.getRow(fieldList, subscriptionData, true, true, !!meta.partial), true).forEach(field => {
|
||||
keys.push(field.key);
|
||||
values.push(field.value);
|
||||
});
|
||||
|
@ -280,10 +143,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
let query = 'SELECT `id`, `status`, `cid` FROM `subscription__' + listId + '` WHERE `email`=? OR `cid`=? LIMIT 1';
|
||||
connection.query(query, [meta.email, meta.cid], (err, rows) => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
let query;
|
||||
|
@ -297,6 +157,10 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
let statusChange = !existing || existing.status !== meta.status;
|
||||
let statusDirection;
|
||||
|
||||
if (existing && !meta.partial) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, new Error(_('Email address already registered')), callback);
|
||||
}
|
||||
|
||||
if (statusChange) {
|
||||
keys.push('status', 'status_change');
|
||||
values.push(meta.status, new Date());
|
||||
|
@ -307,10 +171,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
// nothing to update
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, {
|
||||
|
@ -334,10 +195,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
|
||||
connection.query(query, queryArgs, (err, result) => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
entryId = result.insertId || entryId;
|
||||
|
@ -345,17 +203,11 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
if (statusChange && statusDirection) {
|
||||
connection.query('UPDATE lists SET `subscribers`=`subscribers`' + statusDirection + '1 WHERE id=?', [listId], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, {
|
||||
|
@ -368,10 +220,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
} else {
|
||||
connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, {
|
||||
|
@ -529,7 +378,7 @@ module.exports.update = (listId, cid, updates, allowEmail, callback) => {
|
|||
}
|
||||
|
||||
if (!cid) {
|
||||
return callback(new Error(_('Missing subscription ID')));
|
||||
return callback(new Error(_('Missing Subscription ID')));
|
||||
}
|
||||
|
||||
fields.list(listId, (err, fieldList) => {
|
||||
|
@ -581,45 +430,7 @@ module.exports.update = (listId, cid, updates, allowEmail, callback) => {
|
|||
});
|
||||
};
|
||||
|
||||
module.exports.unsubscribe = (listId, subscriberCid, campaignId, callback) => {
|
||||
listId = Number(listId) || 0;
|
||||
|
||||
campaignId = (campaignId || '').toString().trim() || false;
|
||||
|
||||
if (listId < 1) {
|
||||
return callback(new Error(_('Missing List ID')));
|
||||
}
|
||||
|
||||
if (!subscriberCid) {
|
||||
return callback(new Error(_('Missing subscriber cid')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('SELECT * FROM `subscription__' + listId + '` WHERE `cid`=?', [subscriberCid], (err, rows) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (!rows || !rows.length || rows[0].status !== 1) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
let subscription = tools.convertKeys(rows[0]);
|
||||
module.exports.changeStatus(subscription.id, listId, campaignId, 2, err => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(null, subscription);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
||||
module.exports.changeStatus = (listId, id, campaignId, status, callback) => {
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
|
@ -632,17 +443,11 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
|
||||
connection.query('SELECT `status` FROM `subscription__' + listId + '` WHERE id=? LIMIT 1', [id], (err, rows) => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (!rows || !rows.length) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(null, false);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(null, false));
|
||||
}
|
||||
|
||||
let oldStatus = rows[0].status;
|
||||
|
@ -650,10 +455,7 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
let statusDirection;
|
||||
|
||||
if (!statusChange) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(null, true);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(null, true));
|
||||
}
|
||||
|
||||
if (statusChange && oldStatus === 1 || status === 1) {
|
||||
|
@ -662,19 +464,13 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
|
||||
connection.query('UPDATE `subscription__' + listId + '` SET `status`=?, `status_change`=NOW() WHERE id=? LIMIT 1', [status, id], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (!statusDirection) {
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, true);
|
||||
|
@ -683,20 +479,14 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
|
||||
connection.query('UPDATE `lists` SET `subscribers`=`subscribers`' + statusDirection + '1 WHERE id=? LIMIT 1', [listId], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
// status change is not related to a campaign or it marks message as bounced etc.
|
||||
if (!campaignId || status > 2) {
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, true);
|
||||
|
@ -705,10 +495,7 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
|
||||
connection.query('SELECT `id` FROM `campaigns` WHERE `cid`=? LIMIT 1', [campaignId], (err, rows) => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
let campaign = rows && rows[0] || false;
|
||||
|
@ -717,10 +504,7 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
// should not happend
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, true);
|
||||
|
@ -730,10 +514,7 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
// we should see only unsubscribe events here but you never know
|
||||
connection.query('UPDATE `campaigns` SET `unsubscribed`=`unsubscribed`' + (status === 2 ? '+' : '-') + '1 WHERE `cid`=? LIMIT 1', [campaignId], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
let query = 'UPDATE `campaign__' + campaign.id + '` SET `status`=? WHERE `list`=? AND `subscription`=? LIMIT 1';
|
||||
|
@ -742,18 +523,12 @@ module.exports.changeStatus = (id, listId, campaignId, status, callback) => {
|
|||
// Updated tracker status
|
||||
connection.query(query, values, err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, true);
|
||||
|
@ -805,19 +580,13 @@ module.exports.delete = (listId, cid, callback) => {
|
|||
|
||||
connection.query('DELETE FROM `subscription__' + listId + '` WHERE cid=? LIMIT 1', [cid], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (subscription.status !== 1) {
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, subscription.email);
|
||||
|
@ -826,17 +595,11 @@ module.exports.delete = (listId, cid, callback) => {
|
|||
|
||||
connection.query('UPDATE lists SET subscribers=subscribers-1 WHERE id=? LIMIT 1', [listId], err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.commit(err => {
|
||||
if (err) {
|
||||
return connection.rollback(() => {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
});
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
return callback(null, subscription.email);
|
||||
|
@ -1028,13 +791,13 @@ module.exports.listImports = (listId, callback) => {
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
module.exports.updateAddress = (list, cid, updates, ip, callback) => {
|
||||
updates = tools.convertKeys(updates);
|
||||
/*
|
||||
Performs checks before update of an address. This includes finding the existing subscriber, validating the new email
|
||||
and checking whether the new email does not conflict with other subscribers.
|
||||
*/
|
||||
module.exports.updateAddressCheck = (list, cid, emailNew, ip, callback) => {
|
||||
cid = (cid || '').toString().trim();
|
||||
|
||||
let emailNew = (updates.emailNew || '').toString().trim();
|
||||
|
||||
if (!list || !list.id) {
|
||||
return callback(new Error(_('Missing List ID')));
|
||||
}
|
||||
|
@ -1053,7 +816,7 @@ module.exports.updateAddress = (list, cid, updates, ip, callback) => {
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT `id`, `email` FROM `subscription__' + list.id + '` WHERE `cid`=? LIMIT 1';
|
||||
let query = 'SELECT * FROM `subscription__' + list.id + '` WHERE `cid`=? LIMIT 1';
|
||||
let args = [cid];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
if (err) {
|
||||
|
@ -1072,7 +835,7 @@ module.exports.updateAddress = (list, cid, updates, ip, callback) => {
|
|||
|
||||
let old = rows[0];
|
||||
|
||||
let query = 'SELECT `id` FROM `subscription__' + list.id + '` WHERE `email`=? AND `cid`<>? LIMIT 1';
|
||||
let query = 'SELECT `id` FROM `subscription__' + list.id + '` WHERE `email`=? AND `cid`<>? AND `status`=1 LIMIT 1';
|
||||
let args = [emailNew, cid];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
connection.release();
|
||||
|
@ -1080,18 +843,11 @@ module.exports.updateAddress = (list, cid, updates, ip, callback) => {
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
if (rows && rows[0] && rows[0].id) {
|
||||
|
||||
|
||||
return callback(new Error(_('This address is already registered by someone else')));
|
||||
if (rows && rows.length > 0) {
|
||||
return callback(null, old, false);
|
||||
} else {
|
||||
return callback(null, old, true);
|
||||
}
|
||||
|
||||
module.exports.addConfirmation(list, emailNew, ip, {
|
||||
_action: 'update',
|
||||
cid,
|
||||
subscriber: old.id,
|
||||
emailOld: old.email
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1099,106 +855,64 @@ module.exports.updateAddress = (list, cid, updates, ip, callback) => {
|
|||
};
|
||||
|
||||
|
||||
module.exports.sendMail = (list, email, template, subject, relativeUrls, mailOpts, subscription, callback) => {
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
fields.list(list.id, (err, fieldList) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let encryptionKeys = [];
|
||||
fields.getRow(fieldList, subscription).forEach(field => {
|
||||
if (field.type === 'gpg' && field.value) {
|
||||
encryptionKeys.push(field.value.trim());
|
||||
}
|
||||
});
|
||||
|
||||
settings.list(['defaultHomepage', 'defaultFrom', 'defaultAddress', 'defaultPostaddress', 'serviceUrl', 'disableConfirmations'], (err, configItems) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!mailOpts.ignoreDisableConfirmations && configItems.disableConfirmations) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = {
|
||||
title: list.name,
|
||||
homepage: configItems.defaultHomepage || configItems.serviceUrl,
|
||||
contactAddress: configItems.defaultAddress,
|
||||
defaultPostaddress: configItems.defaultPostaddress,
|
||||
};
|
||||
|
||||
for (let relativeUrlKey in relativeUrls) {
|
||||
data[relativeUrlKey] = urllib.resolve(configItems.serviceUrl, relativeUrls[relativeUrlKey]);
|
||||
}
|
||||
|
||||
function sendMail(html, text) {
|
||||
mailer.sendMail({
|
||||
from: {
|
||||
name: configItems.defaultFrom,
|
||||
address: configItems.defaultAddress
|
||||
},
|
||||
to: {
|
||||
name: [].concat(subscription.firstName || []).concat(subscription.lastName || []).join(' '),
|
||||
address: email
|
||||
},
|
||||
subject: util.format(subject, list.name),
|
||||
encryptionKeys
|
||||
}, {
|
||||
html,
|
||||
text,
|
||||
data
|
||||
}, err => {
|
||||
if (err) {
|
||||
log.error('Subscription', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let text = {
|
||||
template: 'subscription/mail-' + template + '-text.hbs'
|
||||
};
|
||||
|
||||
let html = {
|
||||
template: 'subscription/mail-' + template + '-html.mjml.hbs',
|
||||
layout: 'subscription/layout.mjml.hbs',
|
||||
type: 'mjml'
|
||||
};
|
||||
|
||||
helpers.injectCustomFormTemplates(list.defaultForm, { text, html }, (err, tmpl) => {
|
||||
if (err) {
|
||||
return sendMail(html, text);
|
||||
}
|
||||
|
||||
sendMail(tmpl.html, tmpl.text);
|
||||
});
|
||||
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
FIXME
|
||||
function getUnsubscriptionMode = (listId, start, limit, callback) => {
|
||||
listId = Number(listId) || 0;
|
||||
if (!listId) {
|
||||
return callback(new Error('Missing List ID'));
|
||||
}
|
||||
|
||||
tableHelpers.list('subscription__' + listId, ['*'], 'email', null, start, limit, (err, rows, total) => {
|
||||
if (!err) {
|
||||
rows = rows.map(row => tools.convertKeys(row));
|
||||
Updates address in subscription__xxx
|
||||
*/
|
||||
module.exports.updateAddress = (listId, subscriptionId, emailNew, callback) => {
|
||||
// update email address instead of adding new
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(err, rows, total);
|
||||
connection.beginTransaction(err => {
|
||||
if (err) {
|
||||
connection.release();
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT `id` FROM `subscription__' + listId + '` WHERE `email`=? AND `id`<>? AND `status`=1 LIMIT 1';
|
||||
let args = [emailNew, subscriptionId];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (rows && rows.length > 0) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, new Error(_('Email address already registered')), callback);
|
||||
}
|
||||
|
||||
let query = 'DELETE FROM `subscription__' + listId + '` WHERE `email`=? AND `id`<>?';
|
||||
let args = [emailNew, subscriptionId];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
let query = 'UPDATE `subscription__' + listId + '` SET `email`=? WHERE `id`=? LIMIT 1';
|
||||
let args = [emailNew, subscriptionId];
|
||||
connection.query(query, args, err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
connection.release();
|
||||
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
module.exports.getUnsubscriptionMode = (list, subscriptionId) => {
|
||||
// TODO: Once the unsubscription mode is customizable per segment, then this will be a good place to process it.
|
||||
return list.unsubscriptionMode;
|
||||
};
|
||||
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue