integratedhub/services/leaseDocumentService.ts
2026-01-20 09:02:26 -06:00

206 lines
5.3 KiB
TypeScript

// 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";
import {
createE2ELeaseDocument,
deleteE2ELeaseDocument,
getE2EDocumentById,
getE2EDocumentsByLease,
getE2EDocumentsByLeaseAndType,
isE2EEnabled,
signE2EDocumentAsLandlord,
signE2EDocumentAsTenant,
updateE2EDocumentFile,
} from "./e2eFixtures.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,
) {
if (isE2EEnabled()) {
return [createE2ELeaseDocument(
lease_id,
document_type,
file_url,
file_name,
)];
}
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) {
if (isE2EEnabled()) {
return getE2EDocumentsByLease(lease_id);
}
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,
) {
if (isE2EEnabled()) {
return getE2EDocumentsByLeaseAndType(lease_id, document_type);
}
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) {
if (isE2EEnabled()) {
const updated = signE2EDocumentAsLandlord(id);
return updated ? [updated] : [];
}
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) {
if (isE2EEnabled()) {
const updated = signE2EDocumentAsTenant(id);
return updated ? [updated] : [];
}
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) {
if (isE2EEnabled()) {
return getE2EDocumentById(id);
}
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) {
if (isE2EEnabled()) {
const deleted = deleteE2ELeaseDocument(id);
return deleted ? [deleted] : [];
}
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,
) {
if (isE2EEnabled()) {
const updated = updateE2EDocumentFile(id, file_url, file_name);
return updated ? [updated] : [];
}
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();
}