diff --git a/src/main/routes/guards/claimantIntentGuard.ts b/src/main/routes/guards/claimantIntentGuard.ts index 6bb6d33b539..be905c9cf40 100644 --- a/src/main/routes/guards/claimantIntentGuard.ts +++ b/src/main/routes/guards/claimantIntentGuard.ts @@ -1,28 +1,34 @@ -import { NextFunction, Request, Response } from 'express'; -import { Claim } from '../../common/models/claim'; +import { NextFunction,Request, Response } from 'express'; import { constructResponseUrlWithIdParams } from '../../common/utils/urlFormatter'; import { DASHBOARD_CLAIMANT_URL } from '../../routes/urls'; import { getClaimById } from 'modules/utilityService'; -export const claimantIntentGuard = ( - req: Request, - res: Response, - next: NextFunction, -) => { - (async () => { - try { - const caseData: Claim = await getClaimById(req.params.id, req, true); - if (caseData.isClaimantIntentionPending() || req.originalUrl.includes('claimant-response/confirmation')) { - next(); - } else { - res.redirect( - constructResponseUrlWithIdParams( - req.params.id, - DASHBOARD_CLAIMANT_URL, - ), - ); - } - } catch (error) { - next(error); +import {deleteDraftClaimFromStore, generateRedisKey, getCaseDataFromStore } from 'modules/draft-store/draftStoreService'; +import { AppRequest } from 'common/models/AppRequest'; +import { Claim } from 'common/models/claim'; + +export const claimantIntentGuard = (async (req: Request, res: Response, next: NextFunction) => { + try { + const redisClaimId = generateRedisKey(req as AppRequest); + const caseStoreData: Claim = await getCaseDataFromStore(redisClaimId, true); + + // Delete cache from redis if case state is not AWAITING_APPLICANT_INTENTION. + // Giving a chance to reload data from ccd database. + if(caseStoreData && !caseStoreData.isEmpty() && !caseStoreData.isClaimantIntentionPending()) { + await deleteDraftClaimFromStore(redisClaimId); } - })(); -}; + + const caseData = await getClaimById(req.params.id, req, true); + if (caseData.isClaimantIntentionPending() || req.originalUrl.includes('claimant-response/confirmation')) { + next(); + } else { + res.redirect( + constructResponseUrlWithIdParams( + req.params.id, + DASHBOARD_CLAIMANT_URL, + ), + ); + } + } catch (error) { + next(error); + } +}); diff --git a/src/test/unit/routes/features/claimantResponse/repaymentPlanAcceptedController.test.ts b/src/test/unit/routes/features/claimantResponse/repaymentPlanAcceptedController.test.ts index 729d61fb77e..83fbf0f786d 100644 --- a/src/test/unit/routes/features/claimantResponse/repaymentPlanAcceptedController.test.ts +++ b/src/test/unit/routes/features/claimantResponse/repaymentPlanAcceptedController.test.ts @@ -10,6 +10,13 @@ jest.mock('modules/utilityService', () => ({ getClaimById: jest.fn().mockResolvedValue({ isClaimantIntentionPending: () => true }), getRedisStoreForSession: jest.fn(), })); + +jest.mock('modules/draft-store/draftStoreService', () => ({ + getCaseDataFromStore: jest.fn().mockResolvedValue({ isClaimantIntentionPending: () => true,isEmpty: jest.fn().mockReturnValue(true) }), + deleteDraftClaimFromStore: jest.fn(), + generateRedisKey:jest.fn, +})); + describe('Claimant Response - Rejection reason', () => { const citizenRoleToken: string = config.get('citizenRoleToken'); const idamUrl: string = config.get('idamUrl'); diff --git a/src/test/unit/routes/features/claimantResponse/signSettlmentAgreementController.test.ts b/src/test/unit/routes/features/claimantResponse/signSettlmentAgreementController.test.ts index 0c7845c4855..7d8cf0e5862 100644 --- a/src/test/unit/routes/features/claimantResponse/signSettlmentAgreementController.test.ts +++ b/src/test/unit/routes/features/claimantResponse/signSettlmentAgreementController.test.ts @@ -105,6 +105,7 @@ describe('Sign Settlement Agreement', () => { app.locals.draftStoreClient = { set: jest.fn(() => Promise.resolve({})), get: jest.fn(() => Promise.resolve(JSON.stringify(civilClaimResponseMock))), + del: jest.fn(() => Promise.resolve({})), }; await request(app).get(CLAIMANT_SIGN_SETTLEMENT_AGREEMENT).expect((res) => { diff --git a/src/test/unit/routes/guards/claimantIntentGuard.test.ts b/src/test/unit/routes/guards/claimantIntentGuard.test.ts index 723125e43c1..6e4f8f109fc 100644 --- a/src/test/unit/routes/guards/claimantIntentGuard.test.ts +++ b/src/test/unit/routes/guards/claimantIntentGuard.test.ts @@ -4,42 +4,63 @@ import { Claim } from 'common/models/claim'; import { getClaimById } from 'modules/utilityService'; import { constructResponseUrlWithIdParams } from 'common/utils/urlFormatter'; import { CLAIMANT_RESPONSE_CONFIRMATION_URL, DASHBOARD_CLAIMANT_URL } from 'routes/urls'; -jest.mock('modules/utilityService', () => ({ +import { getCaseDataFromStore } from 'modules/draft-store/draftStoreService'; + +jest.mock('../../../../main/modules/utilityService', () => ({ getClaimById: jest.fn(), + getRedisStoreForSession: jest.fn(), })); +jest.mock('../../../../main/modules/draft-store/draftStoreService', () => ({ + getCaseDataFromStore: jest.fn(), + deleteDraftClaimFromStore: jest.fn(), + generateRedisKey:jest.fn, +})); + describe('claimantIntentGuard', () => { let req: Partial; let res: Partial & { redirect: jest.Mock }; let next: jest.Mock; + + const caseStoreData: Partial = { + isClaimantIntentionPending: jest.fn().mockReturnValue(true), + isEmpty: jest.fn().mockReturnValue(true), + }; + beforeEach(() => { req = {params: {id: '123'}, originalUrl: 'test' }; res = { redirect: jest.fn() }; next = jest.fn(); }); + it('should call next if isClaimantIntentionPending returns true', async () => { const claim: Partial = { isClaimantIntentionPending: jest.fn().mockReturnValue(true), }; - (getClaimById as jest.Mock).mockResolvedValueOnce(claim as Claim); + (getCaseDataFromStore as jest.Mock).mockResolvedValue(caseStoreData as Claim); + (getClaimById as jest.Mock).mockResolvedValue(claim as Claim); await claimantIntentGuard(req as Request, res as Response, next); expect(next).toHaveBeenCalled(); expect(res.redirect).not.toHaveBeenCalled(); }); + it('should call next if isClaimantIntentionPending returns false but it`s claimant response confirmation page', async () => { const claim: Partial = { isClaimantIntentionPending: jest.fn().mockReturnValue(false), }; - (getClaimById as jest.Mock).mockResolvedValueOnce(claim as Claim); + (getClaimById as jest.Mock).mockResolvedValue(claim as Claim); req.originalUrl = CLAIMANT_RESPONSE_CONFIRMATION_URL; await claimantIntentGuard(req as Request, res as Response, next); expect(next).toHaveBeenCalled(); expect(res.redirect).not.toHaveBeenCalled(); }); + it('should redirect if isClaimantIntentionPending returns false', async () => { - const claim = { + const claim : Partial = { isClaimantIntentionPending: jest.fn().mockReturnValue(false), }; - (getClaimById as jest.Mock).mockResolvedValueOnce(claim); + + (getCaseDataFromStore as jest.Mock).mockResolvedValue(caseStoreData as Claim); + (getClaimById as jest.Mock).mockResolvedValue(claim as Claim); const redirectUrl = constructResponseUrlWithIdParams( req.params.id, DASHBOARD_CLAIMANT_URL, @@ -47,9 +68,12 @@ describe('claimantIntentGuard', () => { await claimantIntentGuard(req as Request, res as Response, next); expect(res.redirect).toHaveBeenCalledWith(redirectUrl); }); + it('should pass the error to next if there is an exception', async () => { const error = new Error('Test error'); - (getClaimById as jest.Mock).mockRejectedValueOnce(error); + + (getCaseDataFromStore as jest.Mock).mockResolvedValue(caseStoreData as Claim); + (getClaimById as jest.Mock).mockRejectedValue(error); await claimantIntentGuard(req as Request, res as Response, next); expect(next).toHaveBeenCalledWith(error); });