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;
|
Loading…
Add table
Add a link
Reference in a new issue