const utils = require('../lib/utils')
const insecurity = require('../lib/insecurity')
const safeEval = require('notevil')
const vm = require('vm')
const challenges = require('../data/datacache').challenges

exports = module.exports = function b2bOrder () {
  return (req, res, next) => {
    const orderLinesData = req.body.orderLinesData || ''
    try {
      const sandbox = { safeEval, orderLinesData }
      vm.createContext(sandbox)
      vm.runInContext('safeEval(orderLinesData)', sandbox, { timeout: 2000 })
    } catch (err) {
      if (err.message === 'Infinite loop detected - reached max iterations') {
        if (utils.notSolved(challenges.rceChallenge)) {
          utils.solve(challenges.rceChallenge)
        }
        next(err)
      } else if (err.message === 'Script execution timed out.') {
        if (utils.notSolved(challenges.rceOccupyChallenge)) {
          utils.solve(challenges.rceOccupyChallenge)
        }
        res.status(503)
        next(new Error('Sorry, we are temporarily not available! Please try again later.'))
      }
    }
    res.json({ cid: req.body.cid, orderNo: uniqueOrderNumber(), paymentDue: dateTwoWeeksFromNow() })
  }

  function uniqueOrderNumber () {
    return insecurity.hash(new Date() + '_B2B')
  }

  function dateTwoWeeksFromNow () {
    return new Date(new Date().getTime() + (14 * 24 * 60 * 60 * 1000)).toISOString()
  }
}

@@ -517,247 +517,133 @@
     
-%7D catch (err) %7B%0A      if (err.message === 'Infinite loop detected - reached max iterations') %7B%0A        if (utils.notSolved(challenges.rceChallenge)) %7B%0A          utils.solve(challenges.rceChallenge)%0A       
+  res.json(%7B cid: req.body.cid, orderNo: uniqueOrderNumber(), paymentDue: dateTwoWeeksFromNow()
  %7D
+)
 %0A    
-    next
+%7D catch 
 (err)
+ %7B
 %0A      
-%7D else 
 if (
@@ -943,115 +943,229 @@
    %7D
-%0A
+ else %7B%0A 
     
-%7D%0A
    
- res.json(%7B cid: req.body.cid, orderNo: uniqueOrderNumber(), paymentDue: dateTwoWeeksFromNow() %7D)
+if (utils.notSolved(challenges.rceChallenge) && err.message === 'Infinite loop detected - reached max iterations') %7B%0A          utils.solve(challenges.rceChallenge)%0A        %7D%0A        next(err)%0A      %7D%0A    %7D
 %0A  %7D

Fix response being closed prematurely (and reorder condition checks for more clarity)