// services/leaseDocumentService.ts // Handle lease documents, templates, and e-signature workflow import { and, eq } from "drizzle-orm"; import { db } from "../db/db.ts"; import { lease_documents } from "../db/schema.ts"; // export interface LeaseDocument { // id: string; // lease_id: string; // document_type: string | null; // 'lease', 'addendum', 'receipt' // file_url: string | null; // file_name: string | null; // signed_by_landlord: boolean | null; // signed_by_tenant: boolean | null; // signature_date: number | null; // created_on: number; // } // Create new lease document export async function createLeaseDocument( lease_id: string, document_type: string, file_url: string, file_name: string, ) { const now = Date.now(); return await db.insert(lease_documents) .values({ id: crypto.randomUUID(), lease_id, document_type, file_url, file_name, signed_by_landlord: false, signed_by_tenant: false, signature_date: null, created_on: now, }) .returning(); } // Get all documents for a lease export async function getDocumentsByLease(lease_id: string) { return await db.select() .from(lease_documents) .where(eq(lease_documents.lease_id, lease_id)); } // Get documents by type for a lease export async function getDocumentsByLeaseAndType( lease_id: string, document_type: string, ) { return await db.select() .from(lease_documents) .where(and( eq(lease_documents.lease_id, lease_id), eq(lease_documents.document_type, document_type), )); } // Get main lease document export async function getMainLeaseDocument(lease_id: string) { const result = await getDocumentsByLeaseAndType(lease_id, "lease"); return result[0] || null; } // Update landlord signature export async function signDocumentAsLandlord(id: string) { const now = Date.now(); return await db.update(lease_documents) .set({ signed_by_landlord: true, signature_date: now, }) .where(eq(lease_documents.id, id)) .returning(); } // Update tenant signature export async function signDocumentAsTenant(id: string) { const now = Date.now(); const document = await getDocumentById(id); return await db.update(lease_documents) .set({ signed_by_tenant: true, // Only update signature_date if landlord hasn't signed yet, or if both are signing signature_date: document?.signed_by_landlord ? now : (document?.signature_date || now), }) .where(eq(lease_documents.id, id)) .returning(); } // Get document by ID export async function getDocumentById(id: string) { const result = await db.select() .from(lease_documents) .where(eq(lease_documents.id, id)); return result[0] || null; } // Check if document is fully signed export async function isDocumentFullySigned(id: string) { const document = await getDocumentById(id); return document?.signed_by_landlord && document?.signed_by_tenant; } // Get signing status for a lease export async function getLeaseSigningStatus(lease_id: string) { const documents = await getDocumentsByLease(lease_id); const mainLease = documents.find((doc) => doc.document_type === "lease"); if (!mainLease) { return { has_lease_document: false, landlord_signed: false, tenant_signed: false, fully_signed: false, signature_date: null, }; } return { has_lease_document: true, landlord_signed: mainLease.signed_by_landlord || false, tenant_signed: mainLease.signed_by_tenant || false, fully_signed: (mainLease.signed_by_landlord && mainLease.signed_by_tenant) || false, signature_date: mainLease.signature_date, }; } // Delete document export async function deleteLeaseDocument(id: string) { return await db.delete(lease_documents) .where(eq(lease_documents.id, id)) .returning(); } // Update document file (replace with new version) export async function updateDocumentFile( id: string, file_url: string, file_name: string, ) { return await db.update(lease_documents) .set({ file_url, file_name, // Reset signatures when document is updated signed_by_landlord: false, signed_by_tenant: false, signature_date: null, }) .where(eq(lease_documents.id, id)) .returning(); }