Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analysis Page #30

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions apps/www/src/actions/create-analyse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"use server"

import { revalidatePath } from "next/cache"

import { prisma } from "@/lib/db"
import { getCurrentUser } from "@/lib/session"

export async function createAnalysis(analysisData) {
const user = await getCurrentUser()
const userId = user?.id

if (!userId) {
console.error("No user is currently logged in.")
return { success: false, error: "User not authenticated" }
}

try {
// Fetch the user's workspace to associate the analysis
const workspace = await prisma.workspace.findFirst({
where: { users: { some: { id: userId } } },
select: { id: true },
})

if (!workspace) {
throw new Error("No workspace found for this user")
}

// Create a new financial analysis for the selected building
const newAnalysis = await prisma.financialAnalysisBuilding.create({
data: {
buildingId: analysisData.buildingId,
name: analysisData.name, // Include the name field
rentableArea: 1000,
ratioAreaOffice: 0.5,
ratioAreaMerch: 0.3,
ratioAreaMisc: 0.2,
rentPerArea: 500,
avgExpiryPeriod: 12,
appreciationDate: new Date(),
lastDayOfYear: new Date(new Date().getFullYear(), 11, 31),
lastBalanceDate: new Date(),
numMonthsOfYear: 12,
sumValueNow: 1000000,
sumValueExit: 1200000,
vacancyPerYear: JSON.stringify({ "2024": "10.5", "2025": "9.8" }),
ownerCostsMethod: true,
ownerCostsManual: 50000,
costMaintenance: 10000,
costInsurance: 5000,
costRevision: 3000,
costAdm: 2000,
costOther: 1000,
costNegotiation: 2000,
costLegalFees: 1000,
costConsultFees: 1500,
costAssetMgmt: 2500,
costSum: 45000,
costBigExpenses: JSON.stringify({ "2024": "100000", "2025": "20500" }),
useCalcROI: true,
roiWeightedYield: 0.05,
roiInflation: 0.02,
roiCalculated: 0.07,
roiManual: 0.06,
marketRentOffice: 600,
marketRentMerch: 400,
marketRentMisc: 300,
usePrimeYield: true,
manYieldOffice: 0.05,
manYieldMerch: 0.04,
manYieldMisc: 0.03,
manYieldWeighted: 0.045,
kpi1: 0,
kpi2: 0,
kpi3: 0,
kpi4: 0,
},
})
console.log(
`Created analysis with ID: ${newAnalysis.id} for building ID: ${analysisData.buildingId}.`,
)

revalidatePath("/analytics")

return { success: true, analysis: newAnalysis }
} catch (error) {
console.error(`Error creating analysis for user ID: ${userId}`, error)
return { success: false, error: error.message }
}
}
32 changes: 32 additions & 0 deletions apps/www/src/actions/get-analysis-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server"

import { prisma } from "@/lib/db"

export async function getAnalysisDetails(analysisId: string) {
try {
const numericId = parseInt(analysisId, 10)
if (isNaN(numericId)) {
throw new Error("Invalid analysis ID")
}

const analysisDetails = await prisma.financialAnalysisBuilding.findUnique({
where: { id: numericId },
include: {
building: {
select: {
name: true,
},
},
},
})

if (!analysisDetails) {
throw new Error("Analysis not found")
}

return { success: true, analysisDetails }
} catch (error) {
console.error("Error fetching analysis details:", error)
return { success: false, error: error.message }
}
}
38 changes: 38 additions & 0 deletions apps/www/src/actions/get-analyst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use server"

import { prisma } from "@/lib/db"

export async function getAnalyses(workspaceId: string) {
try {
const analyses = await prisma.financialAnalysisBuilding.findMany({
where: {
building: {
property: {
workspaceId,
},
},
},
select: {
id: true,
name: true,
rentableArea: true,
rentPerArea: true,
sumValueNow: true,
sumValueExit: true,
building: {
select: {
name: true,
},
},
},
orderBy: {
name: "asc",
},
})

return { success: true, analyses }
} catch (error) {
console.error("Error fetching analyses:", error)
return { success: false, error: error.message }
}
}
30 changes: 30 additions & 0 deletions apps/www/src/actions/update-analysis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use server"

import { prisma } from "@/lib/db"
import { getCurrentUser } from "@/lib/session"

export async function updateAnalysis(analysisId, updateData) {
const user = await getCurrentUser()
const userId = user?.id

if (!userId) {
console.error("No user is currently logged in.")
return { success: false, error: "User not authenticated" }
}

try {
const updatedAnalysis = await prisma.financialAnalysisBuilding.update({
where: { id: parseInt(analysisId, 10) },
data: {
...updateData,
},
})

console.log(`Updated analysis with ID: ${updatedAnalysis.id}.`)

return { success: true, analysis: updatedAnalysis }
} catch (error) {
console.error(`Error updating analysis for user ID: ${userId}`, error)
return { success: false, error: error.message }
}
}
28 changes: 0 additions & 28 deletions apps/www/src/app/(analytics)/analytics/[id]/contract/page.tsx

This file was deleted.

51 changes: 51 additions & 0 deletions apps/www/src/app/(analytics)/analytics/[id]/dcf/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Link from "next/link"
import { getAnalysisDetails } from "@/actions/get-analysis-details"

import { Button } from "@dingify/ui/components/button"

import { AnalysisTableDCF } from "@/components/analyse/AnalysisTableDCF"
import { DashboardHeader } from "@/components/dashboard/header"
import { DashboardShell } from "@/components/dashboard/shell"

export default async function DCFDetailsPage({
params,
}: {
params: { id: string }
}) {
const analysisId = params.id

try {
const { success, analysisDetails, error } =
await getAnalysisDetails(analysisId)
console.log(analysisDetails)

if (!success || !analysisDetails) {
return (
<DashboardShell>
<DashboardHeader
heading="Analysis not found"
text="We couldn't find the analysis you're looking for."
/>
</DashboardShell>
)
}

return (
<DashboardShell>
<DashboardHeader
heading={analysisDetails.name}
text="Detaljer om analysen."
></DashboardHeader>
<div className="mt-4 space-y-4">
<AnalysisTableDCF details={analysisDetails} />
</div>
</DashboardShell>
)
} catch (error) {
return (
<DashboardShell>
<DashboardHeader heading="Error" text={error.message} />
</DashboardShell>
)
}
}
45 changes: 45 additions & 0 deletions apps/www/src/app/(analytics)/analytics/[id]/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { SidebarNavItem } from "@/types"
import { notFound } from "next/navigation"

import { getCurrentUser } from "@/lib/session"
import { DashboardNav } from "@/components/layout/nav"

interface DashboardLayoutProps {
children?: React.ReactNode
params: { id: string }
}

export default async function DashboardLayout({
children,
params,
}: DashboardLayoutProps) {
const user = await getCurrentUser()

if (!user) {
return notFound()
}

const sidebarNavItems: SidebarNavItem[] = [
{
title: "Informasjon",
href: `/analytics/${params.id}`,
icon: "home",
},
{
title: "Kontantstrøm",
href: `/analytics/${params.id}/dcf`,
icon: "piechart",
},
]

return (
<div className="grid flex-1 gap-12 md:grid-cols-[200px_1fr]">
<aside className="w-[200px] flex-col md:flex">
<DashboardNav items={sidebarNavItems} />
</aside>
<div className="flex w-full flex-1 flex-col overflow-hidden">
{children}
</div>
</div>
)
}
Loading