Skip to content

Commit

Permalink
CIV-14636 CUI R2 - View and Response link not working intermittently (#…
Browse files Browse the repository at this point in the history
…4196)

* CIV-14636 test pr

* CIV-14636 clearing cache after fee breakup controller

* CIV-14363 reverted temp change

* CIV-14636 Added redis logic for claimantIntent guard

* CIV-14636 Removed redis cache in claimant Intent Guard

* CIV-14636 Updated CLaimantIntent guard for redis cache

* CIV-14636 fixed unit tests

* CIV-14636 Fixed formatting issue

---------

Co-authored-by: Paul Pearson <[email protected]>
Co-authored-by: Raja Mani <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent f7cf956 commit d79ea3d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
54 changes: 30 additions & 24 deletions src/main/routes/guards/claimantIntentGuard.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
36 changes: 30 additions & 6 deletions src/test/unit/routes/guards/claimantIntentGuard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,76 @@ 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<Request>;
let res: Partial<Response> & { redirect: jest.Mock };
let next: jest.Mock;

const caseStoreData: Partial<Claim> = {
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<Claim> = {
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<Claim> = {
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<Claim> = {
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,
);
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);
});
Expand Down

0 comments on commit d79ea3d

Please sign in to comment.