-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Codehagen/41-feature-ux-and-error-handling
41 feature ux and error handling
- Loading branch information
Showing
43 changed files
with
971 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use server" | ||
|
||
import { revalidatePath } from "next/cache" | ||
|
||
import { prisma } from "@/lib/db" | ||
import { getCurrentUser } from "@/lib/session" | ||
|
||
export async function deleteTenant(tenantId: string) { | ||
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 { | ||
// Check if the tenant exists | ||
const tenant = await prisma.tenant.findUnique({ | ||
where: { id: tenantId }, | ||
include: { property: true }, | ||
}) | ||
|
||
if (!tenant) { | ||
throw new Error("Tenant not found") | ||
} | ||
|
||
// Check if the user has permission to delete this tenant | ||
const userWorkspace = await prisma.workspace.findFirst({ | ||
where: { users: { some: { id: userId } } }, | ||
include: { properties: true }, | ||
}) | ||
|
||
if ( | ||
!userWorkspace || | ||
!userWorkspace.properties.some((p) => p.id === tenant.propertyId) | ||
) { | ||
throw new Error("You don't have permission to delete this tenant") | ||
} | ||
|
||
// Delete related records first | ||
await prisma.$transaction([ | ||
// Delete contracts first | ||
prisma.contract.deleteMany({ where: { tenantId } }), | ||
// Then delete other related records | ||
prisma.customerInvoice.deleteMany({ where: { tenantId } }), | ||
prisma.tenantCommunications.deleteMany({ where: { tenantId } }), | ||
prisma.contactPerson.deleteMany({ where: { tenantId } }), | ||
// Finally, delete the tenant | ||
prisma.tenant.delete({ where: { id: tenantId } }), | ||
]) | ||
|
||
console.log(`Deleted tenant with ID: ${tenantId}`) | ||
|
||
revalidatePath("/tenant") | ||
|
||
return { success: true } | ||
} catch (error) { | ||
console.error(`Error deleting tenant with ID: ${tenantId}`, error) | ||
return { success: false, error: error.message } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use server" | ||
|
||
import { revalidatePath } from "next/cache" | ||
|
||
import { prisma } from "@/lib/db" | ||
import { getCurrentUser } from "@/lib/session" | ||
|
||
export async function updateTenant( | ||
tenantId: string, | ||
tenantData: { | ||
name: string | ||
orgnr?: number | null | ||
numEmployees: number | ||
buildingId: string | ||
floorId?: string | null | ||
officeSpaceId?: string | null | ||
propertyId: string | ||
}, | ||
) { | ||
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 { | ||
// Check if the tenant exists | ||
const tenant = await prisma.tenant.findUnique({ | ||
where: { id: tenantId }, | ||
include: { property: true }, | ||
}) | ||
|
||
if (!tenant) { | ||
throw new Error("Tenant not found") | ||
} | ||
|
||
// Check if the user has permission to update this tenant | ||
const userWorkspace = await prisma.workspace.findFirst({ | ||
where: { users: { some: { id: userId } } }, | ||
include: { properties: true }, | ||
}) | ||
|
||
if ( | ||
!userWorkspace || | ||
!userWorkspace.properties.some((p) => p.id === tenant.propertyId) | ||
) { | ||
throw new Error("You don't have permission to update this tenant") | ||
} | ||
|
||
// Update the tenant | ||
const updatedTenant = await prisma.tenant.update({ | ||
where: { id: tenantId }, | ||
data: { | ||
name: tenantData.name, | ||
orgnr: tenantData.orgnr, | ||
numEmployees: tenantData.numEmployees, | ||
buildingId: tenantData.buildingId, | ||
floorId: tenantData.floorId, | ||
officeSpaceId: tenantData.officeSpaceId, | ||
propertyId: tenantData.propertyId, | ||
}, | ||
}) | ||
|
||
console.log(`Updated tenant with ID: ${tenantId}`) | ||
|
||
revalidatePath("/tenant") | ||
|
||
return { success: true, tenant: updatedTenant } | ||
} catch (error) { | ||
console.error(`Error updating tenant with ID: ${tenantId}`, error) | ||
return { success: false, error: error.message } | ||
} | ||
} |
Oops, something went wrong.