{"size": 1025, "ext": "js", "max_stars_repo_path": "app/middleware/redirectMiddleware.js", "max_stars_repo_name": "uk-gov-mirror/hmcts.div-petitioner-frontend", "max_stars_repo_stars_event_min_datetime": "2018-06-13T10:34:54.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-10T16:19:28.000Z", "max_issues_repo_path": "app/middleware/redirectMiddleware.js", "max_issues_repo_name": "uk-gov-mirror/hmcts.div-petitioner-frontend", "max_issues_repo_issues_event_min_datetime": "2018-03-08T14:52:20.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T15:36:22.000Z", "max_forks_repo_path": "app/middleware/redirectMiddleware.js", "max_forks_repo_name": "uk-gov-mirror/hmcts.div-petitioner-frontend", "max_forks_repo_forks_event_min_datetime": "2018-03-07T10:15:56.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-10T22:33:41.000Z"}
{"max_stars_count": 3, "max_issues_count": 457, "max_forks_count": 12, "avg_line_length": 41.0, "max_line_length": 120, "alphanum_fraction": 0.6917073171}
const CONF = require('config');
const _ = require('lodash');
const logger = require('app/services/logger').logger(__filename);

const authTokenString = '__auth-token';

const redirectOnCondition = (req, res, next) => {
  const session = req.session;
  const caseState = _.get(session, 'state');
  const courtId = _.get(session, 'allocatedCourt.courtId', _.get(session, 'courts'));
  const caseId = _.get(session, 'caseId');

  logger.infoWithReq(req, 'PFE redirect check', `Case Ref: ${caseId}. Case State: ${caseState}. Court ID: ${courtId}.`);
  if (caseState && CONF.ccd.courts.includes(courtId) && !CONF.ccd.d8States.includes(caseState)) {
    logger.infoWithReq(req, 'PFE redirecting to DN', `Case Ref: ${caseId}. Redirect check Passed.`);
    const appLandingPage = `${CONF.apps.dn.url}${CONF.apps.dn.landing}`;
    const queryString = `?${authTokenString}=${req.cookies[authTokenString]}`;
    return res.redirect(`${appLandingPage}${queryString}`);
  }

  return next();
};

module.exports = { redirectOnCondition };
