diff --git a/src/app/dataroom/overview/page.tsx b/src/app/dataroom/overview/page.tsx new file mode 100644 index 0000000..e5abbb5 --- /dev/null +++ b/src/app/dataroom/overview/page.tsx @@ -0,0 +1,216 @@ +"use client"; + +import { useMemo } from "react"; +import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; +import { requestAccessToDataRoom } from "@/lib/data/dataroomMutate"; +import { getInvestmentByUserId } from "@/lib/data/investmentQuery"; +import { + Table, + TableBody, + TableCaption, + TableCell, + TableHeader, + TableHead, + TableRow, + TableFooter, +} from "@/components/ui/table"; +import { Button } from "@/components/ui/button"; +import { getAccessRequests } from "@/lib/data/dataroomQuery"; +import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; +import { Separator } from "@/components/ui/separator"; +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; +import useSession from "@/lib/supabase/useSession"; +import toast from "react-hot-toast"; + +interface Investment { + id: any; + project_id: any; + project_name: any; + project_short_description: any; + dataroom_id: any; + deal_amount: any; + investor_id: any; + created_time: any; +} + +interface AccessRequest { + id: number; + dataroom_id: number; + user_id: string; + status: "approve" | "reject" | "pending"; + requested_at: string; +} + +export default function ListRequestAccessPage() { + const client = createSupabaseClient(); + const { session, loading: sessionLoading } = useSession(); + const userId = session?.user?.id; + + const { + data: investments = [], + error: investmentsError, + isLoading: isLoadingInvestments, + } = useQuery(getInvestmentByUserId(client, userId ?? ""), { + enabled: !!userId, + }); + + const { + data: accessRequests = [], + error: accessRequestsError, + isLoading: isLoadingAccessRequests, + refetch: refetchAccessRequests, + } = useQuery(getAccessRequests(client, { userId: userId ?? "" }), { + enabled: !!userId, + }); + + const projectInvestments = useMemo(() => { + if (!investments) return {}; + + return investments.reduce((acc: { [key: string]: Investment[] }, investment: Investment) => { + const projectId = investment.project_id; + if (!acc[projectId]) { + acc[projectId] = []; + } + acc[projectId].push(investment); + return acc; + }, {}); + }, [investments]); + + const handleRequestAccess = async (dataroomId: any) => { + if (!userId) { + toast.error("Please login first"); + return; + } + + try { + const { error } = await requestAccessToDataRoom(client, dataroomId, userId); + if (error) { + toast.error("Error sending request."); + return; + } + + toast.success("Request sent successfully!"); + await refetchAccessRequests(); + } catch (error) { + toast.error("An unexpected error occurred"); + } + }; + + if (sessionLoading) { + return
Checking authentication...
; + } + + if (!session) { + return
Please login to view this page
; + } + + if (isLoadingInvestments || isLoadingAccessRequests) { + return
Loading data...
; + } + + if (investmentsError || accessRequestsError) { + return
Error loading data!
; + } + + return ( +
+

List of Access Requests

+ +
+

Project Investments

+ + + {Object.entries(projectInvestments).map(([projectId, projectInvestments]) => ( + + + {projectInvestments[0].project_name} + {projectInvestments[0].project_short_description} + + + + Investments for this project + + + Investment Id + Invested At + Amount + + + + {projectInvestments.map((investment) => ( + + {investment.id} + {investment.created_time} + {investment.deal_amount} + + ))} + + + + Total + + {projectInvestments + .reduce((total, investment) => total + (parseFloat(investment.deal_amount) || 0), 0) + .toLocaleString("en-US", { style: "currency", currency: "USD" })} + + + +
+
+ + {(accessRequests || []).some( + (request) => request.dataroom_id === projectInvestments[0].dataroom_id && request.status === "pending" + ) ? ( + + ) : (accessRequests || []).some( + (request) => request.dataroom_id === projectInvestments[0].dataroom_id && request.status === "approve" + ) ? ( + + ) : ( + + )} + +
+ ))} +
+ + {/* Access Requests Table - Unchanged */} +
+

Access Requests & Status

+ + A list of your access requests. + + + Request ID + Dataroom ID + Status + Requested At + + + + {(accessRequests || []).length > 0 ? ( + accessRequests?.map((request: AccessRequest) => ( + + {request.id} + {request.dataroom_id} + {request.status} + {new Date(request.requested_at).toLocaleDateString()} + + )) + ) : ( + + + No access requests found. + + + )} + +
+
+
+ ); +} diff --git a/src/lib/data/investmentQuery.ts b/src/lib/data/investmentQuery.ts index e978640..bf8b349 100644 --- a/src/lib/data/investmentQuery.ts +++ b/src/lib/data/investmentQuery.ts @@ -9,3 +9,23 @@ export const getInvestmentCountsByProjectsIds = (client: SupabaseClient, project }) .in("project_id", projectIds); }; + +export const getInvestmentByUserId = (client: SupabaseClient, userId: string) => { + return client + .from("investment_deal") + .select( + ` + id, + ...project_id ( + project_id:id, + project_name, + project_short_description, + dataroom_id + ), + deal_amount, + investor_id, + created_time + ` + ) + .eq("investor_id", userId); +};