// Auto-generated Contract service import { queryAll, queryOne, execute } from "../db/client.js"; import type { Contract, CreateContractInput, UpdateContractInput } from "../types/index.js"; export class ContractService { /** List all contracts */ list(): Contract[] { return queryAll("SELECT * FROM contracts ORDER BY created_at DESC"); } /** Get by ID */ getById(id: string): Contract | undefined { return queryOne("SELECT * FROM contracts WHERE id = ?", [id]); } /** Create */ create(input: CreateContractInput): Contract { const id = crypto.randomUUID(); const now = new Date().toISOString(); const cols = ["id", "user_id", "title", "party_a", "party_b", "amount", "signed_at", "expires_at", "status", "file_url", "created_at", "updated_at"]; const values = [id, input.userId ?? null, input.title ?? null, input.partyA ?? null, input.partyB ?? null, input.amount ?? null, input.signedAt ?? null, input.expiresAt ?? null, input.status ?? null, input.fileUrl ?? null, now, now]; execute(`INSERT INTO contracts (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, values); return this.getById(id)!; } /** Update */ update(id: string, input: UpdateContractInput): Contract | undefined { const existing = this.getById(id); if (!existing) return undefined; const sets: string[] = []; const values: unknown[] = []; if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); } if (input.title !== undefined) { sets.push("title = ?"); values.push(input.title); } if (input.partyA !== undefined) { sets.push("party_a = ?"); values.push(input.partyA); } if (input.partyB !== undefined) { sets.push("party_b = ?"); values.push(input.partyB); } if (input.amount !== undefined) { sets.push("amount = ?"); values.push(input.amount); } if (input.signedAt !== undefined) { sets.push("signed_at = ?"); values.push(input.signedAt); } if (input.expiresAt !== undefined) { sets.push("expires_at = ?"); values.push(input.expiresAt); } if (input.status !== undefined) { sets.push("status = ?"); values.push(input.status); } if (input.fileUrl !== undefined) { sets.push("file_url = ?"); values.push(input.fileUrl); } if (sets.length === 0) return existing; sets.push("updated_at = ?"); values.push(new Date().toISOString()); values.push(id); execute(`UPDATE contracts SET ${sets.join(", ")} WHERE id = ?`, values); return this.getById(id)!; } /** Delete */ delete(id: string): boolean { const result = execute("DELETE FROM contracts WHERE id = ?", [id]); return result.changes > 0; } }