larvituser
Advanced tools
Comparing version 0.7.2 to 0.8.0
@@ -11,3 +11,4 @@ 'use strict'; | ||
log = require('winston'), | ||
db = require('larvitdb'); | ||
db = require('larvitdb'), | ||
logPrefix = 'larvituser: ./dataWriter.js - '; | ||
@@ -18,3 +19,3 @@ let readyInProgress = false, | ||
function addField(params, deliveryTag, msgUuid) { | ||
function addUserField(params, deliveryTag, msgUuid, cb) { | ||
const uuid = params.uuid, | ||
@@ -24,19 +25,75 @@ name = params.name, | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
db.query(sql, [lUtils.uuidToBuffer(uuid), name], function(err) { | ||
if (err) log.warn(logPrefix + 'addUserField - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
} | ||
function addUserField(params, deliveryTag, msgUuid) { | ||
helpers.getFieldUuid(params.fieldName, function(err, fieldUuid) { | ||
const dbFields = [lUtils.uuidToBuffer(params.userUuid), lUtils.uuidToBuffer(fieldUuid), params.fieldValue], | ||
sql = 'INSERT INTO user_users_data (userUuid, fieldUuid, data) VALUES(?,?,?)'; | ||
function addUserDataFields(params, deliveryTag, msgUuid, cb) { | ||
const tasks = [], | ||
dbValues = [], | ||
userUuidBuffer = lUtils.uuidToBuffer(params.userUuid); | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
let sql = 'INSERT INTO user_users_data (userUuid, fieldUuid, data) VALUES'; | ||
for (let key in params.fields) { | ||
tasks.push(function (cb) { | ||
helpers.getFieldUuid(key, function (err, fieldUuid) { | ||
if (err) { | ||
log.warn(logPrefix + 'addUserDataFields() - ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
if (params.fields[key] === null || params.fields[key] === undefined) { | ||
sql += '(?,?,NULL),'; | ||
dbValues.push(userUuidBuffer, lUtils.uuidToBuffer(fieldUuid)); | ||
} else { | ||
if (Array.isArray(params.fields[key])) { | ||
for (let i = 0; i < params.fields[key].length; i ++) { | ||
sql += '(?,?,?),'; | ||
dbValues.push(userUuidBuffer, lUtils.uuidToBuffer(fieldUuid), params.fields[key][i]); | ||
} | ||
} else { | ||
sql += '(?,?,?),'; | ||
dbValues.push(userUuidBuffer, lUtils.uuidToBuffer(fieldUuid), params.fields[key]); | ||
} | ||
} | ||
cb(err); | ||
}); | ||
}); | ||
} | ||
async.parallel(tasks, function (err) { | ||
if (err) { | ||
log.warn(logPrefix + 'addUserDataFields() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
return; | ||
} | ||
db.query(sql, dbFields, function(err) { | ||
sql = sql.substring(0, sql.length - 1); | ||
if (dbValues.length === 0) { | ||
log.info(logPrefix + 'addUserDataFields() - ' + 'No fields or field data specifed'); | ||
exports.emitter.emit(msgUuid); | ||
cb(); | ||
return; | ||
} | ||
db.query(sql, dbValues, function (err) { | ||
if (err) { log.warn(logPrefix + ' addUserDataFields() - ' + err.message); } | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
@@ -46,6 +103,11 @@ }); | ||
function create(params, deliveryTag, msgUuid) { | ||
function create(params, deliveryTag, msgUuid, cb) { | ||
const dbFields = [], | ||
sql = 'INSERT IGNORE INTO user_users (uuid, username, password) VALUES(?,?,?);'; | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
dbFields.push(lUtils.uuidToBuffer(params.uuid)); | ||
@@ -60,3 +122,3 @@ dbFields.push(params.username); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
return; | ||
@@ -66,3 +128,5 @@ } | ||
db.query(sql, dbFields, function(err) { | ||
if (err) log.warn(logPrefix + 'create() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
@@ -226,3 +290,3 @@ } | ||
function replaceFields(params, deliveryTag, msgUuid) { | ||
function replaceFields(params, deliveryTag, msgUuid, cb) { | ||
const fieldNamesToUuidBufs = {}, | ||
@@ -232,2 +296,6 @@ userUuidBuf = lUtils.uuidToBuffer(params.userUuid), | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
if (userUuidBuf === false) { | ||
@@ -238,3 +306,3 @@ const err = new Error('Invalid user uuid supplied: "' + params.userUuid + '", deliveryTag: "' + deliveryTag + '", msgUuid: "' + msgUuid + '"'); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
return; | ||
@@ -301,9 +369,15 @@ } | ||
async.series(tasks, function(err) { | ||
if (err) log.warn(logPrefix + 'replaceFields() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
} | ||
function rmUser(params, deliveryTag, msgUuid) { | ||
function rmUser(params, deliveryTag, msgUuid, cb) { | ||
const tasks = []; | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
tasks.push(function(cb) { | ||
@@ -322,7 +396,14 @@ const sql = 'DELETE FROM user_users_data WHERE userUuid = ?;'; | ||
async.series(tasks, function(err) { | ||
if (err) log.warn(logPrefix + 'rmUser() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
} | ||
function rmUserField(params, deliveryTag, msgUuid) { | ||
function rmUserField(params, deliveryTag, msgUuid, cb) { | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
helpers.getFieldUuid(params.fieldName, function(err, fieldUuid) { | ||
@@ -338,3 +419,5 @@ const dbFields = [lUtils.uuidToBuffer(params.userUuid), lUtils.uuidToBuffer(fieldUuid)], | ||
db.query(sql, dbFields, function(err) { | ||
if (err) log.warn(logPrefix + 'rmUserField() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
@@ -381,6 +464,10 @@ }); | ||
function setPassword(params, deliveryTag, msgUuid) { | ||
function setPassword(params, deliveryTag, msgUuid, cb) { | ||
const dbFields = [], | ||
sql = 'UPDATE user_users SET password = ? WHERE uuid = ?;'; | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
if (params.password === false) { | ||
@@ -394,17 +481,25 @@ dbFields.push(''); | ||
db.query(sql, dbFields, function(err) { | ||
if (err) log.warn(logPrefix + 'setPassword() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
} | ||
function setUsername(params, deliveryTag, msgUuid) { | ||
function setUsername(params, deliveryTag, msgUuid, cb) { | ||
const dbFields = [params.username, lUtils.uuidToBuffer(params.userUuid)], | ||
sql = 'UPDATE user_users SET username = ? WHERE uuid = ?;'; | ||
if (cb === undefined || typeof cb !== 'function') { | ||
cb = function() {}; | ||
} | ||
db.query(sql, dbFields, function(err) { | ||
if (err) log.warn(logPrefix + 'setUsername() - ' + err.message); | ||
exports.emitter.emit(msgUuid, err); | ||
cb(err); | ||
}); | ||
} | ||
exports.addField = addField; | ||
exports.addUserField = addUserField; | ||
exports.addUserDataFields = addUserDataFields; | ||
exports.create = create; | ||
@@ -411,0 +506,0 @@ exports.emitter = new EventEmitter(); |
@@ -62,6 +62,6 @@ 'use strict'; | ||
sendObj.action = 'addField'; | ||
sendObj.params = {}; | ||
sendObj.action = 'addUserField'; | ||
sendObj.params = {}; | ||
sendObj.params.name = fieldName; | ||
sendObj.params.uuid = uuidLib.v1(); | ||
sendObj.params.name = fieldName; | ||
@@ -68,0 +68,0 @@ intercom.send(sendObj, options, function(err, msgUuid) { |
80
index.js
@@ -19,21 +19,38 @@ 'use strict'; | ||
*/ | ||
function addUserField(userUuid, fieldName, fieldValue, cb) { | ||
const options = {'exchange': dataWriter.exchangeName}, | ||
sendObj = {}; | ||
function addUserDataField(userUuid, fieldName, fieldValue, cb) { | ||
const fields = {}; | ||
sendObj.action = 'addUserField'; | ||
sendObj.params = {}; | ||
sendObj.params.userUuid = userUuid; | ||
sendObj.params.fieldName = fieldName; | ||
sendObj.params.fieldValue = String(fieldValue).trim(); | ||
fields[fieldName] = fieldValue; | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
addUserDataFields(userUuid, fields, cb); | ||
} | ||
/** | ||
* Add user fields | ||
* | ||
* @param str userUuid | ||
* @param obj fields - field name as key, field values as array to that key - ex: {'role': ['admin','user']} | ||
* @param func cb(err) | ||
*/ | ||
function addUserDataFields(userUuid, fields, cb) { | ||
dataWriter.ready(function(err) { | ||
if (err) { cb(err); return; } | ||
dataWriter.emitter.once(msgUuid, cb); | ||
const options = {'exchange': dataWriter.exchangeName}, | ||
sendObj = {}; | ||
sendObj.action = 'addUserDataFields'; | ||
sendObj.params = {}; | ||
sendObj.params.userUuid = userUuid; | ||
sendObj.params.fields = fields; | ||
dataWriter.intercom.send(sendObj, options, function(err, msgUuid) { | ||
if (err) { cb(err); return; } | ||
dataWriter.emitter.once(msgUuid, cb); | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Checks a password for validity | ||
/**Analyze Optimize Check Repair Truncate Drop * Checks a password for validity | ||
* | ||
@@ -164,3 +181,3 @@ * @param str password - plain text password | ||
tasks.push(function(cb) { | ||
replaceUserFields(uuid, userData, cb); | ||
addUserDataFields(uuid, userData, cb); | ||
}); | ||
@@ -632,3 +649,3 @@ | ||
addUserField(returnObj.uuid, name, value, function(err) { | ||
addUserDataField(returnObj.uuid, name, value, function(err) { | ||
if (err) { cb(err); return; } | ||
@@ -646,2 +663,32 @@ | ||
/** | ||
* Adds one or more fields with values to the user object. Does not overwrite existing values. It is possible to add the same value multiple times | ||
* | ||
* @param obj fields - field name as key, field values as array to that key - ex: {'role': ['admin','user']} | ||
* @param func cb(err) | ||
*/ | ||
returnObj.addFields = function addFields(fields, cb) { | ||
if (returnObj.uuid === undefined) { | ||
const err = new Error('Cannot add field; no user loaded'); | ||
cb(err); | ||
return; | ||
} | ||
addUserDataFields(returnObj.uuid, fields, function(err) { | ||
if (err) { cb(err); return; } | ||
for (let key in fields) { | ||
if (returnObj.fields[key] === undefined) { | ||
returnObj[key] = fields[key]; | ||
} else { | ||
for (let value of fields[key]) { | ||
returnObj.fields[key].push(value); | ||
} | ||
} | ||
} | ||
cb(); | ||
}); | ||
}; | ||
/** | ||
* Replace all fields | ||
@@ -771,3 +818,4 @@ * IMPORTANT!!! Will clear all data not given in the fields parameter | ||
exports.addUserField = addUserField; | ||
exports.addUserDataField = addUserDataField; | ||
exports.addUserDataFields = addUserDataFields; | ||
exports.checkPassword = checkPassword; | ||
@@ -774,0 +822,0 @@ exports.create = create; |
{ | ||
"name": "larvituser", | ||
"version": "0.7.2", | ||
"version": "0.8.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Mikael 'Lilleman' Göransson", |
@@ -62,2 +62,14 @@ [![Build Status](https://travis-ci.org/larvit/larvituser.svg?branch=master)](https://travis-ci.org/larvit/larvituser) [![Dependencies](https://david-dm.org/larvit/larvituser.svg)](https://david-dm.org/larvit/larvituser.svg) | ||
Get distinct values for field from all users | ||
```javascript | ||
const users = new userLib.Users(); | ||
users.getFieldData('fieldName', function(err, result) { | ||
if (err) throw err; | ||
console.log(userList); // An array of strings | ||
}); | ||
``` | ||
### Advanced usage | ||
@@ -68,3 +80,3 @@ | ||
```javascript | ||
userLib.addUserField(userUuid, fieldName, fieldValue, function(err) { | ||
userLib.addUserDataField(userUuid, fieldName, fieldValue, function(err) { | ||
// Field have been added | ||
@@ -71,0 +83,0 @@ }); |
@@ -369,2 +369,19 @@ 'use strict'; | ||
}); | ||
it('should add a new field along side existing fields', function (done) { | ||
userLib.fromUsername('lilleman', function (err, user) { | ||
if (err) throw err; | ||
assert(user != false, 'The user object should not be false'); | ||
assert.deepEqual(user.fields.foo.length, 1); | ||
assert.deepEqual(user.fields.foo[0], 'bar'); | ||
user.addFields({'foo': ['yes', 'no', 'bar']}, function (err) { | ||
if (err) throw err; | ||
assert.deepEqual(user.fields.foo.length, 4); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -430,3 +447,3 @@ | ||
tasks.push(function (cb) { | ||
userLib.create('user2', 'somepassword', { 'role' : ['not customer', 'user']}, function (err, user) { | ||
userLib.create('user2', 'somepassword', { 'role' : ['not customer', 'user'], 'lastname': ['biff', 'bonk']}, function (err, user) { | ||
uuids.push(user.uuid); | ||
@@ -482,2 +499,18 @@ if (err) throw err; | ||
}); | ||
it('Get list of data values for field', function (done) { | ||
const users = new userLib.Users(); | ||
users.getFieldData('lastname', function (err, result) { | ||
assert.deepEqual(err, undefined); | ||
assert.deepEqual(result.length, 3); | ||
assert.deepEqual(result.indexOf('biff') > - 1, true); | ||
assert.deepEqual(result.indexOf('baff') > - 1, true); | ||
assert.deepEqual(result.indexOf('bonk') > - 1, true); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
@@ -484,0 +517,0 @@ |
23
users.js
@@ -11,2 +11,25 @@ 'use strict'; | ||
/** | ||
* Gets distinct data values from speciefied field for all users | ||
* | ||
* @param str fieldName - the name of the field | ||
* @param func cb(err, result) - an array with values liek ['value1', 'value2'] | ||
*/ | ||
Users.prototype.getFieldData = function (fieldName, cb) { | ||
db.query('SELECT DISTINCT d.data FROM user_users_data d ' | ||
+ 'JOIN user_data_fields f ON d.fieldUuid = f.uuid ' | ||
+ 'WHERE f.name = "' + fieldName + '"', function (err, rows) { | ||
if (err) { cb(err); return; } | ||
let result = []; | ||
for (let row of rows) { | ||
result.push(row.data); | ||
} | ||
cb(err, result); | ||
}); | ||
}; | ||
Users.prototype.get = function(cb) { | ||
@@ -13,0 +36,0 @@ const tasks = [], |
74366
23
1929
212