Skip to content

Commit

Permalink
Merge pull request #480 from ateuber/master
Browse files Browse the repository at this point in the history
API: Add the possibility to change the email address of an existing list subscriber
  • Loading branch information
bures authored Mar 9, 2019
2 parents fa1bf1c + 33f9403 commit e74401b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
107 changes: 107 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,111 @@ router.get('/blacklist/get', (req, res) => {
});
});

router.post('/changeemail/:listId', (req, res) => {
let input = {};
Object.keys(req.body).forEach(key => {
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
});
if (!(input.EMAILOLD) || (input.EMAILOLD === '')) {
res.status(500);
return res.json({
error: 'EMAILOLD argument is required',
data: []
});
}
if (!(input.EMAILNEW) || (input.EMAILNEW === '')) {
res.status(500);
return res.json({
error: 'EMAILNEW argument is required',
data: []
});
}
lists.getByCid(req.params.listId, (err, list) => {
if (err) {
log.error('API', err);
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}
if (!list) {
res.status(404);
return res.json({
error: 'Selected listId not found',
data: []
});
}
blacklist.isblacklisted(input.EMAILNEW, (err, blacklisted) =>{
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}
if (blacklisted) {
res.status(500);
return res.json({
error: 'New email is blacklisted',
data: []
});
}

subscriptions.getByEmail(list.id, input.EMAILOLD, (err, subscription) => {
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}

if (!subscription) {
res.status(404);
return res.json({
error: 'Subscription with given old email not found',
data: []
});
}

subscriptions.updateAddressCheck(list, subscription.cid, input.EMAILNEW, null, (err, old, valid) => {
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}

if (!valid) {
res.status(500);
return res.json({
error: 'New email not valid',
data: []
});
}

subscriptions.updateAddress(list.id, subscription.id, input.EMAILNEW, (err) => {
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}
res.status(200);
res.json({
data: {
id: subscription.id,
changedemail: true
}
});
});
});
});
});
});
});

module.exports = router;
29 changes: 29 additions & 0 deletions views/users/api.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,32 @@
</p>

<pre>curl -XGET '{{serviceUrl}}api/list/1?access_token={{accessToken}}'</pre>

<h3>POST /api/changeemail/:listId – {{#translate}}Change email of existing list subscriber{{/translate}}</h3>

<p>
{{#translate}}This API call changes the email address of an existing list subscriber.{{/translate}}
</p>

<p>
<strong>GET</strong> {{#translate}}arguments{{/translate}}
</p>
<ul>
<li><strong>access_token</strong> – {{#translate}}your personal access token{{/translate}}
</ul>

<p>
<strong>POST</strong> {{#translate}}arguments{{/translate}}
</p>
<ul>
<li><strong>EMAILOLD</strong> – {{#translate}}subscriber's old email address{{/translate}} (<em>{{#translate}}required{{/translate}}</em>)
<li><strong>EMAILNEW</strong> – {{#translate}}subscriber's new email address{{/translate}} (<em>{{#translate}}required{{/translate}}</em>)
</ul>

<p>
<strong>{{#translate}}Example{{/translate}}</strong>
</p>

<pre>curl -XPOST {{serviceUrl}}api/changeemail/B16uVTdW?access_token={{accessToken}} \
--data '[email protected]&[email protected]'</pre>

0 comments on commit e74401b

Please sign in to comment.