Working API for subscribing and unsubscribing
This commit is contained in:
parent
d5222f7b4d
commit
11f412ded1
15 changed files with 439 additions and 24 deletions
|
@ -348,7 +348,7 @@ function addCustomField(listId, name, defaultValue, type, group, visible, callba
|
|||
});
|
||||
}
|
||||
|
||||
module.exports.getRow = (fieldList, values, useDate, showAll) => {
|
||||
module.exports.getRow = (fieldList, values, useDate, showAll, onlyExisting) => {
|
||||
let valueList = {};
|
||||
let row = [];
|
||||
|
||||
|
@ -363,6 +363,10 @@ module.exports.getRow = (fieldList, values, useDate, showAll) => {
|
|||
});
|
||||
|
||||
fieldList.filter(field => showAll || field.visible).forEach(field => {
|
||||
if (onlyExisting && field.column && !valueList.hasOwnProperty(field.column)) {
|
||||
// ignore missing values
|
||||
return;
|
||||
}
|
||||
switch (field.type) {
|
||||
case 'text':
|
||||
case 'website':
|
||||
|
@ -409,15 +413,21 @@ module.exports.getRow = (fieldList, values, useDate, showAll) => {
|
|||
mergeTag: field.key,
|
||||
mergeValue: field.defaultValue,
|
||||
['type' + (field.type || '').toString().trim().replace(/(?:^|\-)([a-z])/g, (m, c) => c.toUpperCase())]: true,
|
||||
options: (field.options || []).map(subField => ({
|
||||
type: subField.type,
|
||||
name: subField.name,
|
||||
column: subField.column,
|
||||
value: valueList[subField.column] ? 1 : 0,
|
||||
visible: !!subField.visible,
|
||||
mergeTag: subField.key,
|
||||
mergeValue: valueList[subField.column] ? subField.name : subField.defaultValue
|
||||
}))
|
||||
options: (field.options || []).map(subField => {
|
||||
if (onlyExisting && subField.column && !valueList.hasOwnProperty(subField.column)) {
|
||||
// ignore missing values
|
||||
return false;
|
||||
}
|
||||
return {
|
||||
type: subField.type,
|
||||
name: subField.name,
|
||||
column: subField.column,
|
||||
value: valueList[subField.column] ? 1 : 0,
|
||||
visible: !!subField.visible,
|
||||
mergeTag: subField.key,
|
||||
mergeValue: valueList[subField.column] ? subField.name : subField.defaultValue
|
||||
};
|
||||
}).filter(subField => subField)
|
||||
};
|
||||
item.value = item.options.filter(subField => showAll || subField.visible && subField.value).map(subField => subField.name).join(', ');
|
||||
item.mergeValue = item.value || field.defaultValue;
|
||||
|
|
|
@ -248,7 +248,7 @@ module.exports.insert = (listId, meta, subscription, callback) => {
|
|||
}
|
||||
});
|
||||
|
||||
fields.getValues(fields.getRow(fieldList, subscription, true, true), true).forEach(field => {
|
||||
fields.getValues(fields.getRow(fieldList, subscription, true, true, !!meta.partial), true).forEach(field => {
|
||||
keys.push(field.key);
|
||||
values.push(field.value);
|
||||
});
|
||||
|
|
|
@ -21,7 +21,30 @@ module.exports.get = (id, callback) => {
|
|||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
connection.query('SELECT id, username, email FROM users WHERE id=? LIMIT 1', [id], (err, rows) => {
|
||||
connection.query('SELECT `id`, `username`, `email`, `access_token` FROM `users` WHERE `id`=? LIMIT 1', [id], (err, rows) => {
|
||||
connection.release();
|
||||
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!rows.length) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
let user = tools.convertKeys(rows[0]);
|
||||
return callback(null, user);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.findByAccessToken = (accessToken, callback) => {
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('SELECT `id`, `username`, `email`, `access_token` FROM `users` WHERE `access_token`=? LIMIT 1', [accessToken], (err, rows) => {
|
||||
connection.release();
|
||||
|
||||
if (err) {
|
||||
|
@ -48,7 +71,7 @@ module.exports.get = (id, callback) => {
|
|||
module.exports.authenticate = (username, password, callback) => {
|
||||
|
||||
let login = (connection, callback) => {
|
||||
connection.query('SELECT id, password FROM users WHERE username=? OR email=? LIMIT 1', [username, username], (err, rows) => {
|
||||
connection.query('SELECT `id`, `password`, `access_token` FROM `users` WHERE `username`=? OR email=? LIMIT 1', [username, username], (err, rows) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -175,6 +198,34 @@ module.exports.update = (id, updates, callback) => {
|
|||
});
|
||||
};
|
||||
|
||||
module.exports.resetToken = (id, callback) => {
|
||||
id = Number(id) || 0;
|
||||
|
||||
if (!id) {
|
||||
return callback(new Error('User ID not set'));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let token = crypto.randomBytes(20).toString('hex').toLowerCase();
|
||||
let query = 'UPDATE users SET `access_token`=? WHERE id=? LIMIT 1';
|
||||
let values = [token, id];
|
||||
|
||||
connection.query(query, values, (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(null, result.affectedRows);
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports.sendReset = (username, callback) => {
|
||||
username = (username || '').toString().trim();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue