diff --git a/lib/units/api/controllers/user.js b/lib/units/api/controllers/user.js index f82ce2b0..537e0099 100644 --- a/lib/units/api/controllers/user.js +++ b/lib/units/api/controllers/user.js @@ -1,5 +1,14 @@ +var Promise = require('bluebird') + +var dbapi = require('../../../db/api') +var logger = require('../../../util/logger') +var datautil = require('../../../util/datautil') + +var log = logger.createLogger('api:contoller:user') + module.exports = { getCurrentUser: getCurrentUser +, getCurrentUserGroup: getCurrentUserGroup }; function getCurrentUser(req, res) { @@ -8,3 +17,25 @@ function getCurrentUser(req, res) { , user: req.user }) } + +function getCurrentUserGroup(req, res) { + dbapi.loadGroup(req.user.email) + .then(function(cursor) { + return Promise.promisify(cursor.toArray, cursor)() + .then(function(list) { + list.forEach(function(device) { + datautil.normalize(device, req.user) + }) + res.json({ + success: true + , devices: list + }) + }) + }) + .catch(function(err) { + log.error('Failed to load group: ', err.stack) + res.json(500, { + success: false + }) + }) +} diff --git a/lib/units/api/swagger/api_v1.yaml b/lib/units/api/swagger/api_v1.yaml index 07cee6b4..20ade953 100644 --- a/lib/units/api/swagger/api_v1.yaml +++ b/lib/units/api/swagger/api_v1.yaml @@ -33,6 +33,22 @@ paths: description: Unexpected Error schema: $ref: "#/definitions/ErrorResponse" +# TODO: Change group endpoint with something more easy to understandable endpoint + /group: + x-swagger-router-controller: user + get: + summary: User Group + description: The User Group endpoint returns information about user group of current authorized user. + operationId: getCurrentUserGroup + responses: + "200": + description: Current User Group information + schema: + $ref: "#/definitions/GroupResponse" + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" /devices: x-swagger-router-controller: device get: @@ -77,6 +93,14 @@ definitions: properties: user: type: object + GroupResponse: + required: + - devices + properties: + devices: + type: array + items: + type: object DeviceListResponse: required: - devices diff --git a/lib/units/app/index.js b/lib/units/app/index.js index ac95ebe3..53ef8489 100644 --- a/lib/units/app/index.js +++ b/lib/units/app/index.js @@ -124,28 +124,6 @@ module.exports = function(options) { res.send('var GLOBAL_APPSTATE = ' + JSON.stringify(state)) }) - app.get('/app/api/v1/group', function(req, res) { - dbapi.loadGroup(req.user.email) - .then(function(cursor) { - return Promise.promisify(cursor.toArray, cursor)() - .then(function(list) { - list.forEach(function(device) { - datautil.normalize(device, req.user) - }) - res.json({ - success: true - , devices: list - }) - }) - }) - .catch(function(err) { - log.error('Failed to load group: ', err.stack) - res.json(500, { - success: false - }) - }) - }) - app.get('/app/api/v1/accessTokens', function(req, res) { dbapi.loadAccessTokens(req.user.email) .then(function(cursor) { diff --git a/res/app/components/stf/device/device-service.js b/res/app/components/stf/device/device-service.js index 0ab5b6b2..a01df982 100644 --- a/res/app/components/stf/device/device-service.js +++ b/res/app/components/stf/device/device-service.js @@ -181,8 +181,8 @@ module.exports = function DeviceServiceFactory($http, socket, EnhanceDeviceServi , digest: true }) - oboe('/app/api/v1/group') - .node('devices[*]', function(device) { + oboe('/api/v1/group') + .node('devices[*]', function (device) { tracker.add(device) })