diff --git a/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.test.tsx b/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.test.tsx index ba52dd16a..ea954b610 100644 --- a/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.test.tsx +++ b/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.test.tsx @@ -10,11 +10,15 @@ import { getContext, mockServiceMethod, } from '../../../testUtils/unitTestHelpers'; -import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage'; +import { + checkIfPageHaveAlreadyBeenAnswered, + generateRedirectUrlForMandatoryQuestionNextPage, +} from '../../../utils/mandatoryQuestionUtils'; +import { routes } from '../../../utils/routes'; import getServerSideProps from './getServerSideProps'; jest.mock('next/dist/server/api-utils/node'); -jest.mock('../../../utils/generateRedirectForMandatoryQuestionNextPage'); +jest.mock('../../../utils/mandatoryQuestionUtils'); const spiedGrantMandatoryQuestionServiceGetMandatoryQuestion = jest.spyOn( GrantMandatoryQuestionService.prototype, @@ -51,6 +55,7 @@ describe('getServerSideProps', () => { describe('when handling a GET request', () => { const getDefaultContext = (): Optional => ({ params: { mandatoryQuestionId: 'mandatoryQuestionId' }, + query: {}, }); it('should return the right props', async () => { @@ -102,11 +107,57 @@ describe('getServerSideProps', () => { redirect: { destination: '/service-error?serviceErrorProps={"errorInformation":"Something went wrong while trying to get the page you requested","linkAttributes":{"href":"/testResolvedURL","linkText":"Please return","linkInformation":" and try again."}}', - permanent: false, statusCode: 302, }, }); }); + + it('should redirect to next not answered page if the page we are accessing has already been answered, and the url does not have the fromSummaryPage query param as true', async () => { + mockServiceMethod( + spiedGrantMandatoryQuestionServiceGetMandatoryQuestion, + getDefaultGrantMandatoryQuestion + ); + ( + generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock + ).mockReturnValue('/nextpage'); + (checkIfPageHaveAlreadyBeenAnswered as jest.Mock).mockReturnValue(true); + + const response = await getServerSideProps(getContext(getDefaultContext)); + + expectObjectEquals(response, { + redirect: { + destination: '/nextpage', + statusCode: 302, + }, + }); + }); + it('should not redirect to next not answered page if the page we are accessing has already been answered when the url have the fromSummaryPage query param as true', async () => { + const getDefaultContext = (): Optional => ({ + params: { mandatoryQuestionId: 'mandatoryQuestionId' }, + query: { fromSummaryPage: 'true' }, + }); + + mockServiceMethod( + spiedGrantMandatoryQuestionServiceGetMandatoryQuestion, + getDefaultGrantMandatoryQuestion + ); + ( + generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock + ).mockReturnValue('/nextpage'); + (checkIfPageHaveAlreadyBeenAnswered as jest.Mock).mockReturnValue(true); + + const response = await getServerSideProps(getContext(getDefaultContext)); + + expectObjectEquals(response, { + props: { + csrfToken: 'testCSRFToken', + fieldErrors: [], + formAction: '/testResolvedURL', + defaultFields: getDefaultGrantMandatoryQuestion(), + mandatoryQuestion: getDefaultGrantMandatoryQuestion(), + }, + }); + }); }); describe('when handling a POST request', () => { @@ -115,6 +166,7 @@ describe('getServerSideProps', () => { method: 'POST', }, params: { mandatoryQuestionId: 'mandatoryQuestionId' }, + query: {}, }); beforeEach(() => { @@ -151,6 +203,26 @@ describe('getServerSideProps', () => { }); }); + it('Should redirect to the summary page after successfully updating if query parameter fromSummaryPage is true', async () => { + const getDefaultContext = (): Optional => ({ + req: { + method: 'POST', + }, + params: { mandatoryQuestionId: 'mandatoryQuestionId' }, + query: { fromSummaryPage: 'true' }, + }); + const response = await getServerSideProps(getContext(getDefaultContext)); + + expectObjectEquals(response, { + redirect: { + destination: routes.mandatoryQuestions.summaryPage( + 'mandatoryQuestionId' + ), + statusCode: 302, + }, + }); + }); + it('Should redirect to the error service page if there is an error when updating', async () => { spiedGrantMandatoryQuestionServiceUpdateMandatoryQuestion.mockRejectedValueOnce( 'Error' diff --git a/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.tsx b/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.tsx index 53c7db382..aaed0392a 100644 --- a/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.tsx +++ b/packages/applicant/src/pages/mandatory-questions/[mandatoryQuestionId]/getServerSideProps.tsx @@ -7,8 +7,11 @@ import { } from '../../../services/GrantMandatoryQuestionService'; import { Optional } from '../../../testUtils/unitTestHelpers'; import callServiceMethod from '../../../utils/callServiceMethod'; -import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage'; import { getJwtFromCookies } from '../../../utils/jwt'; +import { + checkIfPageHaveAlreadyBeenAnswered, + generateRedirectUrlForMandatoryQuestionNextPage, +} from '../../../utils/mandatoryQuestionUtils'; import { routes } from '../../../utils/routes'; export default async function getServerSideProps({ @@ -19,6 +22,7 @@ export default async function getServerSideProps({ resolvedUrl, //the url that the user requested }: GetServerSidePropsContext) { const { mandatoryQuestionId } = params as Record; + const { fromSummaryPage = false } = query as Record; const jwt = getJwtFromCookies(req); const { publicRuntimeConfig } = getConfig(); @@ -45,19 +49,29 @@ export default async function getServerSideProps({ return { redirect: { destination: routes.serviceError(serviceErrorProps), - permanent: false, statusCode: 302, }, }; } - //TODO only when someone access this page from the summary page, - // we want to show the default value - //otherwise we gonna send it to the next non filled page const nextNotAnsweredPage = generateRedirectUrlForMandatoryQuestionNextPage( mandatoryQuestion, mandatoryQuestionId ); + //only when someone access this page from the summary page, + // we want to show the default value + //otherwise we gonna send it to the next non filled page + if ( + checkIfPageHaveAlreadyBeenAnswered(mandatoryQuestion, resolvedUrl) && + !fromSummaryPage + ) { + return { + redirect: { + destination: nextNotAnsweredPage, + statusCode: 302, + }, + }; + } const response = await callServiceMethod( req, @@ -68,7 +82,10 @@ export default async function getServerSideProps({ mandatoryQuestionId, body ), - nextNotAnsweredPage, + //where we want to go after we update the mandatory question + fromSummaryPage + ? routes.mandatoryQuestions.summaryPage(mandatoryQuestionId) + : nextNotAnsweredPage, { errorInformation: 'Something went wrong while trying to update your organisation details', diff --git a/packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.test.ts b/packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.test.ts deleted file mode 100644 index 145feeb00..000000000 --- a/packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.test.ts +++ /dev/null @@ -1,171 +0,0 @@ -import { GrantMandatoryQuestionDto } from '../services/GrantMandatoryQuestionService'; -import { generateRedirectUrlForMandatoryQuestionNextPage } from './generateRedirectForMandatoryQuestionNextPage'; - -describe('generateRedirectUrlForMandatoryQuestionNextPage', () => { - it('should generate the correct URL for the address page when name is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - name: 'Example Organization', - addressLine1: null, - city: null, - postcode: null, - orgType: null, - companiesHouseNumber: null, - charityCommissionNumber: null, - fundingAmount: null, - fundingLocation: null, - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/organisation-address` - ); - }); - - it('should generate the correct URL for the type page when addressLine1, city, and postcode are provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - orgType: null, - companiesHouseNumber: null, - charityCommissionNumber: null, - fundingAmount: null, - fundingLocation: null, - }; - - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/organisation-type` - ); - }); - - it('should generate the correct URL for the companiesHouseNumberPage when orgType is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - companiesHouseNumber: null, - charityCommissionNumber: null, - fundingAmount: null, - fundingLocation: null, - orgType: 'Example Type', - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/organisation-companies-house-number` - ); - }); - - it('should generate the correct URL for the charityCommissionNumberPage when companiesHouseNumber is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - orgType: 'Example Type', - charityCommissionNumber: null, - fundingAmount: null, - fundingLocation: null, - companiesHouseNumber: 'ABC123', - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/organisation-charity-commission-number` - ); - }); - - it('should generate the correct URL for the fundingAmountPage when charityCommissionNumber is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - orgType: 'Example Type', - fundingAmount: null, - fundingLocation: null, - companiesHouseNumber: 'ABC123', - charityCommissionNumber: 'XYZ456', - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/funding-amount` - ); - }); - - it('should generate the correct URL for the fundingLocationPage when fundingAmount is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - orgType: 'Example Type', - fundingAmount: '1000', - fundingLocation: null, - companiesHouseNumber: 'ABC123', - charityCommissionNumber: 'XYZ456', - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe( - `/mandatory-questions/mandatoryQuestionId/funding-location` - ); - }); - - it('should generate the correct URL when everything is provided', () => { - const mandatoryQuestion: GrantMandatoryQuestionDto = { - addressLine1: '123 Main St', - city: 'Example City', - postcode: '12345', - name: 'name', - orgType: 'Example Type', - fundingAmount: '1000', - fundingLocation: 'location', - companiesHouseNumber: 'ABC123', - charityCommissionNumber: 'XYZ456', - }; - const mandatoryQuestionId = 'mandatoryQuestionId'; - - const result = generateRedirectUrlForMandatoryQuestionNextPage( - mandatoryQuestion, - mandatoryQuestionId - ); - - expect(result).toBe(`/mandatory-questions/mandatoryQuestionId/summary`); - }); -}); diff --git a/packages/applicant/src/utils/mandatoryQuestionUtils.test.ts b/packages/applicant/src/utils/mandatoryQuestionUtils.test.ts new file mode 100644 index 000000000..b871f5936 --- /dev/null +++ b/packages/applicant/src/utils/mandatoryQuestionUtils.test.ts @@ -0,0 +1,398 @@ +import { GrantMandatoryQuestionDto } from '../services/GrantMandatoryQuestionService'; +import { + checkIfPageHaveAlreadyBeenAnswered, + generateRedirectUrlForMandatoryQuestionNextPage, + getMandatoryQuestionKeyFromUrl, +} from './mandatoryQuestionUtils'; +import { routes } from './routes'; + +describe('generateRedirectUrlForMandatoryQuestionNextPage', () => { + it('should generate the correct URL for the address page when name is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + name: 'Example Organization', + addressLine1: null, + city: null, + postcode: null, + orgType: null, + companiesHouseNumber: null, + charityCommissionNumber: null, + fundingAmount: null, + fundingLocation: null, + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-address` + ); + }); + + it('should generate the correct URL for the type page when addressLine1, city, and postcode are provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: null, + companiesHouseNumber: null, + charityCommissionNumber: null, + fundingAmount: null, + fundingLocation: null, + }; + + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-type` + ); + }); + + it('should generate the correct URL for the companiesHouseNumberPage when orgType is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + companiesHouseNumber: null, + charityCommissionNumber: null, + fundingAmount: null, + fundingLocation: null, + orgType: 'Example Type', + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-companies-house-number` + ); + }); + + it('should generate the correct URL for the charityCommissionNumberPage when companiesHouseNumber is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: 'Example Type', + charityCommissionNumber: null, + fundingAmount: null, + fundingLocation: null, + companiesHouseNumber: 'ABC123', + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-charity-commission-number` + ); + }); + + it('should generate the correct URL for the fundingAmountPage when charityCommissionNumber is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: 'Example Type', + fundingAmount: null, + fundingLocation: null, + companiesHouseNumber: 'ABC123', + charityCommissionNumber: 'XYZ456', + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-funding-amount` + ); + }); + + it('should generate the correct URL for the fundingLocationPage when fundingAmount is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: 'Example Type', + fundingAmount: '1000', + fundingLocation: null, + companiesHouseNumber: 'ABC123', + charityCommissionNumber: 'XYZ456', + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-funding-location` + ); + }); + + it('should generate the correct URL when everything is provided', () => { + const mandatoryQuestion: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: 'Example Type', + fundingAmount: '1000', + fundingLocation: 'location', + companiesHouseNumber: 'ABC123', + charityCommissionNumber: 'XYZ456', + }; + const mandatoryQuestionId = 'mandatoryQuestionId'; + + const result = generateRedirectUrlForMandatoryQuestionNextPage( + mandatoryQuestion, + mandatoryQuestionId + ); + + expect(result).toBe( + `/mandatory-questions/mandatoryQuestionId/organisation-summary` + ); + }); +}); + +describe('getObjectKeyFromUrl', () => { + it('should return the correct key for the name page', () => { + const url = routes.mandatoryQuestions.namePage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('name'); + }); + it('should return the correct key for the name page even with query', () => { + const url = routes.mandatoryQuestions.namePage('id') + '?name=Example'; + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('name'); + }); + it('should return the correct keys for the address page', () => { + const url = routes.mandatoryQuestions.addressPage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toStrictEqual(['addressLine1', 'city', 'postcode']); + }); + it('should return the correct key for the type page', () => { + const url = routes.mandatoryQuestions.typePage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('orgType'); + }); + it('should return the correct key for the charity Commission Number page', () => { + const url = routes.mandatoryQuestions.charityCommissionNumberPage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('charityCommissionNumber'); + }); + + it('should return the correct key for the Companies House number page', () => { + const url = routes.mandatoryQuestions.companiesHouseNumberPage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('companiesHouseNumber'); + }); + + it('should return the correct key for the funding amount page', () => { + const url = routes.mandatoryQuestions.fundingAmountPage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('fundingAmount'); + }); + + it('should return the correct key for the funding location page', () => { + const url = routes.mandatoryQuestions.fundingLocationPage('id'); + const result = getMandatoryQuestionKeyFromUrl(url); + expect(result).toBe('fundingLocation'); + }); +}); + +describe('checkIfPageHaveAlreadyBeenAnswered', () => { + describe('return true scenarios', () => { + const mandatoryQuestionWithAllAnswers: GrantMandatoryQuestionDto = { + addressLine1: '123 Main St', + city: 'Example City', + postcode: '12345', + name: 'name', + orgType: 'Example Type', + fundingAmount: '1000', + fundingLocation: 'location', + companiesHouseNumber: 'ABC123', + charityCommissionNumber: 'XYZ456', + }; + it('should return true if the name page have already been answered', () => { + const url = routes.mandatoryQuestions.namePage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + it('should return true if the address page have already been answered', () => { + const url = routes.mandatoryQuestions.addressPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + it('should return true if the type page have already been answered', () => { + const url = routes.mandatoryQuestions.typePage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + + it('should return true if the charity Commission Number page have already been answered', () => { + const url = routes.mandatoryQuestions.charityCommissionNumberPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + + it('should return true if the charity Companies House number have already been answered', () => { + const url = routes.mandatoryQuestions.companiesHouseNumberPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + + it('should return true if the charity funding amount have already been answered', () => { + const url = routes.mandatoryQuestions.fundingAmountPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + + it('should return true if the charity funding location have already been answered', () => { + const url = routes.mandatoryQuestions.fundingLocationPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithAllAnswers, + url + ); + expect(result).toBe(true); + }); + }); + describe('return false scenarios', () => { + const mandatoryQuestionWithNoAnswers: GrantMandatoryQuestionDto = { + addressLine1: null, + city: null, + postcode: null, + name: null, + orgType: null, + fundingAmount: null, + fundingLocation: null, + companiesHouseNumber: null, + charityCommissionNumber: null, + }; + + it('should return false if a random page is used', () => { + const url = 'www.example.com/random/page?page=1'; + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + it('should return false if the name page have not been answered', () => { + const url = routes.mandatoryQuestions.namePage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + it('should return false if the address page address Line 1 have not been answered', () => { + const url = routes.mandatoryQuestions.addressPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + { addressLine1: null, city: 'city', postcode: 'postcode' }, + url + ); + expect(result).toBe(false); + }); + it('should return false if the address page city have not been answered', () => { + const url = routes.mandatoryQuestions.addressPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + { addressLine1: 'addressLine1', city: null, postcode: 'postcode' }, + url + ); + expect(result).toBe(false); + }); + it('should return false if the address page postcode have not been answered', () => { + const url = routes.mandatoryQuestions.addressPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + { addressLine1: 'addressLine1', city: 'city', postcode: null }, + url + ); + expect(result).toBe(false); + }); + it('should return false if the type page have not been answered', () => { + const url = routes.mandatoryQuestions.typePage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + + it('should return false if the charity Commission Number page have not been answered', () => { + const url = routes.mandatoryQuestions.charityCommissionNumberPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + + it('should return false if the charity Companies House number have not been answered', () => { + const url = routes.mandatoryQuestions.companiesHouseNumberPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + + it('should return false if the charity funding amount have not been answered', () => { + const url = routes.mandatoryQuestions.fundingAmountPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + + it('should return false if the charity funding location have not been answered', () => { + const url = routes.mandatoryQuestions.fundingLocationPage('id'); + const result = checkIfPageHaveAlreadyBeenAnswered( + mandatoryQuestionWithNoAnswers, + url + ); + expect(result).toBe(false); + }); + }); +}); diff --git a/packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.ts b/packages/applicant/src/utils/mandatoryQuestionUtils.ts similarity index 73% rename from packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.ts rename to packages/applicant/src/utils/mandatoryQuestionUtils.ts index 9ed6e32ab..2975a8579 100644 --- a/packages/applicant/src/utils/generateRedirectForMandatoryQuestionNextPage.ts +++ b/packages/applicant/src/utils/mandatoryQuestionUtils.ts @@ -76,3 +76,33 @@ export const generateRedirectUrlForMandatoryQuestionNextPage = ( return routes.mandatoryQuestions.addressPage(mandatoryQuestionId); } }; + +const mapper = { + 'organisation-name': 'name', + 'organisation-address': ['addressLine1', 'city', 'postcode'], + 'organisation-type': 'orgType', + 'organisation-companies-house-number': 'companiesHouseNumber', + 'organisation-charity-commission-number': 'charityCommissionNumber', + 'organisation-funding-amount': 'fundingAmount', + 'organisation-funding-location': 'fundingLocation', +}; + +export const getMandatoryQuestionKeyFromUrl = (url: string) => { + const question = url.split('/').pop().split('?')[0]; + const questionKey = mapper[question]; + return questionKey; +}; + +export const checkIfPageHaveAlreadyBeenAnswered = ( + mandatoryQuestion: GrantMandatoryQuestionDto, + url: string +): boolean => { + const questionKey = getMandatoryQuestionKeyFromUrl(url); + if (!questionKey) { + return false; + } + if (Array.isArray(questionKey)) { + return questionKey.every((key) => mandatoryQuestion[key] !== null); + } + return mandatoryQuestion[questionKey] !== null; +}; diff --git a/packages/applicant/src/utils/routes.test.tsx b/packages/applicant/src/utils/routes.test.tsx index 5096705b8..b90bbec8b 100644 --- a/packages/applicant/src/utils/routes.test.tsx +++ b/packages/applicant/src/utils/routes.test.tsx @@ -100,7 +100,7 @@ describe('Mandatory Questions Routes', () => { it('should generate the correct fundingAmountPage URL', () => { const mandatoryQuestionId = 'exampleMandatoryQuestionId'; - const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/funding-amount`; + const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/organisation-funding-amount`; expect( routes.mandatoryQuestions.fundingAmountPage(mandatoryQuestionId) ).toBe(expectedURL); @@ -108,7 +108,7 @@ describe('Mandatory Questions Routes', () => { it('should generate the correct fundingLocationPage URL', () => { const mandatoryQuestionId = 'exampleMandatoryQuestionId'; - const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/funding-location`; + const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/organisation-funding-location`; expect( routes.mandatoryQuestions.fundingLocationPage(mandatoryQuestionId) ).toBe(expectedURL); @@ -116,7 +116,7 @@ describe('Mandatory Questions Routes', () => { it('should generate the correct summary page URL', () => { const mandatoryQuestionId = 'exampleMandatoryQuestionId'; - const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/summary`; + const expectedURL = `/mandatory-questions/${mandatoryQuestionId}/organisation-summary`; expect(routes.mandatoryQuestions.summaryPage(mandatoryQuestionId)).toBe( expectedURL ); diff --git a/packages/applicant/src/utils/routes.tsx b/packages/applicant/src/utils/routes.tsx index c980ad6b1..81fcabb3c 100644 --- a/packages/applicant/src/utils/routes.tsx +++ b/packages/applicant/src/utils/routes.tsx @@ -30,11 +30,11 @@ export const routes = { charityCommissionNumberPage: (mandatoryQuestionId: string) => `/mandatory-questions/${mandatoryQuestionId}/organisation-charity-commission-number`, fundingAmountPage: (mandatoryQuestionId: string) => - `/mandatory-questions/${mandatoryQuestionId}/funding-amount`, + `/mandatory-questions/${mandatoryQuestionId}/organisation-funding-amount`, fundingLocationPage: (mandatoryQuestionId: string) => - `/mandatory-questions/${mandatoryQuestionId}/funding-location`, + `/mandatory-questions/${mandatoryQuestionId}/organisation-funding-location`, summaryPage: (mandatoryQuestionId: string) => - `/mandatory-questions/${mandatoryQuestionId}/summary`, + `/mandatory-questions/${mandatoryQuestionId}/organisation-summary`, }, applications: '/applications', submissions: {