diff --git a/routes/api.js b/routes/api.js index 1e4fb70d..1f13ba1f 100644 --- a/routes/api.js +++ b/routes/api.js @@ -524,4 +524,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; diff --git a/views/users/api.hbs b/views/users/api.hbs index db51df39..bd025a80 100644 --- a/views/users/api.hbs +++ b/views/users/api.hbs @@ -333,4 +333,32 @@ {{#translate}}Example{{/translate}}
-curl -XGET '{{serviceUrl}}api/list/1?access_token={{accessToken}}'\ No newline at end of file +
curl -XGET '{{serviceUrl}}api/list/1?access_token={{accessToken}}'+ +
+ {{#translate}}This API call changes the email address of an existing list subscriber.{{/translate}} +
+ ++ GET {{#translate}}arguments{{/translate}} +
++ POST {{#translate}}arguments{{/translate}} +
++ {{#translate}}Example{{/translate}} +
+ +curl -XPOST {{serviceUrl}}api/changeemail/B16uVTdW?access_token={{accessToken}} \ +--data 'EMAILOLD=test@example.com&EMAILNEW=foo@bar.com'