From f1ac911340500bf1da219aa2b648a267e599d875 Mon Sep 17 00:00:00 2001 From: dominicwest Date: Wed, 11 Oct 2023 14:15:11 +0100 Subject: [PATCH] GAP-2230: Fixing download submissions page --- .../[schemeId]/download-submissions.page.tsx | 86 ++++++++----------- .../[schemeId]/download-submissions.test.tsx | 29 ++----- packages/admin/src/services/UserService.ts | 5 +- 3 files changed, 48 insertions(+), 72 deletions(-) diff --git a/packages/admin/src/pages/scheme/[schemeId]/download-submissions.page.tsx b/packages/admin/src/pages/scheme/[schemeId]/download-submissions.page.tsx index c6709bd15..40add8e00 100644 --- a/packages/admin/src/pages/scheme/[schemeId]/download-submissions.page.tsx +++ b/packages/admin/src/pages/scheme/[schemeId]/download-submissions.page.tsx @@ -1,5 +1,5 @@ import { Button, FlexibleQuestionPageLayout } from 'gap-web-ui'; -import { GetServerSideProps } from 'next'; +import { GetServerSidePropsContext } from 'next'; import CustomLink from '../../../components/custom-link/CustomLink'; import Meta from '../../../components/layout/Meta'; import ExportStatusEnum from '../../../enums/ExportStatus'; @@ -9,32 +9,24 @@ import { requestSubmissionsExport, } from '../../../services/SubmissionsService'; import { getLoggedInUsersDetails } from '../../../services/UserService'; -import UserDetails from '../../../types/UserDetails'; import { generateErrorPageRedirect } from '../../../utils/serviceErrorHelpers'; import { getSessionIdFromCookies } from '../../../utils/session'; +import { parseBody } from 'next/dist/server/api-utils/node'; +import InferProps from '../../../types/InferProps'; -export const getServerSideProps: GetServerSideProps = async ({ +export const getServerSideProps = async ({ req, query, resolvedUrl, -}) => { +}: GetServerSidePropsContext) => { const sessionCookie = getSessionIdFromCookies(req); - const { schemeId } = query as Record; + const { schemeId, requested } = query as Record; - let exportStatus: ExportStatusEnum; let applicationFormsStatus: { applicationId: string; submissionCount: number; }[]; let applicationId: string; - let userDetails: UserDetails = { - firstName: '', - lastName: '', - organisationName: '', - emailAddress: '', - roles: [], - created: '', - }; const errorPageRedirect = generateErrorPageRedirect( 'Something went wrong while trying to export submissions.', @@ -63,40 +55,36 @@ export const getServerSideProps: GetServerSideProps = async ({ ); } - exportStatus = await getApplicationExportStatus(sessionCookie, applicationId); - - if (exportStatus == ExportStatusEnum.COMPLETE) { - // for now, we just treat COMPLETE the same as NOT_STARTED ie. allow them to trigger another download - // in the future, if an export is complete we should aim to redirect them to the summary table page - // so they can access the most recent export without requiring the email link - exportStatus = ExportStatusEnum.NOT_STARTED; + if (req.method === 'POST') { + const body = await parseBody(req, '1mb'); + if ('download-submitted-applications' in body) { + requestSubmissionsExport(sessionCookie, applicationId); + return { + redirect: { + destination: `/scheme/${schemeId}/download-submissions?requested=true`, + statusCode: 302, + }, + }; + } } - if (exportStatus == ExportStatusEnum.NOT_STARTED) { - requestSubmissionsExport(sessionCookie, applicationId); - return { - redirect: { - destination: `/scheme/${schemeId}/download-submissions`, - permanent: false, - }, - }; - } + const exportStatus = await getApplicationExportStatus( + sessionCookie, + applicationId + ); - if ( - exportStatus == ExportStatusEnum.PROCESSING || - exportStatus == ExportStatusEnum.REQUESTED - ) { - try { - userDetails = await getLoggedInUsersDetails(sessionCookie); - } catch (error) { - return errorPageRedirect; - } + let userDetails; + try { + userDetails = await getLoggedInUsersDetails(sessionCookie); + } catch (error) { + return errorPageRedirect; } return { props: { backButtonHref: `/scheme/${schemeId}`, exportStatus, + requested: requested || null, emailAddress: userDetails.emailAddress, formAction: resolvedUrl, csrfToken: (req as any).csrfToken?.() || '', @@ -107,16 +95,18 @@ export const getServerSideProps: GetServerSideProps = async ({ const DownloadSubmissions = ({ backButtonHref, exportStatus, + requested, emailAddress, formAction, csrfToken, -}: DownloadSubmissionsProps) => { +}: InferProps) => { return ( <>
{(exportStatus == ExportStatusEnum.PROCESSING || - exportStatus == ExportStatusEnum.REQUESTED) && ( + exportStatus == ExportStatusEnum.REQUESTED || + requested == 'true') && ( <>
@@ -151,7 +142,8 @@ const DownloadSubmissions = ({ )} - {exportStatus == ExportStatusEnum.NOT_STARTED && ( + {(exportStatus == ExportStatusEnum.NOT_STARTED || + exportStatus == ExportStatusEnum.COMPLETE) && ( <>

View your applications

({ + parseBody: jest.fn(), +})); const mockedFindApplicationFormFromScheme = findApplicationFormFromScheme as jest.Mock; @@ -35,6 +39,7 @@ const customProps = { emailAddress: '', csrfToken: '', formAction: '', + requested: null, }; const component = ; @@ -138,6 +143,7 @@ describe('Download submissions page', () => { mockedGetApplicationExportStatus.mockResolvedValue( ExportStatusEnum.PROCESSING ); + (parseBody as jest.Mock).mockResolvedValue({ testBody: true }); }); it('Should NOT return a redirect object if applicationFormsStatus is successfully retrieved', async () => { @@ -213,26 +219,6 @@ describe('Download submissions page', () => { ); }); - it('Should redirect to self when NOT_STARTED', async () => { - mockedGetApplicationExportStatus.mockResolvedValue( - ExportStatusEnum.NOT_STARTED - ); - mockedRequestSubmissionsExport.mockResolvedValue({}); - - const response = (await getServerSideProps( - getContext() - )) as NextGetServerSidePropsResponse; - - expect(response).toStrictEqual({ - redirect: { - permanent: false, - destination: '/scheme/testSchemeId/download-submissions', - }, - }); - }); - - it.todo('Placeholder for checking if the export is in progress'); - describe('POST method', () => { beforeEach(() => { mockedFindApplicationFormFromScheme.mockResolvedValue([ @@ -250,6 +236,9 @@ describe('Download submissions page', () => { mockedGetApplicationExportStatus.mockResolvedValue( ExportStatusEnum.NOT_STARTED ); + (parseBody as jest.Mock).mockResolvedValue({ + 'download-submitted-applications': true, + }); await getServerSideProps(getPostContext()); diff --git a/packages/admin/src/services/UserService.ts b/packages/admin/src/services/UserService.ts index 8b885171d..b43990e10 100644 --- a/packages/admin/src/services/UserService.ts +++ b/packages/admin/src/services/UserService.ts @@ -1,9 +1,12 @@ import axios from 'axios'; import { axiosSessionConfig } from '../utils/session'; +import UserDetails from '../types/UserDetails'; const BASE_USERS_URL = process.env.BACKEND_HOST + '/users'; -const getLoggedInUsersDetails = async (sessionCookie: string) => { +const getLoggedInUsersDetails = async ( + sessionCookie: string +): Promise => { const response = await axios.get( `${BASE_USERS_URL}/loggedInUser`, axiosSessionConfig(sessionCookie)