Skip to content

Commit

Permalink
Add eligibility check to api route
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcohen committed Dec 3, 2024
1 parent 69e359b commit b14187d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
18 changes: 11 additions & 7 deletions pages/api/hold/request/[id]/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next"
import { NextResponse } from "next/server"

import {
postHoldRequest,
Expand All @@ -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
)
}
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions pages/hold/request/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function HoldRequestPage({
patronId,
isAuthenticated,
errorStatus: defaultErrorStatus,
patronEligibilityStatus,
patronEligibilityStatus: defaultEligibilityStatus,
}: HoldRequestPropsType) {
const metadataTitle = `Item Request | ${SITE_NAME}`

Expand All @@ -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<HTMLDivElement>()

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b14187d

Please sign in to comment.