feat: Complete lease approval workflow implementation (#136)
* Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Empty commit to trigger CI --------- Co-authored-by: open-swe-dev-andrinoid[bot] <open-swe-dev-andrinoid@users.noreply.github.com>
This commit is contained in:
parent
a5886eb01e
commit
52439c1178
@ -1,8 +1,10 @@
|
|||||||
// routes/api/lease/approve.ts
|
// routes/api/lease/approve.ts
|
||||||
import { define } from "../../../utils.ts";
|
import { define } from "../../../utils.ts";
|
||||||
import { page } from "fresh";
|
|
||||||
import { SessionCookie } from "../../auth/callback.ts";
|
import { SessionCookie } from "../../auth/callback.ts";
|
||||||
import { approveLease, getLeaseById } from "../../../services/leaseService.ts";
|
import { approveLease, getLeaseById } from "../../../services/leaseService.ts";
|
||||||
|
import { getUserById } from "../../../services/userService.ts";
|
||||||
|
import { getPropertyById } from "../../../services/propertyService.ts";
|
||||||
|
import { sendLeaseApproved } from "../../../services/notificationService.ts";
|
||||||
|
|
||||||
export const handler = define.handlers({
|
export const handler = define.handlers({
|
||||||
async POST(ctx) {
|
async POST(ctx) {
|
||||||
@ -78,10 +80,30 @@ export const handler = define.handlers({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const approvedLease = result[0];
|
||||||
|
|
||||||
|
if (lease.tenant_id) {
|
||||||
|
const tenant = await getUserById(lease.tenant_id);
|
||||||
|
const landlord = await getUserById(user_id);
|
||||||
|
const property = lease.property_id
|
||||||
|
? await getPropertyById(lease.property_id)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
await sendLeaseApproved(lease.tenant_id, {
|
||||||
|
lease_id: lease.id,
|
||||||
|
property_address: property?.address_1
|
||||||
|
? `${property.address_1}, ${property.city}, ${property.state}`
|
||||||
|
: lease.property_address || "Property",
|
||||||
|
tenant_name: tenant?.name || "Tenant",
|
||||||
|
landlord_name: landlord?.name || "Landlord",
|
||||||
|
monthly_rent: property?.monthly_rent ?? lease.rent ?? undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
lease: result[0],
|
lease: approvedLease,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
// routes/api/lease/reject.ts
|
// routes/api/lease/reject.ts
|
||||||
import { define } from "../../../utils.ts";
|
import { define } from "../../../utils.ts";
|
||||||
import { page } from "fresh";
|
|
||||||
import { SessionCookie } from "../../auth/callback.ts";
|
import { SessionCookie } from "../../auth/callback.ts";
|
||||||
import { rejectLease, getLeaseById } from "../../../services/leaseService.ts";
|
import { getLeaseById, rejectLease } from "../../../services/leaseService.ts";
|
||||||
|
import { getUserById } from "../../../services/userService.ts";
|
||||||
|
import { getPropertyById } from "../../../services/propertyService.ts";
|
||||||
|
import { sendLeaseRejected } from "../../../services/notificationService.ts";
|
||||||
|
|
||||||
export const handler = define.handlers({
|
export const handler = define.handlers({
|
||||||
async POST(ctx) {
|
async POST(ctx) {
|
||||||
@ -80,10 +82,30 @@ export const handler = define.handlers({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const rejectedLease = result[0];
|
||||||
|
|
||||||
|
if (lease.tenant_id) {
|
||||||
|
const tenant = await getUserById(lease.tenant_id);
|
||||||
|
const landlord = await getUserById(user_id);
|
||||||
|
const property = lease.property_id
|
||||||
|
? await getPropertyById(lease.property_id)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
await sendLeaseRejected(lease.tenant_id, {
|
||||||
|
lease_id: lease.id,
|
||||||
|
property_address: property?.address_1
|
||||||
|
? `${property.address_1}, ${property.city}, ${property.state}`
|
||||||
|
: lease.property_address || "Property",
|
||||||
|
tenant_name: tenant?.name || "Tenant",
|
||||||
|
landlord_name: landlord?.name || "Landlord",
|
||||||
|
monthly_rent: property?.monthly_rent ?? lease.rent ?? undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
lease: result[0],
|
lease: rejectedLease,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
// routes/api/lease/request-approval.ts
|
// routes/api/lease/request-approval.ts
|
||||||
import { define } from "../../../utils.ts";
|
import { define } from "../../../utils.ts";
|
||||||
import { page } from "fresh";
|
|
||||||
import { SessionCookie } from "../../auth/callback.ts";
|
import { SessionCookie } from "../../auth/callback.ts";
|
||||||
import {
|
import { requestLeaseApproval } from "../../../services/leaseService.ts";
|
||||||
requestLeaseApproval,
|
|
||||||
getLeaseById,
|
|
||||||
} from "../../../services/leaseService.ts";
|
|
||||||
import {
|
import {
|
||||||
getTenantInviteById,
|
getTenantInviteById,
|
||||||
updateTenantInvite,
|
updateTenantInvite,
|
||||||
} from "../../../services/tenantInviteService.ts";
|
} from "../../../services/tenantInviteService.ts";
|
||||||
import { getPropertyById } from "../../../services/propertyService.ts";
|
import { getPropertyById } from "../../../services/propertyService.ts";
|
||||||
|
import { getUserById } from "../../../services/userService.ts";
|
||||||
|
import {
|
||||||
|
sendLeaseApprovalRequest,
|
||||||
|
} from "../../../services/notificationService.ts";
|
||||||
|
|
||||||
export const handler = define.handlers({
|
export const handler = define.handlers({
|
||||||
async POST(ctx) {
|
async POST(ctx) {
|
||||||
@ -99,6 +99,21 @@ export const handler = define.handlers({
|
|||||||
|
|
||||||
await updateTenantInvite(invite_id, { lease_id: lease.id });
|
await updateTenantInvite(invite_id, { lease_id: lease.id });
|
||||||
|
|
||||||
|
if (invite.landlord_id) {
|
||||||
|
const tenant = await getUserById(user_id);
|
||||||
|
const landlord = await getUserById(invite.landlord_id);
|
||||||
|
|
||||||
|
await sendLeaseApprovalRequest(invite.landlord_id, {
|
||||||
|
lease_id: lease.id,
|
||||||
|
property_address: property.address_1
|
||||||
|
? `${property.address_1}, ${property.city}, ${property.state}`
|
||||||
|
: property.title || "Property",
|
||||||
|
tenant_name: tenant?.name || "Tenant",
|
||||||
|
landlord_name: landlord?.name || "Landlord",
|
||||||
|
monthly_rent: property.monthly_rent ?? undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
418
routes/landlord/approvals/index.tsx
Normal file
418
routes/landlord/approvals/index.tsx
Normal file
@ -0,0 +1,418 @@
|
|||||||
|
// routes/landlord/approvals/index.tsx
|
||||||
|
import { define } from "../../../utils.ts";
|
||||||
|
import { page } from "fresh";
|
||||||
|
import { SessionCookie } from "../../auth/callback.ts";
|
||||||
|
import { createPublicId } from "../../../helpers.ts";
|
||||||
|
|
||||||
|
interface PendingApproval {
|
||||||
|
id: string;
|
||||||
|
property_id?: string;
|
||||||
|
tenant_id?: string;
|
||||||
|
approval_status?: string;
|
||||||
|
approval_requested_at?: number;
|
||||||
|
approved_at?: number;
|
||||||
|
approved_by?: string;
|
||||||
|
rejection_reason?: string | null;
|
||||||
|
property?: {
|
||||||
|
id: string;
|
||||||
|
title?: string;
|
||||||
|
address_1?: string;
|
||||||
|
address_2?: string;
|
||||||
|
city?: string;
|
||||||
|
state?: string;
|
||||||
|
zip?: string;
|
||||||
|
monthly_rent?: number;
|
||||||
|
bedrooms?: number;
|
||||||
|
bathrooms?: number;
|
||||||
|
} | null;
|
||||||
|
tenant?: {
|
||||||
|
id: string;
|
||||||
|
name?: string;
|
||||||
|
email?: string;
|
||||||
|
phone?: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface LandlordApprovalsData {
|
||||||
|
stateData?: SessionCookie;
|
||||||
|
approvals?: PendingApproval[];
|
||||||
|
error?: string;
|
||||||
|
publicId?: string;
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface LandlordApprovalsProps {
|
||||||
|
data: LandlordApprovalsData;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const handler = define.handlers({
|
||||||
|
async GET(ctx) {
|
||||||
|
const stateData: SessionCookie = ctx.state.data;
|
||||||
|
const { user_id } = stateData;
|
||||||
|
|
||||||
|
if (!user_id) {
|
||||||
|
return new Response("", {
|
||||||
|
status: 302,
|
||||||
|
headers: { Location: "/auth/login" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/lease/pending-approvals", {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to fetch pending approvals");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
const publicId = await createPublicId(user_id);
|
||||||
|
|
||||||
|
return page({
|
||||||
|
stateData,
|
||||||
|
approvals: data.approvals || [],
|
||||||
|
error: "",
|
||||||
|
publicId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error loading pending approvals:", error);
|
||||||
|
const publicId = await createPublicId(user_id);
|
||||||
|
return page({
|
||||||
|
stateData,
|
||||||
|
error: "Failed to load pending approvals",
|
||||||
|
publicId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async POST(ctx) {
|
||||||
|
const stateData: SessionCookie = ctx.state.data;
|
||||||
|
const { user_id } = stateData;
|
||||||
|
|
||||||
|
if (!user_id) {
|
||||||
|
return new Response("", {
|
||||||
|
status: 302,
|
||||||
|
headers: { Location: "/auth/login" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const formData = await ctx.req.formData();
|
||||||
|
const lease_id = formData.get("lease_id") as string;
|
||||||
|
const action = formData.get("action") as string;
|
||||||
|
const rejection_reason = formData.get("rejection_reason") as string || "";
|
||||||
|
|
||||||
|
if (!lease_id || !action) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({ error: "lease_id and action are required" }),
|
||||||
|
{
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const apiEndpoint = action === "approve"
|
||||||
|
? "/api/lease/approve"
|
||||||
|
: "/api/lease/reject";
|
||||||
|
|
||||||
|
const body = action === "reject"
|
||||||
|
? { lease_id, rejection_reason }
|
||||||
|
: { lease_id };
|
||||||
|
|
||||||
|
const response = await fetch(apiEndpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => ({
|
||||||
|
error: "Unknown error",
|
||||||
|
}));
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
error: errorData.error || "Failed to process approval",
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: response.status,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response("", {
|
||||||
|
status: 302,
|
||||||
|
headers: { Location: "/landlord/approvals" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing approval:", error);
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({ error: String(error) }),
|
||||||
|
{
|
||||||
|
status: 500,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default define.page(
|
||||||
|
function LandlordApprovals({ data }: LandlordApprovalsProps) {
|
||||||
|
const { approvals, error } = data;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div class="min-h-screen bg-gray-50">
|
||||||
|
<div class="bg-white shadow">
|
||||||
|
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||||
|
<div class="flex justify-between items-center py-6">
|
||||||
|
<div>
|
||||||
|
<h1 class="text-3xl font-bold text-gray-900">
|
||||||
|
Lease Approvals
|
||||||
|
</h1>
|
||||||
|
<p class="mt-1 text-sm text-gray-500">
|
||||||
|
Review and manage pending lease approval requests
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex space-x-3">
|
||||||
|
<a
|
||||||
|
href="/landlord/dashboard"
|
||||||
|
class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50"
|
||||||
|
>
|
||||||
|
Back to Dashboard
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||||
|
{error && (
|
||||||
|
<div class="mb-6 bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{approvals && approvals.length > 0
|
||||||
|
? (
|
||||||
|
<div class="bg-white shadow overflow-hidden sm:rounded-md">
|
||||||
|
<ul class="divide-y divide-gray-200">
|
||||||
|
{approvals.map((approval) => (
|
||||||
|
<li key={approval.id}>
|
||||||
|
<div class="px-4 py-4 sm:px-6">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<div class="flex items-center">
|
||||||
|
<div class="flex-shrink-0">
|
||||||
|
<div class="h-10 w-10 rounded-full bg-blue-100 flex items-center justify-center">
|
||||||
|
<span class="text-sm font-medium text-blue-600">
|
||||||
|
{approval.tenant?.name?.charAt(0) || "T"}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="ml-4">
|
||||||
|
<p class="text-sm font-medium text-blue-600 truncate">
|
||||||
|
{approval.tenant?.name || "Tenant"}
|
||||||
|
</p>
|
||||||
|
<span
|
||||||
|
class={`ml-2 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
|
||||||
|
approval.approval_status === "pending"
|
||||||
|
? "bg-yellow-100 text-yellow-800"
|
||||||
|
: approval.approval_status === "approved"
|
||||||
|
? "bg-green-100 text-green-800"
|
||||||
|
: "bg-red-100 text-red-800"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{approval.approval_status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-2 flex items-center text-sm text-gray-500">
|
||||||
|
<svg
|
||||||
|
class="flex-shrink-0 mr-1.5 h-5 w-5 text-gray-400"
|
||||||
|
viewBox="0 0 20 20"
|
||||||
|
fill="currentColor"
|
||||||
|
>
|
||||||
|
<path d="M2.003 5.884L10 9.882l7.997 7.997-1.414 1.414-4.95-4.95-7.071-7.071V13a2 2 0 002-2h12a2 2 0 002-2V6.414L10 9.882z" />
|
||||||
|
<path d="M18 8.118l-8 4-8-4V14a2 2 0 002 2h12a2 2 0 002-2V8.118z" />
|
||||||
|
</svg>
|
||||||
|
<p>{approval.tenant?.email}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<div class="sm:flex sm:justify-between">
|
||||||
|
<div class="sm:flex">
|
||||||
|
<p class="flex items-center text-sm text-gray-500">
|
||||||
|
Property: {approval.property?.title ||
|
||||||
|
approval.property?.address_1 ||
|
||||||
|
"Property Address"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="mt-2 flex items-center text-sm text-gray-500 sm:mt-0">
|
||||||
|
<p>
|
||||||
|
Monthly Rent: ${approval.property?.monthly_rent
|
||||||
|
? `$${approval.property.monthly_rent.toLocaleString()}/mo`
|
||||||
|
: "N/A"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mt-2 flex items-center text-sm text-gray-500 sm:mt-0">
|
||||||
|
<p>
|
||||||
|
Requested: {approval.approval_requested_at
|
||||||
|
? new Date(approval.approval_requested_at)
|
||||||
|
.toLocaleDateString()
|
||||||
|
: "Unknown date"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{approval.approval_status === "pending" && (
|
||||||
|
<div class="mt-4 space-y-3">
|
||||||
|
<div class="flex space-x-2">
|
||||||
|
<form method="POST" class="inline">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="lease_id"
|
||||||
|
value={approval.id}
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="action"
|
||||||
|
value="approve"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500"
|
||||||
|
>
|
||||||
|
Approve
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<form method="POST" class="inline">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="lease_id"
|
||||||
|
value={approval.id}
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="action"
|
||||||
|
value="reject"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
|
||||||
|
>
|
||||||
|
Reject
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form method="POST" class="mt-2">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="lease_id"
|
||||||
|
value={approval.id}
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="action"
|
||||||
|
value="reject"
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<label
|
||||||
|
for={`rejection_reason_${approval.id}`}
|
||||||
|
class="block text-sm font-medium text-gray-700"
|
||||||
|
>
|
||||||
|
Rejection Reason (optional)
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
id={`rejection_reason_${approval.id}`}
|
||||||
|
name="rejection_reason"
|
||||||
|
rows={3}
|
||||||
|
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
|
||||||
|
placeholder="Provide a reason for rejection..."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="mt-2">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
|
||||||
|
>
|
||||||
|
Reject with Reason
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{approval.approval_status === "approved" && (
|
||||||
|
<div class="mt-4 rounded-md bg-green-50 border border-green-200 px-4 py-3">
|
||||||
|
<p class="text-sm text-green-700">
|
||||||
|
This lease has been approved and is now active.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{approval.approval_status === "rejected" && (
|
||||||
|
<div class="mt-4 rounded-md bg-red-50 border border-red-200 px-4 py-3">
|
||||||
|
<p class="text-sm text-red-700">
|
||||||
|
This lease request has been rejected.
|
||||||
|
</p>
|
||||||
|
{approval.rejection_reason && (
|
||||||
|
<p class="mt-2 text-sm text-red-600">
|
||||||
|
<strong>Reason:</strong>{" "}
|
||||||
|
{approval.rejection_reason}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
: (
|
||||||
|
<div class="text-center py-12">
|
||||||
|
<svg
|
||||||
|
class="mx-auto h-12 w-12 text-gray-400"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
stroke-width="2"
|
||||||
|
d="M9 12l2 2 4-4m6 2l-4-4m6 2M9 12h6m-6 0h6m-6 0h6"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<h3 class="mt-2 text-sm font-medium text-gray-900">
|
||||||
|
No pending approvals
|
||||||
|
</h3>
|
||||||
|
<p class="mt-1 text-sm text-gray-500">
|
||||||
|
You don't have any pending lease approval requests at this
|
||||||
|
time.
|
||||||
|
</p>
|
||||||
|
<div class="mt-6">
|
||||||
|
<a
|
||||||
|
href="/landlord/dashboard"
|
||||||
|
class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700"
|
||||||
|
>
|
||||||
|
Return to Dashboard
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
424
tests/e2e/lease-approval.spec.ts
Normal file
424
tests/e2e/lease-approval.spec.ts
Normal file
@ -0,0 +1,424 @@
|
|||||||
|
// tests/e2e/lease-approval.spec.ts
|
||||||
|
import { expect, Page, test } from "@playwright/test";
|
||||||
|
import { applyDeterministicPage, e2eHeaders, useE2EUser } from "./helpers.ts";
|
||||||
|
|
||||||
|
test("tenant can request lease approval", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Tenant requests lease approval
|
||||||
|
await useE2EUser(page, "tenant");
|
||||||
|
|
||||||
|
// Create a tenant invite first
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(inviteResponse.ok()).toBeTruthy();
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
expect(inviteId).toBeTruthy();
|
||||||
|
|
||||||
|
// Request lease approval
|
||||||
|
const approvalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(approvalResponse.ok()).toBeTruthy();
|
||||||
|
|
||||||
|
const approvalPayload = await approvalResponse.json();
|
||||||
|
expect(approvalPayload?.success).toBeTruthy();
|
||||||
|
expect(approvalPayload?.lease).toBeDefined();
|
||||||
|
expect(approvalPayload?.lease?.approval_status).toBe("pending_approval");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("landlord can view pending approvals on dashboard", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
|
||||||
|
// Create a pending lease approval
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
await page.request.post("/api/lease/request-approval", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Navigate to landlord dashboard
|
||||||
|
await page.goto("/landlord/dashboard");
|
||||||
|
|
||||||
|
// Check for pending approvals section
|
||||||
|
await expect(page.getByRole("heading", { name: /pending lease approvals/i }))
|
||||||
|
.toBeVisible();
|
||||||
|
|
||||||
|
// Verify approval card is displayed
|
||||||
|
await expect(page.getByText(/taylor\.tenant/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(/skyline lofts/i)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("landlord can view pending approvals on dedicated page", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
|
||||||
|
// Create a pending lease approval
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
await page.request.post("/api/lease/request-approval", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Navigate to dedicated approvals page
|
||||||
|
await page.goto("/landlord/approvals");
|
||||||
|
|
||||||
|
// Check for page heading
|
||||||
|
await expect(page.getByRole("heading", { name: /lease approvals/i }))
|
||||||
|
.toBeVisible();
|
||||||
|
|
||||||
|
// Verify approval card is displayed
|
||||||
|
await expect(page.getByText(/taylor\.tenant/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(/skyline lofts/i)).toBeVisible();
|
||||||
|
|
||||||
|
// Verify approve and reject buttons are present
|
||||||
|
await expect(page.getByRole("button", { name: /approve/i })).toBeVisible();
|
||||||
|
await expect(page.getByRole("button", { name: /reject/i })).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("landlord can approve a lease", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
|
||||||
|
// Create a pending lease approval
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
const requestApprovalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestApprovalPayload = await requestApprovalResponse.json();
|
||||||
|
const leaseId = requestApprovalPayload?.lease?.id || "";
|
||||||
|
|
||||||
|
// Navigate to approvals page
|
||||||
|
await page.goto("/landlord/approvals");
|
||||||
|
|
||||||
|
// Submit approval via API
|
||||||
|
const approveResponse = await page.request.post("/api/lease/approve", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: leaseId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(approveResponse.ok()).toBeTruthy();
|
||||||
|
|
||||||
|
const approveData = await approveResponse.json();
|
||||||
|
expect(approveData?.success).toBeTruthy();
|
||||||
|
expect(approveData?.lease?.approval_status).toBe("approved");
|
||||||
|
expect(approveData?.lease?.approved_at).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("landlord can reject a lease with reason", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
|
||||||
|
// Create a pending lease approval
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
const requestApprovalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestApprovalPayload = await requestApprovalResponse.json();
|
||||||
|
const leaseId = requestApprovalPayload?.lease?.id || "";
|
||||||
|
|
||||||
|
// Navigate to approvals page
|
||||||
|
await page.goto("/landlord/approvals");
|
||||||
|
|
||||||
|
// Submit rejection via API
|
||||||
|
const rejectionReason = "Insufficient income verification";
|
||||||
|
const rejectResponse = await page.request.post("/api/lease/reject", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: leaseId,
|
||||||
|
rejection_reason: rejectionReason,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(rejectResponse.ok()).toBeTruthy();
|
||||||
|
|
||||||
|
const rejectData = await rejectResponse.json();
|
||||||
|
expect(rejectData?.success).toBeTruthy();
|
||||||
|
expect(rejectData?.lease?.approval_status).toBe("rejected");
|
||||||
|
expect(rejectData?.lease?.rejection_reason).toBe(rejectionReason);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("tenant sees updated approval status on invite page", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Create and approve a lease
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
const requestApprovalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestApprovalPayload = await requestApprovalResponse.json();
|
||||||
|
const leaseId = requestApprovalPayload?.lease?.id || "";
|
||||||
|
|
||||||
|
// Approve lease as landlord
|
||||||
|
await page.request.post("/api/lease/approve", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: leaseId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Navigate to tenant invite page
|
||||||
|
await useE2EUser(page, "tenant");
|
||||||
|
await page.goto(`/tenant/invite/${inviteId}`);
|
||||||
|
|
||||||
|
// Verify approved status is displayed
|
||||||
|
await expect(page.getByText(/lease approved/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(/congratulations/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(/tenant portal/i)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("tenant sees rejected status on invite page", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Create and reject a lease
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
const requestApprovalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestApprovalPayload = await requestApprovalResponse.json();
|
||||||
|
const leaseId = requestApprovalPayload?.lease?.id || "";
|
||||||
|
|
||||||
|
const rejectionReason = "Property not available";
|
||||||
|
await page.request.post("/api/lease/reject", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: leaseId,
|
||||||
|
rejection_reason: rejectionReason,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Navigate to tenant invite page
|
||||||
|
await useE2EUser(page, "tenant");
|
||||||
|
await page.goto(`/tenant/invite/${inviteId}`);
|
||||||
|
|
||||||
|
// Verify rejected status is displayed
|
||||||
|
await expect(page.getByText(/lease rejected/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(/declined/i)).toBeVisible();
|
||||||
|
await expect(page.getByText(rejectionReason)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("unauthorized user cannot request approval", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Try to request approval without authentication
|
||||||
|
const response = await page.request.post("/api/lease/request-approval", {
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
data: {
|
||||||
|
invite_id: "test-invite-id",
|
||||||
|
property_id: "test-property-id",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
const errorData = await response.json();
|
||||||
|
expect(errorData?.error).toBe("Unauthorized");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("unauthorized user cannot approve lease", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Try to approve without authentication
|
||||||
|
const response = await page.request.post("/api/lease/approve", {
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
data: {
|
||||||
|
lease_id: "test-lease-id",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
const errorData = await response.json();
|
||||||
|
expect(errorData?.error).toBe("Unauthorized");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("unauthorized user cannot reject lease", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Try to reject without authentication
|
||||||
|
const response = await page.request.post("/api/lease/reject", {
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
data: {
|
||||||
|
lease_id: "test-lease-id",
|
||||||
|
rejection_reason: "Test rejection",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
const errorData = await response.json();
|
||||||
|
expect(errorData?.error).toBe("Unauthorized");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("invalid lease_id returns error", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
|
||||||
|
// Try to approve with invalid lease_id
|
||||||
|
const response = await page.request.post("/api/lease/approve", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: "invalid-lease-id",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status()).toBe(404);
|
||||||
|
const errorData = await response.json();
|
||||||
|
expect(errorData?.error).toBe("Lease not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("landlord cannot approve lease they don't own", async ({ page }: { page: Page }) => {
|
||||||
|
await applyDeterministicPage(page);
|
||||||
|
|
||||||
|
// Create a lease as tenant
|
||||||
|
const inviteResponse = await page.request.post("/api/update/tenant_invite", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
invited_emails: ["taylor.tenant@example.com"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const invitePayload = await inviteResponse.json();
|
||||||
|
const inviteId = invitePayload?.invite?.id || "";
|
||||||
|
|
||||||
|
const requestApprovalResponse = await page.request.post(
|
||||||
|
"/api/lease/request-approval",
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("tenant") },
|
||||||
|
data: {
|
||||||
|
invite_id: inviteId,
|
||||||
|
property_id: "e2e-prop-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestApprovalPayload = await requestApprovalResponse.json();
|
||||||
|
const leaseId = requestApprovalPayload?.lease?.id || "";
|
||||||
|
|
||||||
|
// Try to approve as landlord (should fail)
|
||||||
|
await useE2EUser(page, "landlord");
|
||||||
|
const response = await page.request.post("/api/lease/approve", {
|
||||||
|
headers: { "Content-Type": "application/json", ...e2eHeaders("landlord") },
|
||||||
|
data: {
|
||||||
|
lease_id: leaseId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status()).toBe(403);
|
||||||
|
const errorData = await response.json();
|
||||||
|
expect(errorData?.error).toBe("Unauthorized: You do not own this lease");
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user