diff --git a/src/groupdb.js b/src/groupdb.js index 21fe34ece..535df90e8 100644 --- a/src/groupdb.js +++ b/src/groupdb.js @@ -2,6 +2,7 @@ exports = module.exports = { get: get, + getByName: getByName, getWithMembers: getWithMembers, getAll: getAll, getAllWithMembers: getAllWithMembers, @@ -42,6 +43,18 @@ function get(groupId, callback) { }); } +function getByName(name, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof callback, 'function'); + + database.query('SELECT ' + GROUPS_FIELDS + ' FROM userGroups WHERE name = ?', [ name ], function (error, result) { + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); + if (result.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Group not found')); + + callback(null, result[0]); + }); +} + function getWithMembers(groupId, callback) { assert.strictEqual(typeof groupId, 'string'); assert.strictEqual(typeof callback, 'function'); diff --git a/src/groups.js b/src/groups.js index 6f8aa69b5..4dda3a598 100644 --- a/src/groups.js +++ b/src/groups.js @@ -4,6 +4,7 @@ exports = module.exports = { create: create, remove: remove, get: get, + getByName: getByName, update: update, getWithMembers: getWithMembers, getAll: getAll, @@ -85,6 +86,17 @@ function get(id, callback) { }); } +function getByName(name, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof callback, 'function'); + + groupdb.getByName(name, function (error, result) { + if (error) return callback(error); + + return callback(null, result); + }); +} + function getWithMembers(id, callback) { assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof callback, 'function');