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

const { updateInvestment } = require('../../repos')
const { requirementsFormConfig } = require('../../macros')
const { getOptions } = require('../../../../lib/options')
const { buildFormWithStateAndErrors } = require('../../../builders')

async function getFormOptions (token, createdOn) {
  return {
    strategicDrivers: await getOptions(token, 'investment-strategic-driver', { createdOn }),
    countries: await getOptions(token, 'country', { createdOn }),
    ukRegions: await getOptions(token, 'uk-region', { createdOn }),
  }
}

async function populateForm (req, res, next) {
  try {
    const investmentId = req.params.investmentId
    const investmentData = res.locals.investmentData
    const createdOn = get(res.locals.investmentData, 'created_on')
    const options = await getFormOptions(req.session.token, createdOn)
    const body = res.locals.formattedBody || investmentData

    res.locals.requirementsForm = assign({},
      buildFormWithStateAndErrors(requirementsFormConfig(options), body, res.locals.errors),
      { returnLink: `/investment-projects/${investmentId}` },
    )
  } catch (error) {
    return next(error)
  }

  next()
}

// Strips out empty entries so they are not posted (which result in errors)
// and when an error is thrown it doesn't mean yet another is added
function cleanArray (values) {
  return flatten([values])
    .filter(item => item)
}

async function handleFormPost (req, res, next) {
  try {
    const investmentId = req.params.investmentId

    res.locals.formattedBody = assign({}, req.body, {
      strategic_drivers: cleanArray(req.body.strategic_drivers),
      competitor_countries: req.body.client_considering_other_countries === 'true' ? cleanArray(req.body.competitor_countries) : [],
      uk_region_locations: cleanArray(req.body.uk_region_locations),
    })

    // if called with the add item instruction, simply re-render the form and it will add extra fields as needed
    if (req.body.add_item) {
      return next()
    }

    await updateInvestment(req.session.token, investmentId, res.locals.formattedBody)
    req.flash('success', 'Investment requirements updated')
    res.redirect(`/investment-projects/${investmentId}/details`)
  } catch (err) {
    if (err.statusCode === 400) {
      res.locals.errors = err.error
      next()
    } else {
      next(err)
    }
  }
}

module.exports = {
  populateForm,
  handleFormPost,
}<commit_msg>Add landed regions to invest requirements form handler<commit_after>const { assign, flatten, get } = require('lodash')

const { updateInvestment } = require('../../repos')
const { requirementsFormConfig } = require('../../macros')
const { getOptions } = require('../../../../lib/options')
const { buildFormWithStateAndErrors } = require('../../../builders')

async function getFormOptions (token, createdOn) {
  return {
    strategicDrivers: await getOptions(token, 'investment-strategic-driver', { createdOn }),
    countries: await getOptions(token, 'country', { createdOn }),
    ukRegions: await getOptions(token, 'uk-region', { createdOn }),
  }
}

async function populateForm (req, res, next) {
  try {
    const investmentId = req.params.investmentId
    const investmentData = res.locals.investmentData
    const createdOn = get(res.locals.investmentData, 'created_on')
    const options = await getFormOptions(req.session.token, createdOn)
    const body = res.locals.formattedBody || investmentData

    res.locals.requirementsForm = assign({},
      buildFormWithStateAndErrors(requirementsFormConfig(options), body, res.locals.errors),
      { returnLink: `/investment-projects/${investmentId}` },
    )
  } catch (error) {
    return next(error)
  }

  next()
}

// Strips out empty entries so they are not posted (which result in errors)
// and when an error is thrown it doesn't mean yet another is added
function cleanArray (values) {
  return flatten([values])
    .filter(item => item)
}

async function handleFormPost (req, res, next) {
  try {
    const investmentId = req.params.investmentId

    res.locals.formattedBody = assign({}, req.body, {
      strategic_drivers: cleanArray(req.body.strategic_drivers),
      competitor_countries: req.body.client_considering_other_countries === 'true' ? cleanArray(req.body.competitor_countries) : [],
      uk_region_locations: cleanArray(req.body.uk_region_locations),
      actual_uk_regions: req.body.site_decided === 'true' ? cleanArray(req.body.actual_uk_regions) : [],
    })

    // if called with the add item instruction, simply re-render the form and it will add extra fields as needed
    if (req.body.add_item) {
      return next()
    }

    await updateInvestment(req.session.token, investmentId, res.locals.formattedBody)
    req.flash('success', 'Investment requirements updated')
    res.redirect(`/investment-projects/${investmentId}/details`)
  } catch (err) {
    if (err.statusCode === 400) {
      res.locals.errors = err.error
      next()
    } else {
      next(err)
    }
  }
}

module.exports = {
  populateForm,
  handleFormPost,
}
{'fraction_non_alphanumeric': 0.13023893458676067, 'fraction_numerical': 0.0011750881316098707, 'mean_word_length': 3.763992537313433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2559431', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1539, 'n_words': 361}