<commit_before>/* global IrcEntryEventValidatorService Event Detainee */

'use strict';

var ValidationError = require('../lib/exceptions/ValidationError');

const updateDetaineeModel = (detainee, newDetaineeProperties) => {
  detainee.timestamp = newDetaineeProperties.timestamp;

  if (newDetaineeProperties.gender) {
    detainee.gender = newDetaineeProperties.gender;
  }
  if (newDetaineeProperties.nationality) {
    detainee.nationality = newDetaineeProperties.nationality;
  }
  if (newDetaineeProperties.cid_id) {
    detainee.cid_id = newDetaineeProperties.cid_id;
  }
};

let generateStandardEvent = (detainee, request_body) => {
  return {
    centre: detainee.centre,
    detainee: detainee,
    operation: request_body.operation,
    timestamp: request_body.timestamp
  };
};

let generateDetainee = (centre, request_body) => {
  return {
    centre: centre,
    person_id: request_body.person_id,
    timestamp: request_body.timestamp,
    nationality: request_body.nationality,
    gender: Detainee.normalizeGender(request_body.gender),
    cid_id: request_body.cid_id
  };
};

let createOrUpdateDetainee = (detaineeProperties) => {
  return Detainee.findOne({
    person_id: detaineeProperties.person_id,
    centre: detaineeProperties.centre.id
  }).then((detainee) => {
    if (!detainee) {
      return Detainee.create(detaineeProperties);
    } else if (detainee.timestamp.toISOString() < detaineeProperties.timestamp) {
      updateDetaineeModel(detainee, detaineeProperties);
      return detainee.save();
    }
    return detainee;
  });
};

let processEventDetainee = (request_body) => {
  return Centres.findOne({ name: request_body.centre })
    .then((centre) => createOrUpdateDetainee(generateDetainee(centre, request_body)));
};

module.exports = {
  _config: {
    actions: false,
    shortcuts: false,
    rest: false,
    exposedMethods: [
      'heartbeat',
      'event'
    ]
  },

  heartbeatOptions: (req, res) =>
    res.ok(IrcEntryHeartbeatValidatorService.schema),
  eventOptions: (req, res) =>
    res.ok(IrcEntryEventValidatorService.schema),

  index: (req, res) => res.ok,

  process_heartbeat: (request_body) =>
    Centres.update(<commit_msg>IBM-116 Reformatted some functions to remove unnecessary returns and braces<commit_after>/* global IrcEntryEventValidatorService Event Detainee */

'use strict';

var ValidationError = require('../lib/exceptions/ValidationError');

const updateDetaineeModel = (detainee, newDetaineeProperties) => {
  detainee.timestamp = newDetaineeProperties.timestamp;

  if (newDetaineeProperties.gender) {
    detainee.gender = newDetaineeProperties.gender;
  }
  if (newDetaineeProperties.nationality) {
    detainee.nationality = newDetaineeProperties.nationality;
  }
  if (newDetaineeProperties.cid_id) {
    detainee.cid_id = newDetaineeProperties.cid_id;
  }
};

let generateStandardEvent = (detainee, request_body) =>
  ({
    centre: detainee.centre,
    detainee: detainee,
    operation: request_body.operation,
    timestamp: request_body.timestamp
  });

let generateDetainee = (centre, request_body) =>
  ({
    centre: centre,
    person_id: request_body.person_id,
    timestamp: request_body.timestamp,
    nationality: request_body.nationality,
    gender: Detainee.normalizeGender(request_body.gender),
    cid_id: request_body.cid_id
  });

let createOrUpdateDetainee = (detaineeProperties) =>
  Detainee.findOne({
    person_id: detaineeProperties.person_id,
    centre: detaineeProperties.centre.id
  }).then((detainee) => {
    if (!detainee) {
      return Detainee.create(detaineeProperties);
    } else if (detainee.timestamp.toISOString() < detaineeProperties.timestamp) {
      updateDetaineeModel(detainee, detaineeProperties);
      return detainee.save();
    }
    return detainee;
  });

let processEventDetainee = (request_body) =>
  Centres.findOne({ name: request_body.centre })
    .then((centre) => createOrUpdateDetainee(generateDetainee(centre, request_body)));

module.exports = {
  _config: {
    actions: false,
    shortcuts: false,
    rest: false,
    exposedMethods: [
      'heartbeat',
      'event'
    ]
  },

  heartbeatOptions: (req, res) =>
    res.ok(IrcEntryHeartbeatValidatorService.schema),
  eventOptions: (req, res) =>
    res.ok(IrcEntryEventValidatorService.schema),

  index: (req, res) => res.ok,

  process_heartbeat: (request_body) =>
    Centres.update(
{'fraction_non_alphanumeric': 0.11533212010919017, 'fraction_numerical': 0.0006824385805277526, 'mean_word_length': 4.291215403128761, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '4026246', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1434, 'n_words': 240}