diff --git a/db/schema.ts b/db/schema.ts old mode 100755 new mode 100644 index 0fe7a6d..006538b --- a/db/schema.ts +++ b/db/schema.ts @@ -241,6 +241,7 @@ export const tenant_invites = pgTable("tenant_invites", { id: text("id").primaryKey(), landlord_id: text("landlord_id").references(() => users.id), property_id: text("property_id").references(() => properties.id), + lease_id: text("lease_id").references(() => leases.id), available_on: bigint("available_on", { mode: "number" }), invited_emails: text("invited_emails"), // JSON array of invited emails created_on: bigint("created_on", { mode: "number" }), diff --git a/services/leaseService.ts b/services/leaseService.ts index 839df86..2fc0601 100644 --- a/services/leaseService.ts +++ b/services/leaseService.ts @@ -233,6 +233,18 @@ export async function deleteLease(id: string) { .returning(); } +// Sign lease document +export async function signLease(lease_id: string, user_id: string) { + const now = Date.now(); + return await db.update(leases) + .set({ + lease_document_url: `signed_${lease_id}_${now}`, + updated_on: now, + }) + .where(eq(leases.id, lease_id)) + .returning(); +} + // Get lease statistics for an owner export async function getLeaseStatsByOwner(owner_id: string) { const allLeases = await getLeasesByOwner(owner_id); @@ -268,10 +280,17 @@ export async function inviteLease(lease_id: string, inviteId?: string) { const lease = await getLeaseById(lease_id); if (!lease) return null; + const property_id = lease.property_id; + const landlord_id = lease.owner_id; + + if (!property_id || !landlord_id) { + return null; + } + const existing = await db.select().from(tenant_invites).where( and( - eq(tenant_invites.property_id, lease.property_id), - eq(tenant_invites.landlord_id, lease.owner_id), + eq(tenant_invites.property_id, property_id), + eq(tenant_invites.landlord_id, landlord_id), ), ); @@ -286,8 +305,8 @@ export async function inviteLease(lease_id: string, inviteId?: string) { // Create a new invite linking to this lease return await db.insert(tenant_invites).values({ id: crypto.randomUUID(), - landlord_id: lease.owner_id, - property_id: lease.property_id, + landlord_id, + property_id, lease_id, available_on: null, invited_emails: JSON.stringify([]),