diff --git a/lib/models/subscriptions.js b/lib/models/subscriptions.js index e75cf298..822a31b2 100644 --- a/lib/models/subscriptions.js +++ b/lib/models/subscriptions.js @@ -531,6 +531,34 @@ module.exports.getById = (listId, id, callback) => { }); }; +module.exports.getByEmail = (listId, email, callback) => { + if (!email) { + return callback(new Error('Missing Subbscription email address')); + } + + db.getConnection((err, connection) => { + if (err) { + return callback(err); + } + + connection.query('SELECT * FROM `subscription__' + listId + '` WHERE email=?', [email], (err, rows) => { + connection.release(); + if (err) { + return callback(err); + } + + if (!rows || !rows.length) { + return callback(null, false); + } + + let subscription = tools.convertKeys(rows[0]); + // ensure list id in response + subscription.list = subscription.list || listId; + return callback(null, subscription); + }); + }); +}; + module.exports.getWithMergeTags = (listId, cid, callback) => { module.exports.get(listId, cid, (err, subscription) => { if (err) { diff --git a/routes/api.js b/routes/api.js index 804ef93f..8c312711 100644 --- a/routes/api.js +++ b/routes/api.js @@ -207,4 +207,73 @@ router.post('/unsubscribe/:listId', (req, res) => { }); }); +router.post('/delete/:listId', (req, res) => { + let input = {}; + Object.keys(req.body).forEach(key => { + input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim(); + }); + lists.getByCid(req.params.listId, (err, list) => { + if (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: [] + }); + } + if (!input.EMAIL) { + res.status(400); + return res.json({ + error: 'Missing EMAIL', + data: [] + }); + } + subscriptions.getByEmail(list.id, input.EMAIL, (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 not found', + data: [] + }); + } + subscriptions.delete(list.id, subscription.cid, (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 not found', + data: [] + }); + } + res.status(200); + res.json({ + data: { + id: subscription.id, + deleted: true + } + }); + }); + }); + }); +}); + module.exports = router; diff --git a/views/triggers/triggered.hbs b/views/triggers/triggered.hbs index cc5945ea..57af08aa 100644 --- a/views/triggers/triggered.hbs +++ b/views/triggers/triggered.hbs @@ -1,6 +1,6 @@ diff --git a/views/users/api.hbs b/views/users/api.hbs index 5563ce02..d8249d95 100644 --- a/views/users/api.hbs +++ b/views/users/api.hbs @@ -113,3 +113,30 @@
curl -XPOST {{serviceUrl}}api/unsubscribe/B16uVTdW?access_token={{accessToken}}\
 --data 'EMAIL=test@example.com'
+ +

POST /api/delete/:listId – Delete subscription

+ +

+ This API call deletes a subscription +

+ +

+ GET arguments +

+ + +

+ POST arguments +

+ + +

+ Example +

+ +
curl -XPOST {{serviceUrl}}api/delete/B16uVTdW?access_token={{accessToken}}\
+--data 'EMAIL=test@example.com'