async (req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> => {
        const form: Form<CCJPaymentOption> = req.body

        if (form.hasErrors()) {
          res.render(Paths.paymentOptionsPage.associatedView, { form: form })
        } else {
          const draft: Draft<DraftCCJ> = res.locals.ccjDraft
          const user: User = res.locals.user

          draft.document.paymentOption = form.model
          if (form.model.option === PaymentType.IMMEDIATELY) {
            draft.document.repaymentPlan = undefined
            draft.document.payBySetDate = undefined
          }
          await new DraftService().save(draft, user.bearerToken)

          const { externalId } = req.params

          switch (form.model.option) {
            case PaymentType.IMMEDIATELY:
              res.redirect(Paths.checkAndSendPage.evaluateUri({ externalId: externalId }))
              break
            case PaymentType.BY_SPECIFIED_DATE:
              res.redirect(Paths.payBySetDatePage.evaluateUri({ externalId: externalId }))
              break
            case PaymentType.INSTALMENTS:
              res.redirect(Paths.repaymentPlanPage.evaluateUri({ externalId: externalId }))
              break
          }
        }
      }))
 it('should redirect to pay by set date page for "BY_SPECIFIED_DATE" option selected', async () => {
   await checkThatSelectedPaymentOptionRedirectsToPage({ option: PaymentType.BY_SPECIFIED_DATE.value }, Paths.payBySetDatePage.evaluateUri({ externalId: externalId }))
 })
import { Paths } from 'ccj/paths'
import { app } from 'main/app'

import * as idamServiceMock from 'test/http-mocks/idam'
import * as claimStoreServiceMock from 'test/http-mocks/claim-store'
import * as draftStoreServiceMock from 'test/http-mocks/draft-store'
import { checkAuthorizationGuards } from 'test/features/ccj/routes/checks/authorization-check'
import { checkNotClaimantInCaseGuard } from 'test/features/ccj/routes/checks/not-claimant-in-case-check'
import * as moment from 'moment'
import { MomentFactory } from 'shared/momentFactory'

const externalId = claimStoreServiceMock.sampleClaimObj.externalId

const cookieName: string = config.get<string>('session.cookieName')
const pagePath: string = Paths.payBySetDatePage.evaluateUri({ externalId : externalId })
const checkAndSavePage: string = Paths.checkAndSendPage.evaluateUri({ externalId : externalId })
const checkContent: string = 'When do you want the defendant to pay?'

describe('CCJ - Pay by set date', () => {
  attachDefaultHooks(app)

  describe('on GET', () => {
    const method = 'get'
    checkAuthorizationGuards(app, method, pagePath)
    checkNotClaimantInCaseGuard(app, method, pagePath)

    context('when user authorised', () => {
      beforeEach(() => {
        idamServiceMock.resolveRetrieveUserFor('1', 'citizen')
      })