<commit_before>const { assign, find, get } = require('lodash')

const { getOptions } = require('../../../lib/options')
const { hqLabels } = require('../labels')
const companyFormService = require('../services/form')
const { transformCompanyToForm } = require('../transformers')

function getHeadquarterOptions (token) {
  return getOptions(token, 'headquarter-type')
    .then((options) => {
      const headquarterOptions = options.map(option => {
        return {
          value: option.value,
          label: hqLabels[option.label],
        }
      })

      headquarterOptions.unshift({
        value: 'not_headquarters',
        label: 'Not a headquarters',
      })

      return headquarterOptions
    })
}

async function getCompanyFormOptions (token, createdOn) {
  return {
    headquarters: await getHeadquarterOptions(token),
    regions: await getOptions(token, 'uk-region', { createdOn }),
    sectors: await getOptions(token, 'sector', { createdOn }),
    employees: await getOptions(token, 'employee-range', { createdOn, sorted: false }),
    turnovers: await getOptions(token, 'turnover', { createdOn, sorted: false }),
    countries: await getOptions(token, 'country', { createdOn }),
  }
}

async function populateForm (req, res, next) {
  try {
    const token = req.session.token
    const createdOn = get(res.locals, 'company.created_on')

    const defaultCompanyData = transformCompanyToForm(res.locals.companiesHouseRecord || res.locals.company)
    const formData = assign({}, defaultCompanyData, req.body)

    if (get(req.query, 'business_type')) {
      formData.business_type = req.query.business_type
    }

    if (!get(formData.headquarter_type)) {
      formData.headquarter_type = 'not_headquarters'
    }

    res.locals = assign({}, res.locals, {
      formData,
      options: await getCompanyFormOptions(token, createdOn),
    })

    next()
  } catch (error) {
    next(error)
  }
}

async function handleFormPost (req, res, next) {
  try {
    const token = req.session.token
    const countryQueryParam = get(req.query, 'country')

    if (get(req.body, 'headquarter_type') === 'not_headquarters') {
      req.body.headquarter_type = ''
    }

    if (countryQueryParam && countryQueryParam === 'uk') {<commit_msg>Move getCompanyFormOptions to top of try block<commit_after>const { assign, find, get } = require('lodash')

const { getOptions } = require('../../../lib/options')
const { hqLabels } = require('../labels')
const companyFormService = require('../services/form')
const { transformCompanyToForm } = require('../transformers')

function getHeadquarterOptions (token) {
  return getOptions(token, 'headquarter-type')
    .then((options) => {
      const headquarterOptions = options.map(option => {
        return {
          value: option.value,
          label: hqLabels[option.label],
        }
      })

      headquarterOptions.unshift({
        value: 'not_headquarters',
        label: 'Not a headquarters',
      })

      return headquarterOptions
    })
}

async function getCompanyFormOptions (token, createdOn) {
  return {
    headquarters: await getHeadquarterOptions(token),
    regions: await getOptions(token, 'uk-region', { createdOn }),
    sectors: await getOptions(token, 'sector', { createdOn }),
    employees: await getOptions(token, 'employee-range', { createdOn, sorted: false }),
    turnovers: await getOptions(token, 'turnover', { createdOn, sorted: false }),
    countries: await getOptions(token, 'country', { createdOn }),
  }
}

async function populateForm (req, res, next) {
  try {
    const token = req.session.token
    const createdOn = get(res.locals, 'company.created_on')
    const options = await getCompanyFormOptions(token, createdOn)

    const defaultCompanyData = transformCompanyToForm(res.locals.companiesHouseRecord || res.locals.company)
    const formData = assign({}, defaultCompanyData, req.body)

    if (get(req.query, 'business_type')) {
      formData.business_type = req.query.business_type
    }

    if (!get(formData.headquarter_type)) {
      formData.headquarter_type = 'not_headquarters'
    }

    res.locals = assign({}, res.locals, {
      formData,
      options,
    })

    next()
  } catch (error) {
    next(error)
  }
}

async function handleFormPost (req, res, next) {
  try {
    const token = req.session.token
    const countryQueryParam = get(req.query, 'country')

    if (get(req.body, 'headquarter_type') === 'not_headquarters') {
      req.body.headquarter_type = ''
    }

    if (countryQueryParam && countryQueryParam === 'uk') {
{'fraction_non_alphanumeric': 0.13485113835376533, 'fraction_numerical': 0.0, 'mean_word_length': 3.4445525291828796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4923073', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1350, 'n_words': 300}