Allow updating subscribers email address
This commit is contained in:
parent
8db3672a28
commit
7fce6e28fa
6 changed files with 158 additions and 4 deletions
|
@ -10,7 +10,6 @@ let settings = require('./settings');
|
|||
let mailer = require('../mailer');
|
||||
let urllib = require('url');
|
||||
let log = require('npmlog');
|
||||
let csvGenerate = require('csv-generate');
|
||||
|
||||
module.exports.list = (listId, start, limit, callback) => {
|
||||
listId = Number(listId) || 0;
|
||||
|
@ -280,6 +279,29 @@ module.exports.subscribe = (cid, optInIp, callback) => {
|
|||
subscription = {};
|
||||
}
|
||||
|
||||
if (subscription.action === 'update' && subscription.subscriber) {
|
||||
// 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();
|
||||
// reload full data from db in case it was an update, not insert
|
||||
return module.exports.getById(listId, subscription.subscriber, callback);
|
||||
});
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
subscription.cid = cid;
|
||||
subscription.list = listId;
|
||||
subscription.email = email;
|
||||
|
@ -1116,3 +1138,54 @@ module.exports.listImports = (listId, callback) => {
|
|||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
module.exports.updateAddress = (list, cid, updates, optInIp, callback) => {
|
||||
updates = tools.convertKeys(updates);
|
||||
cid = (cid || '').toString().trim();
|
||||
|
||||
let emailNew = (updates.emailNew || '').toString().trim();
|
||||
|
||||
if (!list || !list.id) {
|
||||
return callback(new Error('Missing List ID'));
|
||||
}
|
||||
|
||||
if (!cid) {
|
||||
return callback(new Error('Missing subscription ID'));
|
||||
}
|
||||
|
||||
tools.validateEmail(emailNew, false, err => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT `id`, `email` FROM `subscription__' + list.id + '` WHERE `cid`=? LIMIT 1';
|
||||
let args = [cid];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (!rows || !rows.length) {
|
||||
return callback(new Error('Unknown subscription ID'));
|
||||
}
|
||||
|
||||
if (rows[0].email === emailNew) {
|
||||
return callback(new Error('Nothing seems to be changed'));
|
||||
}
|
||||
|
||||
module.exports.addConfirmation(list, emailNew, optInIp, {
|
||||
action: 'update',
|
||||
cid,
|
||||
subscriber: rows[0].id,
|
||||
emailOld: rows[0].email
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue