From b14187d248e249e31ef3887de6d132e87336e958 Mon Sep 17 00:00:00 2001 From: Diego Cohen Date: Tue, 3 Dec 2024 16:34:10 -0500 Subject: [PATCH] Add eligibility check to api route --- pages/api/hold/request/[id]/index.ts | 18 +++++++++++------- pages/hold/request/[id]/index.tsx | 16 ++++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pages/api/hold/request/[id]/index.ts b/pages/api/hold/request/[id]/index.ts index fe15161a3..c3a65d1c7 100644 --- a/pages/api/hold/request/[id]/index.ts +++ b/pages/api/hold/request/[id]/index.ts @@ -1,4 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next" +import { NextResponse } from "next/server" import { postHoldRequest, @@ -22,20 +23,22 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const holdId = req.query.id as string const [, itemId] = holdId.split("-") - const patronEligibility = await fetchHoldRequestEligibility(patronId) + const patronEligibilityStatus = await fetchHoldRequestEligibility(patronId) - if (!patronEligibility?.eligibility) { + if (patronEligibilityStatus && !patronEligibilityStatus?.eligibility) { switch (jsEnabled) { case true: return res.status(401).json({ - patronEligibility, + patronEligibilityStatus, }) // Server side redirect on ineligibility when Js is disabled // TODO: Move this to seaprate API route + // TODO: Parse these query params in getServerSideProps default: - res.redirect( - `${BASE_URL}${PATHS.HOLD_REQUEST}/${holdId}?patronEligibility=${patronEligibility}` + return NextResponse.redirect( + `${BASE_URL}${PATHS.HOLD_REQUEST}/${holdId}?patronEligibility=${patronEligibilityStatus}`, + 307 ) } } @@ -64,8 +67,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { } // Server side redirect in case user has JS disabled - res.redirect( - `${BASE_URL}${PATHS.HOLD_CONFIRMATION}/${holdId}?pickupLocation=${pickupLocationFromResponse}?requestId=${requestId}` + return NextResponse.redirect( + `${BASE_URL}${PATHS.HOLD_CONFIRMATION}/${holdId}?pickupLocation=${pickupLocationFromResponse}?requestId=${requestId}`, + 307 ) } catch (error) { const { statusText } = error as Response diff --git a/pages/hold/request/[id]/index.tsx b/pages/hold/request/[id]/index.tsx index 1c0091d13..015c3dd14 100644 --- a/pages/hold/request/[id]/index.tsx +++ b/pages/hold/request/[id]/index.tsx @@ -60,7 +60,7 @@ export default function HoldRequestPage({ patronId, isAuthenticated, errorStatus: defaultErrorStatus, - patronEligibilityStatus, + patronEligibilityStatus: defaultEligibilityStatus, }: HoldRequestPropsType) { const metadataTitle = `Item Request | ${SITE_NAME}` @@ -70,6 +70,9 @@ export default function HoldRequestPage({ const holdId = `${item.bibId}-${item.id}` const [errorStatus, setErrorStatus] = useState(defaultErrorStatus) + const [patronEligibilityStatus, setPatronEligibilityStatus] = useState( + defaultEligibilityStatus + ) const [formPosting, setFormPosting] = useState(false) const bannerContainerRef = useRef() @@ -98,28 +101,29 @@ export default function HoldRequestPage({ }), }) const responseJson = await response.json() - const { pickupLocation: pickupLocationFromResponse, requestId } = - responseJson switch (response.status) { + // Patron is ineligible to place holds case 401: - setErrorStatus("patronIneligible") setFormPosting(false) + setErrorStatus("patronIneligible") + setPatronEligibilityStatus(responseJson.patronEligibilityStatus) bannerContainerRef.current.focus() break + // Server side error placing the hold request case 500: + setFormPosting(false) console.error( "HoldRequestPage: Error in hold request api response", responseJson.error ) setErrorStatus("failed") - setFormPosting(false) break default: setFormPosting(false) // Success state await router.push( - `${PATHS.HOLD_CONFIRMATION}/${holdId}?pickupLocation=${pickupLocationFromResponse}&requestId=${requestId}` + `${PATHS.HOLD_CONFIRMATION}/${holdId}?pickupLocation=${responseJson.pickupLocation}&requestId=${responseJson.requestId}` ) } } catch (error) {