Added API call to create new custom fields

This commit is contained in:
Andris Reinman 2016-08-29 12:27:14 +03:00
parent 175b8eb94d
commit 1469e08063
4 changed files with 99 additions and 5 deletions

View file

@ -7,6 +7,7 @@ let lists = require('./lists');
let shortid = require('shortid'); let shortid = require('shortid');
let allowedKeys = ['name', 'key', 'default_value', 'group', 'visible']; let allowedKeys = ['name', 'key', 'default_value', 'group', 'visible'];
let allowedTypes;
module.exports.grouped = ['radio', 'checkbox', 'dropdown']; module.exports.grouped = ['radio', 'checkbox', 'dropdown'];
module.exports.types = { module.exports.types = {
@ -25,6 +26,8 @@ module.exports.types = {
option: 'Option' option: 'Option'
}; };
module.exports.allowedTypes = allowedTypes = Object.keys(module.exports.types);
module.exports.genericTypes = { module.exports.genericTypes = {
text: 'string', text: 'string',
website: 'string', website: 'string',
@ -268,7 +271,6 @@ function addCustomField(listId, name, defaultValue, type, group, visible, callba
let column = null; let column = null;
let key = slugify('merge ' + name, '_').toUpperCase(); let key = slugify('merge ' + name, '_').toUpperCase();
let allowedTypes = ['text', 'number', 'radio', 'checkbox', 'dropdown', 'date-us', 'date-eur', 'birthday-us', 'birthday-eur', 'website', 'option', 'gpg', 'longtext'];
if (allowedTypes.indexOf(type) < 0) { if (allowedTypes.indexOf(type) < 0) {
return callback(new Error('Unknown column type ' + type)); return callback(new Error('Unknown column type ' + type));

View file

@ -276,4 +276,52 @@ router.post('/delete/:listId', (req, res) => {
}); });
}); });
router.post('/field/: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) {
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: []
});
}
let field = {
name: (input.NAME || '').toString().trim(),
defaultValue: (input.DEFAULT || '').toString().trim() || null,
type: (input.TYPE || '').toString().toLowerCase().trim(),
group: Number(input.GROUP) || null,
visible: !['false', 'no', '0', ''].includes((input.VISIBLE || '').toString().toLowerCase().trim())
};
fields.create(list.id, field, (err, id) => {
if (err) {
res.status(500);
return res.json({
error: err.message || err,
data: []
});
}
res.status(200);
res.json({
data: {
id
}
});
});
});
});
module.exports = router; module.exports = router;

View file

@ -4,6 +4,7 @@ let passport = require('../lib/passport');
let express = require('express'); let express = require('express');
let router = new express.Router(); let router = new express.Router();
let users = require('../lib/models/users'); let users = require('../lib/models/users');
let fields = require('../lib/models/fields');
let settings = require('../lib/models/settings'); let settings = require('../lib/models/settings');
router.get('/logout', (req, res) => passport.logout(req, res)); router.get('/logout', (req, res) => passport.logout(req, res));
@ -90,6 +91,10 @@ router.get('/api', passport.csrfProtection, (req, res, next) => {
} }
user.serviceUrl = configItems.serviceUrl; user.serviceUrl = configItems.serviceUrl;
user.csrfToken = req.csrfToken(); user.csrfToken = req.csrfToken();
user.allowedTypes = Object.keys(fields.types).map(key => ({
type: key,
description: fields.types[key]
}));
res.render('users/api', user); res.render('users/api', user);
}); });
}); });

View file

@ -73,7 +73,8 @@
<ul> <ul>
<li> <li>
<strong>FORCE_SUBSCRIBE</strong> set to "yes" if you want to make sure the email is marked as subscribed even if it was previously marked as unsubscribed. If the email was already unsubscribed/blocked then subscription status is not changed by default. <strong>FORCE_SUBSCRIBE</strong> set to "yes" if you want to make sure the email is marked as subscribed even if it was previously marked as unsubscribed. If the email was already unsubscribed/blocked then subscription status is not changed
by default.
</li> </li>
<li> <li>
<strong>REQUIRE_CONFIRMATION</strong> set to "yes" if you want to send confirmation email to the subscriber before actually marking as subscribed <strong>REQUIRE_CONFIRMATION</strong> set to "yes" if you want to send confirmation email to the subscriber before actually marking as subscribed
@ -140,3 +141,41 @@
<pre>curl -XPOST {{serviceUrl}}api/delete/B16uVTdW?access_token={{accessToken}} \ <pre>curl -XPOST {{serviceUrl}}api/delete/B16uVTdW?access_token={{accessToken}} \
--data 'EMAIL=test@example.com'</pre> --data 'EMAIL=test@example.com'</pre>
<h3>POST /api/field/:listId Add new custom field</h3>
<p>
This API call creates a new custom field for a list.
</p>
<p>
<strong>GET</strong> arguments
</p>
<ul>
<li><strong>access_token</strong> your personal access token
</ul>
<p>
<strong>POST</strong> arguments
</p>
<ul>
<li><strong>NAME</strong> field name (<em>required</em>)</li>
<li><strong>TYPE</strong> one of the following types:
<ul>
{{#each allowedTypes}}
<li>
<strong>{{type}}</strong> {{description}}
</li>
{{/each}}
</ul>
</li>
<li><strong>GROUP</strong> If the type is 'option' then you also need to specify the parent element ID</li>
<li><strong>VISIBLE</strong> yes/no, if not visible then the subscriber can not view or modify this value at the profile page</li>
</ul>
<p>
<strong>Example</strong>
</p>
<pre>curl -XPOST {{serviceUrl}}api/field/B16uVTdW?access_token={{accessToken}} \
--data 'NAME=Birthday&amp;TYPE=birthday-us&amp;VISIBLE=yes'</pre>