Merge pull request #369 from gsiou/api-search-user

API functionality - Get lists a user has subscribed to
This commit is contained in:
Andris Reinman 2018-01-09 12:37:29 +02:00 committed by GitHub
commit 304b9a0fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View file

@ -4,6 +4,7 @@ let db = require('../db');
let tools = require('../tools');
let shortid = require('shortid');
let segments = require('./segments');
let subscriptions = require('./subscriptions');
let _ = require('../translate')._;
let tableHelpers = require('../table-helpers');
@ -64,6 +65,37 @@ module.exports.quicklist = callback => {
});
};
module.exports.getListsWithEmail = (email, callback) => {
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
connection.query('SELECT id, name FROM lists', (err, rows) => {
connection.release();
if (err) {
return callback(err);
}
let lists = (rows || []).map(tools.convertKeys);
const results = [];
lists.forEach((list, index, arr) => {
subscriptions.getByEmail(list.id, email, (err, sub) => {
if (err) {
return callback(err);
}
if (sub) {
results.push(list.id);
}
if (index === arr.length - 1) {
return callback(null, lists.filter(list => results.includes(list.id)));
}
});
});
});
});
};
module.exports.getByCid = (cid, callback) => {
resolveCid(cid, (err, id) => {
if (err) {

View file

@ -348,6 +348,22 @@ router.get('/subscriptions/:listId', (req, res) => {
});
});
router.get('/lists/:email', (req, res) => {
lists.getListsWithEmail(req.params.email, (err, lists) => {
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}
res.status(200);
res.json({
data: lists
});
});
});
router.post('/field/:listId', (req, res) => {
let input = {};
Object.keys(req.body).forEach(key => {