diff --git a/src/app/(investment)/deals/[id]/page.tsx b/src/app/(investment)/deals/[id]/page.tsx index 4fd2c27..f6d8280 100644 --- a/src/app/(investment)/deals/[id]/page.tsx +++ b/src/app/(investment)/deals/[id]/page.tsx @@ -6,7 +6,7 @@ import ReactMarkdown from "react-markdown"; import * as Tabs from "@radix-ui/react-tabs"; import { Button } from "@/components/ui/button"; import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from "@/components/ui/carousel"; -import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; +import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import { Separator } from "@/components/ui/separator"; import { createSupabaseClient } from "@/lib/supabase/serverComponentClient"; @@ -16,6 +16,7 @@ import { getProjectData } from "@/lib/data/projectQuery"; import { getDealList } from "@/app/api/dealApi"; import { sumByKey, toPercentage } from "@/lib/utils"; import { redirect } from "next/navigation"; +import { isOwnerOfProject } from "./query"; import remarkGfm from "remark-gfm"; const PHOTO_MATERIAL_ID = 2; @@ -23,6 +24,7 @@ const PHOTO_MATERIAL_ID = 2; export default async function ProjectDealPage({ params }: { params: { id: number } }) { const supabase = createSupabaseClient(); const { data: projectData, error: projectDataError } = await getProjectData(supabase, params.id); + const { data: user, error: userError } = await supabase.auth.getUser(); const { data: projectMaterial, error: projectMaterialError } = await supabase .from("project_material") @@ -48,6 +50,19 @@ export default async function ProjectDealPage({ params }: { params: { id: number ); } + if (userError || !user) { + return ( +
+

Error fetching data. Please try again.

+ +
+ ); + } + + const isOwner = await isOwnerOfProject(supabase, params.id, user.user?.id); + const projectBusinessOwnerId = projectData.user_id; const dealList = await getDealList(projectBusinessOwnerId); const totalDealAmount = sumByKey(dealList, "deal_amount"); @@ -122,7 +137,66 @@ export default async function ProjectDealPage({ params }: { params: { id: number -
+ + +
+
+ +

${totalDealAmount}

+

+ {toPercentage(totalDealAmount, projectData?.target_investment)}% raised of $ + {projectData?.target_investment} max goal +

+ +
+ +

+

{dealList.length}

+

+

Investors

+
+ + +

+ {projectData?.investment_deadline ? ( + <> +

{Math.floor(hourLeft)} hours

+

Left to invest

+ + ) : ( +

No deadline

+ )} +
+
+
+
+ + +
+ + {isOwner && ( + + )} +
+
+ + +
+
+
+ + {/*

${totalDealAmount}

@@ -153,11 +227,20 @@ export default async function ProjectDealPage({ params }: { params: { id: number

No deadline

)}
- +
+

Dataroom

+ + +
-
+
*/} {/* menu */}
diff --git a/src/app/(investment)/deals/[id]/query.ts b/src/app/(investment)/deals/[id]/query.ts new file mode 100644 index 0000000..1f4bb7a --- /dev/null +++ b/src/app/(investment)/deals/[id]/query.ts @@ -0,0 +1,21 @@ +import { SupabaseClient } from "@supabase/supabase-js"; + +const isOwnerOfProject = async (client: SupabaseClient, projectId: number, userId: string) => { + try { + const { data, error } = await client.from("project").select("...business(user_id)").eq("id", projectId).single(); + + if (error) { + throw new Error(`Error fetching project: ${error}`); + } + + if (!data) { + return false; + } + + return data.user_id === userId; + } catch (error) { + return false; + } +}; + +export { isOwnerOfProject }; diff --git a/src/lib/data/projectQuery.ts b/src/lib/data/projectQuery.ts index 5e4bb73..9d534b0 100644 --- a/src/lib/data/projectQuery.ts +++ b/src/lib/data/projectQuery.ts @@ -71,6 +71,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) { project_description, published_time, card_image_url, + deadline, ...project_investment_detail!inner ( min_investment, total_investment,