<commit_before>var log = require('../../logging');
var requestUtil = require('./requestUtil');
var responseUtil = require('./responseUtil');
const uuidv4            = require('uuid/v4');
const multiparty        = require('multiparty');
const config            = require('../../config');
const modelsUtil        = require('../../models/util/util');

var INVALID_DATA = 'Invalid data key in body, should be a JSON.';
var INVALID_ID = 'Invalid ID field. Should be an integer.';
var NO_DATAPOINT_FOUND =
  'No datapoint was found with given ID. make sure that you have permissions ' +
  'to view datapoint.';
var NO_FILE_FOUND = 'No file found with given datapoint.';

function getRecordingsFromModel(modelClass, request, response) {

  // Check that if the request was authenticated that is was
  // authenticated by a user JWT, not a device JWT.
  // TODO get passport to do this...
  if (request.user !== null && !requestUtil.isFromAUser(request))
  {return responseUtil.notFromAUser(response);}

  var query = requestUtil.getSequelizeQueryFromHeaders(request);
  if (query.badRequest)
  {return responseUtil.invalidDatapointGet(response, query.badRequest);}

  modelClass
    .findAllWithUser(request.user, query)
    .then((modelInstances) => {
      var result = {
        rows: [],
        limit: modelInstances.limit,
        offset: modelInstances.offset,
        count: modelInstances.count,
      };
      // Just save the front end fields for each model.
      for (var key in modelInstances.rows)
      {result.rows.push(modelInstances.rows[key].getFrontendFields());}

      return responseUtil.validDatapointGet(response, result);
    })
    .catch((error) => catchError(error, response));
}

/**
 * Processes the POST request checking that the request is valid then saving the
 *  metadata in a sequelize model and uploading the file to the file storage.
 * @param {Object} modelClass - Sequelize Model of the type of the recording.
 * @param {Object} request - Express request object.
 * @param {Object} response - Express response object.
 */
function addRecordingFromPost(model, request, response) {
  var device = request.device;
  var modelClass = model;
  var modelInstance;
  var file;

  if (request.device === null) {
    throw { badRequest: "expected device" };
  }

  requestUtil
    .getFileAndDataField(request)
    .then(([data, f]) => {
      file = f;
      // Build model. But limit what fields can be set.
      modelInstance = modelClass.build(data, {
        fields: modelClass.apiSettableFields
      });

      modelInstance.set('DeviceId', device.id);
      modelInstance.set('GroupId', device.GroupId);
      if (typeof device.public == 'boolean') {
        modelInstance.set('public', device.public);
      } else {
        model.setDataValue('public', false);
      }
      return modelInstance.validate();
    })
    .then(() => modelInstance.save())
    .then(() => responseUtil.validRecordingUpload(response, modelInstance.id))
    .then(() => modelInstance.processRecording(file))
    .then((processedFile) => modelInstance.saveFile(processedFile))
    .catch((error) => {
      catchError(error, response, responseUtil.invalidDatapointUpload);
    });
}

function getRecordingFile(modelClass, request, response) {

  // Check that if the request was authenticated that is was
  // authenticated by a user JWT, not a device JWT.
  // TODO get passport to do this...
  if (request.user !== null && !requestUtil.isFromAUser(request))
  {return responseUtil.notFromAUser(response);}

  var id = parseInt(request.params.id);
  if (!id)
  {return responseUtil.invalidDataId(response);}

  modelClass
    .getFileData(id, request.user)
    .then((fileData) => {
      if (fileData === null || fileData.key === null) {
        responseUtil.invalidFileRequest(response, NO_FILE_FOUND);
        throw { badRequest: NO_FILE_FOUND };
      }
      return responseUtil.validFileRequest(response, {
        _type: 'fileDownload',
        key: fileData.key,
        filename: fileData.name,
        mimeType: fileData.mimeType,
      });
    })
    .catch((error) => {
      catchError(error, response, responseUtil.invalidDatapointGet);
    });
}

function updateDataFromPut(modelClass, request, response) {

  if (!requestUtil.isFromAUser(request)) {
    return responseUtil.notFromAUser(response);
  }

  var data = parseJsonFromString(request.body.data);
  if (data === null) {
    return responseUtil.invalidDatapointUpdate(response, INVALID_DATA);
  }

  var id = parseInt(request.params.id);
  if (!id) {
    return responseUtil.invalidDatapointUpdate(response, INVALID_ID);
  }

  modelClass<commit_msg>Avoid double response when audio recording not found<commit_after>var log = require('../../logging');
var requestUtil = require('./requestUtil');
var responseUtil = require('./responseUtil');
const uuidv4            = require('uuid/v4');
const multiparty        = require('multiparty');
const config            = require('../../config');
const modelsUtil        = require('../../models/util/util');

var INVALID_DATA = 'Invalid data key in body, should be a JSON.';
var INVALID_ID = 'Invalid ID field. Should be an integer.';
var NO_DATAPOINT_FOUND =
  'No datapoint was found with given ID. make sure that you have permissions ' +
  'to view datapoint.';
var NO_FILE_FOUND = 'No file found with given datapoint.';

function getRecordingsFromModel(modelClass, request, response) {

  // Check that if the request was authenticated that is was
  // authenticated by a user JWT, not a device JWT.
  // TODO get passport to do this...
  if (request.user !== null && !requestUtil.isFromAUser(request))
  {return responseUtil.notFromAUser(response);}

  var query = requestUtil.getSequelizeQueryFromHeaders(request);
  if (query.badRequest)
  {return responseUtil.invalidDatapointGet(response, query.badRequest);}

  modelClass
    .findAllWithUser(request.user, query)
    .then((modelInstances) => {
      var result = {
        rows: [],
        limit: modelInstances.limit,
        offset: modelInstances.offset,
        count: modelInstances.count,
      };
      // Just save the front end fields for each model.
      for (var key in modelInstances.rows)
      {result.rows.push(modelInstances.rows[key].getFrontendFields());}

      return responseUtil.validDatapointGet(response, result);
    })
    .catch((error) => catchError(error, response));
}

/**
 * Processes the POST request checking that the request is valid then saving the
 *  metadata in a sequelize model and uploading the file to the file storage.
 * @param {Object} modelClass - Sequelize Model of the type of the recording.
 * @param {Object} request - Express request object.
 * @param {Object} response - Express response object.
 */
function addRecordingFromPost(model, request, response) {
  var device = request.device;
  var modelClass = model;
  var modelInstance;
  var file;

  if (request.device === null) {
    throw { badRequest: "expected device" };
  }

  requestUtil
    .getFileAndDataField(request)
    .then(([data, f]) => {
      file = f;
      // Build model. But limit what fields can be set.
      modelInstance = modelClass.build(data, {
        fields: modelClass.apiSettableFields
      });

      modelInstance.set('DeviceId', device.id);
      modelInstance.set('GroupId', device.GroupId);
      if (typeof device.public == 'boolean') {
        modelInstance.set('public', device.public);
      } else {
        model.setDataValue('public', false);
      }
      return modelInstance.validate();
    })
    .then(() => modelInstance.save())
    .then(() => responseUtil.validRecordingUpload(response, modelInstance.id))
    .then(() => modelInstance.processRecording(file))
    .then((processedFile) => modelInstance.saveFile(processedFile))
    .catch((error) => {
      catchError(error, response, responseUtil.invalidDatapointUpload);
    });
}

function getRecordingFile(modelClass, request, response) {
  // Check that if the request was authenticated that is was
  // authenticated by a user JWT, not a device JWT.
  // TODO get passport to do this...
  if (request.user !== null && !requestUtil.isFromAUser(request)) {
    responseUtil.notFromAUser(response);
    return;
  }

  var id = parseInt(request.params.id);
  if (!id) {
    responseUtil.invalidDataId(response);
    return;
  }

  modelClass
    .getFileData(id, request.user)
    .then((fileData) => {
      if (fileData === null || fileData.key === null) {
        throw { badRequest: NO_FILE_FOUND };
      }
      responseUtil.validFileRequest(response, {
        _type: 'fileDownload',
        key: fileData.key,
        filename: fileData.name,
        mimeType: fileData.mimeType,
      });
    })
    .catch((error) => {
      catchError(error, response, responseUtil.invalidDatapointGet);
    });
}

function updateDataFromPut(modelClass, request, response) {

  if (!requestUtil.isFromAUser(request)) {
    return responseUtil.notFromAUser(response);
  }

  var data = parseJsonFromString(request.body.data);
  if (data === null) {
    return responseUtil.invalidDatapointUpdate(response, INVALID_DATA);
  }

  var id = parseInt(request.params.id);
  if (!id) {
    return responseUtil.invalidDatapointUpdate(response, INVALID_ID);
  }

  modelClass
{'fraction_non_alphanumeric': 0.12358337830545062, 'fraction_numerical': 0.0004317323259579061, 'mean_word_length': 3.3645784267545924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3164659', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2673, 'n_words': 717}