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);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"schemaVersion": 19
|
||||
"schemaVersion": 20
|
||||
}
|
||||
|
|
|
@ -331,6 +331,59 @@ router.post('/:lcid/manage', passport.parseForm, passport.csrfProtection, (req,
|
|||
});
|
||||
});
|
||||
|
||||
router.get('/:lcid/manage-address/:ucid', passport.csrfProtection, (req, res, next) => {
|
||||
lists.getByCid(req.params.lcid, (err, list) => {
|
||||
if (!err && !list) {
|
||||
err = new Error('Selected list not found');
|
||||
err.status = 404;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
subscriptions.get(list.id, req.params.ucid, (err, subscription) => {
|
||||
if (!err && !subscription) {
|
||||
err = new Error('Subscription not found from this list');
|
||||
err.status = 404;
|
||||
}
|
||||
|
||||
subscription.lcid = req.params.lcid;
|
||||
subscription.title = list.name;
|
||||
subscription.csrfToken = req.csrfToken();
|
||||
subscription.layout = 'subscription/layout';
|
||||
subscription.useEditor = true;
|
||||
|
||||
res.render('subscription/manage-address', subscription);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.post('/:lcid/manage-address', passport.parseForm, passport.csrfProtection, (req, res, next) => {
|
||||
lists.getByCid(req.params.lcid, (err, list) => {
|
||||
if (!err && !list) {
|
||||
err = new Error('Selected list not found');
|
||||
err.status = 404;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
subscriptions.updateAddress(list, req.body.cid, req.body, req.ip, err => {
|
||||
if (err) {
|
||||
req.flash('danger', err.message || err);
|
||||
log.error('Subscription', err);
|
||||
return res.redirect('/subscription/' + encodeURIComponent(req.params.lcid) + '/manage-address/' + encodeURIComponent(req.body.cid) + '?' + tools.queryParams(req.body));
|
||||
}
|
||||
|
||||
req.flash('info', 'Email address updated, check your mailbox for verification instructions');
|
||||
res.redirect('/subscription/' + req.params.lcid + '/manage/' + req.body.cid);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:lcid/unsubscribe/:ucid', passport.csrfProtection, (req, res, next) => {
|
||||
lists.getByCid(req.params.lcid, (err, list) => {
|
||||
if (!err && !list) {
|
||||
|
|
|
@ -201,7 +201,7 @@ CREATE TABLE `segments` (
|
|||
`created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `list` (`list`),
|
||||
KEY `name` (`name`),
|
||||
KEY `name` (`name`(191)),
|
||||
CONSTRAINT `segments_ibfk_1` FOREIGN KEY (`list`) REFERENCES `lists` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE TABLE `settings` (
|
||||
|
|
25
views/subscription/manage-address.hbs
Normal file
25
views/subscription/manage-address.hbs
Normal file
|
@ -0,0 +1,25 @@
|
|||
<h2>Update your Email Address</h2>
|
||||
|
||||
<form method="post" action="/subscription/{{lcid}}/manage-address">
|
||||
|
||||
<input type="hidden" name="_csrf" value="{{csrfToken}}">
|
||||
<input type="hidden" name="cid" value="{{cid}}">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="email">Existing Email Address</label>
|
||||
<input type="email" class="form-control" name="email" id="email" placeholder="" value="{{email}}" readonly>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="email-new">New Email Address</label>
|
||||
<input type="email" class="form-control" name="email-new" id="email-new" placeholder="Your new email address" value="{{email}}">
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<small>You will receive a confirmation request to your new email address that you need to accept before your email is actually changed</small>
|
||||
</p>
|
||||
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i> Update Email Address</button>
|
||||
</div>
|
||||
</form>
|
|
@ -15,7 +15,10 @@
|
|||
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input type="email" class="form-control" name="email" id="email" placeholder="" value="{{email}}" readonly>
|
||||
<div class="input-group">
|
||||
<input type="email" class="form-control" name="email" id="email" placeholder="" value="{{email}}" readonly>
|
||||
<div class="input-group-addon"><a href="/subscription/{{lcid}}/manage-address/{{cid}}">want to change it?</a></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
|
|
Loading…
Reference in a new issue