<commit_before>const getBreadcrumbs = require('./get-breadcrumbs')
const getOrganisationUnit = require('./helpers/org-unit-finder')
const getIndividualOverview = require('./data/get-individual-overview')
const getOrganisationOverview = require('./data/get-organisation-overview')
const calculateContractedHours = require('wmt-probation-rules').calculateContractedHours
const DefaultContractedHours = require('wmt-probation-rules').DefaultContractedHours
const orgUnit = require('../constants/organisation-unit')

module.exports = function (id, organisationLevel) {
  var result = {}
  var overviewPromise = {}
  var organisationalUnitType = getOrganisationUnit('name', organisationLevel)

  if (organisationLevel === orgUnit.OFFENDER_MANAGER.name) {
    overviewPromise = getIndividualOverview(id, organisationLevel)
  } else {
    overviewPromise = getOrganisationOverview(id, organisationLevel)
  }

  return overviewPromise.then(function (results) {
    result.overviewDetails = calculateCapacityPercentage(results)
    result.breadcrumbs = getBreadcrumbs(id, organisationLevel)
    result.title = result.breadcrumbs[0].title
    result.subTitle = organisationalUnitType.displayText
    return result
  })
}

var calculateCapacityPercentage = function (results) {
  if (results.length !== undefined) {
    results.forEach(function (result) {
      result.capacityPercentage = (result.totalPoints / result.availablePoints) * 100
      result.contractedHours = getContractedHours(result)
    })
    return results
  } else {
    var capacityPercentage = (results.totalPoints / results.availablePoints) * 100
    results.contractedHours = getContractedHours(results)
    return Object.assign({}, results, {capacity: capacityPercentage})
  }
}

var getContractedHours = function (result) {
  var defaultContractedHours = new DefaultContractedHours(result.defaultContractedHoursPo, result.defaultContractedHoursPso)
  var resultHours = calculateContractedHours(result.contractedHours, defaultContractedHours)
  if (resultHours.baseHours !== null) {
    return resultHours.baseHours
  } else {
    return resultHours.defaultContractedHoursForBand
  }
}<commit_msg>787: Add check on contracted hours if it is 0, to use the default and calculate<commit_after>const getBreadcrumbs = require('./get-breadcrumbs')
const getOrganisationUnit = require('./helpers/org-unit-finder')
const getIndividualOverview = require('./data/get-individual-overview')
const getOrganisationOverview = require('./data/get-organisation-overview')
const calculateContractedHours = require('wmt-probation-rules').calculateContractedHours
const DefaultContractedHours = require('wmt-probation-rules').DefaultContractedHours
const orgUnit = require('../constants/organisation-unit')

module.exports = function (id, organisationLevel) {
  var result = {}
  var overviewPromise = {}
  var organisationalUnitType = getOrganisationUnit('name', organisationLevel)

  if (organisationLevel === orgUnit.OFFENDER_MANAGER.name) {
    overviewPromise = getIndividualOverview(id, organisationLevel)
  } else {
    overviewPromise = getOrganisationOverview(id, organisationLevel)
  }

  return overviewPromise.then(function (results) {
    result.overviewDetails = calculateCapacityPercentage(results)
    result.breadcrumbs = getBreadcrumbs(id, organisationLevel)
    result.title = result.breadcrumbs[0].title
    result.subTitle = organisationalUnitType.displayText
    return result
  })
}

var calculateCapacityPercentage = function (results) {
  if (results.length !== undefined) {
    results.forEach(function (result) {
      result.capacityPercentage = (result.totalPoints / result.availablePoints) * 100
      if (result.contractedHours === 0) {
        result.contractedHours = getContractedHours(result)
      }
    })
    return results
  } else {
    var capacityPercentage = (results.totalPoints / results.availablePoints) * 100
    if (results.contractedHours === 0) {
      results.contractedHours = getContractedHours(results)
    }
    return Object.assign({}, results, {capacity: capacityPercentage})
  }
}

var getContractedHours = function (result) {
  var defaultContractedHours = new DefaultContractedHours(result.defaultContractedHoursPo, result.defaultContractedHoursPso)
  var resultHours = calculateContractedHours(result.contractedHours, defaultContractedHours)
  if (resultHours.baseHours !== null) {
    return resultHours.baseHours
  } else {
    return resultHours.defaultContractedHoursForBand
  }
}
{'fraction_non_alphanumeric': 0.09890844286032524, 'fraction_numerical': 0.0044553352639786145, 'mean_word_length': 5.288515406162465, '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': '6172999', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1249, 'n_words': 237}