11import Link from 'next/link'
2- import { Verify } from '@/lib/firebase/firebase'
2+ import { IsLoggedIn , Verify } from '@/lib/firebase/firebase'
33import { getBorrow , getListBorrows } from '@/lib/api/borrow'
44import { Badge } from '@/components/ui/badge'
55import {
@@ -31,6 +31,7 @@ import { differenceInDays } from 'date-fns'
3131import { Borrow } from '@/lib/types/borrow'
3232import { Button } from '@/components/ui/button'
3333import { BtnUndoReturn } from '@/components/borrows/BtnUndoReturn'
34+ import { redirect , RedirectType } from 'next/navigation'
3435
3536export default async function BorrowDetailsPage ( {
3637 params,
@@ -39,7 +40,9 @@ export default async function BorrowDetailsPage({
3940} ) {
4041 const { id } = await params
4142
42- await Verify ( { from : `/borrows/${ id } ` } )
43+ const from = `/borrows/${ id } `
44+
45+ const headers = await Verify ( { from } )
4346
4447 const [ borrowRes ] = await Promise . all ( [ getBorrow ( { id } ) ] )
4548
@@ -49,11 +52,10 @@ export default async function BorrowDetailsPage({
4952
5053 const isDue = isBorrowDue ( borrowRes . data )
5154
52- // const progressPercent = getBorrowProgressPercent(borrowRes.data)
53-
54- const headers = await Verify ( {
55- from : '/borrows' ,
56- } )
55+ const claim = await IsLoggedIn ( )
56+ if ( ! claim || ! claim . librarease ) {
57+ redirect ( `/login?from=${ encodeURIComponent ( from ) } ` , RedirectType . replace )
58+ }
5759
5860 let prevBorrows : Borrow [ ] = [ ]
5961 const [ prevBorrowsRes ] = await Promise . all ( [
@@ -75,6 +77,12 @@ export default async function BorrowDetailsPage({
7577 prevBorrows = prevBorrowsRes . data
7678 }
7779
80+ const isSuperAdmin = claim . librarease . role === 'SUPERADMIN'
81+ const isAdmin = claim . librarease . role === 'ADMIN'
82+ const isStaff = claim . librarease . admin_libs
83+ . concat ( claim . librarease . staff_libs )
84+ . includes ( borrowRes . data . subscription . membership . library_id )
85+
7886 return (
7987 < >
8088 < div className = "grid grid-cols-1 md:grid-cols-2 gap-4" >
@@ -306,27 +314,32 @@ export default async function BorrowDetailsPage({
306314 </ Card >
307315 ) }
308316
309- < div className = "bottom-0 sticky py-2 flex flex-col md:flex-row gap-2 md:gap-4 basis-1/2" >
310- { borrowRes . data . returning ? (
311- < BtnUndoReturn
312- variant = "outline"
313- className = "w-full"
314- borrow = { borrowRes . data }
315- />
316- ) : (
317- < BtnReturnBook
318- variant = "outline"
319- className = "w-full"
320- borrow = { borrowRes . data }
321- />
322- ) }
323- < Button asChild >
324- < Link href = { `/borrows/${ borrowRes . data . id } /edit` } className = "w-full" >
325- < Pen />
326- Edit
327- </ Link >
328- </ Button >
329- </ div >
317+ { ( isSuperAdmin || isAdmin || isStaff ) && (
318+ < div className = "bottom-0 sticky py-2 flex flex-col md:flex-row gap-2 md:gap-4 basis-1/2" >
319+ { borrowRes . data . returning ? (
320+ < BtnUndoReturn
321+ variant = "outline"
322+ className = "w-full"
323+ borrow = { borrowRes . data }
324+ />
325+ ) : (
326+ < BtnReturnBook
327+ variant = "outline"
328+ className = "w-full"
329+ borrow = { borrowRes . data }
330+ />
331+ ) }
332+ < Button asChild >
333+ < Link
334+ href = { `/borrows/${ borrowRes . data . id } /edit` }
335+ className = "w-full"
336+ >
337+ < Pen />
338+ Edit
339+ </ Link >
340+ </ Button >
341+ </ div >
342+ ) }
330343 </ >
331344 )
332345}
0 commit comments