<commit_before>var models = require('../../models');
var util = require('./util');
var jwt = require('jsonwebtoken');
var config = require('../../config/config');
var responseUtil = require('./responseUtil');

module.exports = function(app, baseUrl) {
  var apiUrl = baseUrl + '/devices';

  app.post(apiUrl, function(req, res) {
    // Check that required data is given.
    if (!req.body.devicename || !req.body.password || !req.body.group) {
      return responseUtil.send(res, {
        statusCode: 400,
        success: false,
        messages: ['Missing devicename or password or group.']
      });
    }

    // Checks that devicename is free, group exists, creates device
    // then responds with a device JWT.
    models.Device.freeDevicename(req.body.devicename)
      .then(function(result) {
        if (!result) {  // Throw error if devicename is allready used.
          var err = new Error('Devicename in use.');
          err.invalidRequest = true;
          throw err;
        }
        return models.Group.getIdFromName(req.body.group); // Promise is rejected if no group with given name.
      })
      .then(function(groupId) {
        if (!groupId) {  //Throw error if the group doesn't exist.
          var err = new Error("Can't find group with given name: " + req.body.group);
          err.invalidRequest = true;
          throw err;
        }<commit_msg>Added API docs for Devices.<commit_after>var models = require('../../models');
var util = require('./util');
var jwt = require('jsonwebtoken');
var config = require('../../config/config');
var responseUtil = require('./responseUtil');

module.exports = function(app, baseUrl) {
  var apiUrl = baseUrl + '/devices';

  /**
   * @api {post} /api/v1/users Register a new device.
   * @apiName RegisterDevice
   * @apiGroup Device
   *
   * @apiParam {String} devicename Unique device name.
   * @apiParam {String} password Password for the device.
   * @apiParam {String} group Group to assign the device to.
   *
   * @apiUse V1ResponseSuccess
   * @apiSuccess {String} token JWT for authentication. Contains the device ID and type.
   *
   * @apiUse V1ResponseError
   */
  app.post(apiUrl, function(req, res) {
    // Check that required data is given.
    if (!req.body.devicename || !req.body.password || !req.body.group) {
      return responseUtil.send(res, {
        statusCode: 400,
        success: false,
        messages: ['Missing devicename or password or group.']
      });
    }

    // Checks that devicename is free, group exists, creates device
    // then responds with a device JWT.
    models.Device.freeDevicename(req.body.devicename)
      .then(function(result) {
        if (!result) {  // Throw error if devicename is allready used.
          var err = new Error('Devicename in use.');
          err.invalidRequest = true;
          throw err;
        }
        return models.Group.getIdFromName(req.body.group); // Promise is rejected if no group with given name.
      })
      .then(function(groupId) {
        if (!groupId) {  //Throw error if the group doesn't exist.
          var err = new Error("Can't find group with given name: " + req.body.group);
          err.invalidRequest = true;
          throw err;
        }
{'fraction_non_alphanumeric': 0.12813370473537605, 'fraction_numerical': 0.002785515320334262, 'mean_word_length': 2.516866158868335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7343131', 'n_tokens_mistral': 948, 'n_tokens_neox': 926, 'n_words': 291}