Files
16gagent/.benchmark/project-mgmt/backend/src/services/approval.ts
T
2026-06-06 10:40:48 +08:00

57 lines
2.3 KiB
TypeScript

// Auto-generated Approval service
import { queryAll, queryOne, execute } from "../db/client.js";
import type { Approval, CreateApprovalInput, UpdateApprovalInput } from "../types/index.js";
export class ApprovalService {
/** List all approvals */
list(): Approval[] {
return queryAll<Approval>("SELECT * FROM approvals ORDER BY created_at DESC");
}
/** Get by ID */
getById(id: string): Approval | undefined {
return queryOne<Approval>("SELECT * FROM approvals WHERE id = ?", [id]);
}
/** Create */
create(input: CreateApprovalInput): Approval {
const id = crypto.randomUUID();
const now = new Date().toISOString();
const cols = ["id", "user_id", "entity_type", "entity_id", "applicant_id", "status", "form_data", "created_at", "updated_at"];
const values = [id, input.userId ?? null, input.entityType ?? null, input.entityId ?? null, input.applicantId ?? null, input.status ?? null, input.formData ?? null, now, now];
execute(`INSERT INTO approvals (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
return this.getById(id)!;
}
/** Update */
update(id: string, input: UpdateApprovalInput): Approval | 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.entityType !== undefined) { sets.push("entity_type = ?"); values.push(input.entityType); }
if (input.entityId !== undefined) { sets.push("entity_id = ?"); values.push(input.entityId); }
if (input.applicantId !== undefined) { sets.push("applicant_id = ?"); values.push(input.applicantId); }
if (input.status !== undefined) { sets.push("status = ?"); values.push(input.status); }
if (input.formData !== undefined) { sets.push("form_data = ?"); values.push(input.formData); }
if (sets.length === 0) return existing;
sets.push("updated_at = ?");
values.push(new Date().toISOString());
values.push(id);
execute(`UPDATE approvals SET ${sets.join(", ")} WHERE id = ?`, values);
return this.getById(id)!;
}
/** Delete */
delete(id: string): boolean {
const result = execute("DELETE FROM approvals WHERE id = ?", [id]);
return result.changes > 0;
}
}