Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Codehagen committed Jul 30, 2024
1 parent 18a7f97 commit 16ec019
Show file tree
Hide file tree
Showing 32 changed files with 353 additions and 333 deletions.
2 changes: 1 addition & 1 deletion apps/www/.content-collections/generated/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// generated by content-collections at Tue Jul 30 2024 07:39:58 GMT+0200 (Central European Summer Time)
// generated by content-collections at Tue Jul 30 2024 11:03:01 GMT+0200 (Central European Summer Time)

import allDocs from "./allDocs.js";
import allMetas from "./allMetas.js";
Expand Down
79 changes: 46 additions & 33 deletions apps/www/src/actions/create-contract.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,63 @@
"use server";
"use server"

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

export async function createContract(contractData) {
const user = await getCurrentUser();
const userId = user?.id;
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" };
console.error("No user is currently logged in.")
return { success: false, error: "User not authenticated" }
}

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

if (!workspace) {
throw new Error("No workspace found for this user");
if (!userWorkspace) {
console.error("No workspace found for this user.")
return { success: false, error: "No workspace found" }
}

// Create a new contract
const newContract = await prisma.contract.create({
data: {
tenantId: contractData.tenantId,
propertyId: contractData.property,
buildingId: contractData.building,
floors: {
connect: { id: contractData.floor },
workspace: {
connect: { id: userWorkspace.id }
},
officeSpaces: {
connect: { id: contractData.officeSpace },
tenant: {
connect: { id: contractData.tenantId }
},
property: {
connect: { id: contractData.propertyId }
},
building: {
connect: { id: contractData.buildingId }
},
floors: contractData.floors,
officeSpaces: contractData.officeSpaces,
contact: {
connect: { id: contractData.contactId }
},
workspaceId: workspace.id,
contactId: parseInt(contractData.contactId), // Ensure contactId is passed correctly
contactName: contractData.contactName,
contactEmail: contractData.contactEmail,
contactPhone: contractData.contactPhone,
landlordOrgnr: contractData.landlordOrgnr,
landlordName: contractData.landlordName,
contractType: contractData.contractType,
contactName: contractData.contactName,
contactEmail: contractData.contactEmail,
contactPhone: contractData.contactPhone,
startDate: contractData.startDate,
endDate: contractData.endDate,
negotiationDate: contractData.negotiationDate,
Expand All @@ -57,15 +72,13 @@ export async function createContract(contractData) {
businessCategory: contractData.businessCategory,
collateral: contractData.collateral,
},
});
})

console.log(
`Created contract with ID: ${newContract.id} for workspace ID: ${workspace.id}.`
);
console.log(`Created contract with ID: ${newContract.id} for workspace ID: ${userWorkspace.id}.`)

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

import { revalidatePath } from "next/cache"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ export function BuildingFormContract({ tenantDetails }) {
property: tenantDetails?.property?.id?.toString() || "",
floor: tenantDetails?.floor?.id?.toString() || "",
officeSpace: tenantDetails?.officeSpace?.id?.toString() || "",
contactId: tenantDetails?.contacts[0]?.id?.toString() || "", // Assuming there's at least one contact
contactId: tenantDetails?.contacts[0]?.id?.toString() || "",
},
})

useEffect(() => {
if (tenantDetails?.floor?.id) {
setSelectedFloor(tenantDetails.floor.id.toString())
form.setValue("floor", tenantDetails.floor.id.toString())
}
}, [tenantDetails?.floor?.id])
if (tenantDetails?.officeSpace?.id) {
form.setValue("officeSpace", tenantDetails.officeSpace.id.toString())
}
}, [tenantDetails, form])

const onSubmit = async (data) => {
setIsLoading(true)
Expand Down Expand Up @@ -158,7 +162,7 @@ export function BuildingFormContract({ tenantDetails }) {
</FormItem>
)}
/>
<FormField
{/* <FormField
control={form.control}
name="floor"
render={({ field }) => (
Expand Down Expand Up @@ -232,7 +236,7 @@ export function BuildingFormContract({ tenantDetails }) {
<FormMessage />
</FormItem>
)}
/>
/> */}
</div>
</CardContent>
<CardFooter className="justify-between space-x-2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from "react"
import { getTenantDetails } from "@/actions/get-tenant-details"

import { Card } from "@dingify/ui/components/card"

import { DashboardHeader } from "@/components/dashboard/header"
import { DashboardShell } from "@/components/dashboard/shell"
import { EmptyPlaceholder } from "@/components/shared/empty-placeholder"
import { ContractCheck } from "@/components/tenant/ContractCheck"

import { BuildingFormContract } from "./_components/BuildingFormContract"

Expand All @@ -23,31 +21,20 @@ export default async function Home({ params }: { params: { id: string } }) {
)
}

if (tenantDetails.name && parseInt(tenantDetails.name) > 0) {
return (
<DashboardShell>
<DashboardHeader
heading="Byggninger"
text="Hvordan byggning skal leietakeren inn i?"
/>
<EmptyPlaceholder>
<EmptyPlaceholder.Icon name="help" />
<EmptyPlaceholder.Title>Du mangler følgende</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
Placeholder
</EmptyPlaceholder.Description>
</EmptyPlaceholder>
</DashboardShell>
)
}
const hasContract =
tenantDetails.contracts && tenantDetails.contracts.length > 0

return (
<DashboardShell>
<DashboardHeader
heading="Byggninger"
text="Hvordan byggning skal leietakeren inn i?"
/>
<BuildingFormContract tenantDetails={tenantDetails} />
{hasContract ? (
<BuildingFormContract tenantDetails={tenantDetails} />
) : (
<ContractCheck tenantDetails={tenantDetails} />
)}
</DashboardShell>
)
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React from "react"
import Link from "next/link"
import { getTenantDetails } from "@/actions/get-tenant-details"

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

import { DashboardHeader } from "@/components/dashboard/header"
import { DashboardShell } from "@/components/dashboard/shell"
import { EmptyPlaceholder } from "@/components/shared/empty-placeholder"
import { ContractCheck } from "@/components/tenant/ContractCheck"

import { KpiDetailsForm } from "./_components/KpiDetailsForm"

Expand All @@ -23,31 +26,17 @@ export default async function kpiPage({ params }: { params: { id: string } }) {
)
}

if (tenantDetails.name && parseInt(tenantDetails.name) > 0) {
return (
<DashboardShell>
<DashboardHeader
heading="Byggninger"
text="Hvordan byggning skal leietakeren inn i?"
/>
<EmptyPlaceholder>
<EmptyPlaceholder.Icon name="help" />
<EmptyPlaceholder.Title>Du mangler følgende</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
Placeholder
</EmptyPlaceholder.Description>
</EmptyPlaceholder>
</DashboardShell>
)
}
const hasContract =
tenantDetails.contracts && tenantDetails.contracts.length > 0

return (
<DashboardShell>
<DashboardHeader
heading="Byggninger"
text="Hvordan byggning skal leietakeren inn i?"
/>
<KpiDetailsForm tenantDetails={tenantDetails} />
<DashboardHeader heading="KPI" text="Hvilke KPI skal leietakeren ha?" />
{hasContract ? (
<KpiDetailsForm tenantDetails={tenantDetails} />
) : (
<ContractCheck tenantDetails={tenantDetails} />
)}
</DashboardShell>
)
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from "react"
import Link from "next/link"
import { getTenantDetails } from "@/actions/get-tenant-details"

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

import { DashboardHeader } from "@/components/dashboard/header"
import { DashboardShell } from "@/components/dashboard/shell"
import { EmptyPlaceholder } from "@/components/shared/empty-placeholder"
import { ContractCheck } from "@/components/tenant/ContractCheck"

import { LandlordDetailsForm } from "./_components/LandlordDetailsForm"

Expand All @@ -15,7 +17,7 @@ export default async function LandlordContract({
params: { id: string }
}) {
const tenantId = params.id
const currentPath = `/tenant/${tenantId}/contract2/landlord`
const currentPath = `/tenant/${tenantId}/contract/landlord`

try {
const tenantDetails = await getTenantDetails(tenantId)
Expand All @@ -28,28 +30,17 @@ export default async function LandlordContract({
)
}

if (tenantDetails.name && parseInt(tenantDetails.name) > 0) {
return (
<DashboardShell>
<DashboardHeader
heading="Utleier"
text="Hvem er utleier på bygget?"
/>
<EmptyPlaceholder>
<EmptyPlaceholder.Icon name="help" />
<EmptyPlaceholder.Title>Du mangler følgende</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
Placeholder
</EmptyPlaceholder.Description>
</EmptyPlaceholder>
</DashboardShell>
)
}
const hasContract =
tenantDetails.contracts && tenantDetails.contracts.length > 0

return (
<DashboardShell>
<DashboardHeader heading="Utleier" text="Hvem er utleier på bygget?" />
<LandlordDetailsForm tenantDetails={tenantDetails} />
{hasContract ? (
<LandlordDetailsForm tenantDetails={tenantDetails} />
) : (
<ContractCheck tenantDetails={tenantDetails} />
)}
</DashboardShell>
)
} catch (error) {
Expand Down
Loading

0 comments on commit 16ec019

Please sign in to comment.