Working API for subscribing and unsubscribing
This commit is contained in:
parent
d5222f7b4d
commit
11f412ded1
15 changed files with 439 additions and 24 deletions
188
routes/api.js
Normal file
188
routes/api.js
Normal file
|
@ -0,0 +1,188 @@
|
|||
'use strict';
|
||||
|
||||
let users = require('../lib/models/users');
|
||||
let lists = require('../lib/models/lists');
|
||||
let fields = require('../lib/models/fields');
|
||||
let subscriptions = require('../lib/models/subscriptions');
|
||||
let tools = require('../lib/tools');
|
||||
let express = require('express');
|
||||
let router = new express.Router();
|
||||
|
||||
router.all('/*', (req, res, next) => {
|
||||
if (!req.query.access_token) {
|
||||
res.status(403);
|
||||
return res.json({
|
||||
error: 'Missing access_token',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
|
||||
users.findByAccessToken(req.query.access_token, (err, user) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!user) {
|
||||
res.status(403);
|
||||
return res.json({
|
||||
error: 'Invalid or expired access_token',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.post('/subscribe/: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: []
|
||||
});
|
||||
}
|
||||
tools.validateEmail(input.EMAIL, false, err => {
|
||||
if (err) {
|
||||
res.status(400);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
|
||||
let subscription = {
|
||||
email: input.EMAIL
|
||||
};
|
||||
|
||||
if (input.FIRST_NAME) {
|
||||
subscription.first_name = (input.FIRST_NAME || '').toString().trim();
|
||||
}
|
||||
|
||||
if (input.LAST_NAME) {
|
||||
subscription.last_name = (input.LAST_NAME || '').toString().trim();
|
||||
}
|
||||
|
||||
if (input.TIMEZONE) {
|
||||
subscription.tz = (input.TIMEZONE || '').toString().trim();
|
||||
}
|
||||
|
||||
fields.list(list.id, (err, fieldList) => {
|
||||
if (err && !fieldList) {
|
||||
fieldList = [];
|
||||
}
|
||||
|
||||
fieldList.forEach(field => {
|
||||
if (input.hasOwnProperty(field.key) && field.column) {
|
||||
subscription[field.column] = input[field.key];
|
||||
} else if (field.options) {
|
||||
for (let i = 0, len = field.options.length; i < len; i++) {
|
||||
if (input.hasOwnProperty(field.options[i].key) && field.options[i].column) {
|
||||
let value = input[field.options[i].key];
|
||||
if (field.options[i].type === 'option') {
|
||||
value = ['false', 'no', '0', ''].indexOf((value || '').toString().trim().toLowerCase()) >= 0 ? '' : '1';
|
||||
}
|
||||
subscription[field.options[i].column] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let meta = {
|
||||
partial: true
|
||||
};
|
||||
|
||||
if (input.FORCE_SUBSCRIBE === 'yes') {
|
||||
meta.status = 1;
|
||||
}
|
||||
|
||||
subscriptions.insert(list.id, meta, subscription, (err, response) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: response.entryId,
|
||||
subscribed: true
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/unsubscribe/: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.unsubscribe(list.id, input.EMAIL, false, (err, subscription) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: subscription.id,
|
||||
unsubscribed: true
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -69,6 +69,10 @@ router.post('/update', passport.parseForm, passport.csrfProtection, (req, res) =
|
|||
Object.keys(data).forEach(key => {
|
||||
let value = data[key].trim();
|
||||
key = tools.toDbKey(key);
|
||||
// ensure trailing slash for service home page
|
||||
if (key === 'service_url' && value && !/\/$/.test(value)) {
|
||||
value = value + '/';
|
||||
}
|
||||
if (allowedKeys.indexOf(key) >= 0) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
|
|
|
@ -4,6 +4,7 @@ let passport = require('../lib/passport');
|
|||
let express = require('express');
|
||||
let router = new express.Router();
|
||||
let users = require('../lib/models/users');
|
||||
let settings = require('../lib/models/settings');
|
||||
|
||||
router.get('/logout', (req, res) => passport.logout(req, res));
|
||||
|
||||
|
@ -67,6 +68,47 @@ router.post('/reset', passport.parseForm, passport.csrfProtection, (req, res) =>
|
|||
});
|
||||
});
|
||||
|
||||
router.all('/api', (req, res, next) => {
|
||||
if (!req.user) {
|
||||
req.flash('danger', 'Need to be logged in to access restricted content');
|
||||
return res.redirect('/users/login?next=' + encodeURIComponent(req.originalUrl));
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
router.get('/api', passport.csrfProtection, (req, res, next) => {
|
||||
users.get(req.user.id, (err, user) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (!user) {
|
||||
return next(new Error('User data not found'));
|
||||
}
|
||||
settings.list(['serviceUrl'], (err, configItems) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
user.serviceUrl = configItems.serviceUrl;
|
||||
user.csrfToken = req.csrfToken();
|
||||
res.render('users/api', user);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.post('/api/reset-token', passport.parseForm, passport.csrfProtection, (req, res) => {
|
||||
users.resetToken(Number(req.user.id), (err, success) => {
|
||||
if (err) {
|
||||
req.flash('danger', err.message || err);
|
||||
} else if (success) {
|
||||
req.flash('success', 'Access token updated');
|
||||
} else {
|
||||
req.flash('info', 'Access token not updated');
|
||||
}
|
||||
return res.redirect('/users/api');
|
||||
});
|
||||
});
|
||||
|
||||
router.all('/account', (req, res, next) => {
|
||||
if (!req.user) {
|
||||
req.flash('danger', 'Need to be logged in to access restricted content');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue