Q30: documents keep every version
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
794fa372c9
commit
4c475e5ceb
9 files changed
+183
-23
No files matched your search
@@ -3,6 +3,17 @@ import type { CreateDocumentInput, UpdateDocumentInput, DeleteDocumentInput } fr
|
||||
import { createId } from "../../shared/ids";
|
||||
import { AuditService } from "./audit-service";
|
||||
|
||||
export interface DocumentVersion {
|
||||
version: number;
|
||||
title: string;
|
||||
content: string;
|
||||
status: string;
|
||||
changed_by_type: string;
|
||||
changed_by_id: string | null;
|
||||
reason: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export class DocumentService {
|
||||
private readonly audit: AuditService;
|
||||
|
||||
@@ -14,9 +25,13 @@ export class DocumentService {
|
||||
const id = createId("doc");
|
||||
const now = new Date().toISOString();
|
||||
this.db.prepare(`
|
||||
insert into documents(id, org_id, title, doc_type, content, status, author_agent_id, reviewer_agent_id, project_id, goal_id, task_id, created_at, updated_at)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
insert into documents(id, org_id, title, doc_type, content, status, author_agent_id, reviewer_agent_id, project_id, goal_id, task_id, version, created_at, updated_at)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
|
||||
`).run(id, input.orgId, input.title.trim(), input.docType, input.content, "draft", input.authorAgentId, input.reviewerAgentId, input.projectId, input.goalId, input.taskId, now, now);
|
||||
this.writeVersion({
|
||||
orgId: input.orgId, documentId: id, version: 1, title: input.title.trim(), content: input.content, status: "draft",
|
||||
changedByType: input.authorAgentId ? "agent" : "employee", changedById: input.authorAgentId, reason: "created", at: now,
|
||||
});
|
||||
this.audit.record({
|
||||
orgId: input.orgId,
|
||||
entityType: "document",
|
||||
@@ -29,15 +44,44 @@ export class DocumentService {
|
||||
return id;
|
||||
}
|
||||
|
||||
update(input: UpdateDocumentInput): void {
|
||||
const result = this.db.prepare(`
|
||||
update documents set title = ?, content = ?, status = ?, updated_at = ? where id = ? and org_id = ?
|
||||
`).run(input.title.trim(), input.content, input.status, new Date().toISOString(), input.documentId, input.orgId);
|
||||
if (result.changes === 0) throw new Error("Document was not found.");
|
||||
/** Saves a new version; earlier versions stay readable. */
|
||||
update(input: UpdateDocumentInput): number {
|
||||
const reason = input.reason.trim();
|
||||
if (!reason) throw new Error("A document update needs a reason.");
|
||||
const current = this.db.prepare("select version from documents where id = ? and org_id = ?")
|
||||
.get(input.documentId, input.orgId) as { version: number } | undefined;
|
||||
if (!current) throw new Error("Document was not found.");
|
||||
const version = current.version + 1;
|
||||
const now = new Date().toISOString();
|
||||
this.db.prepare(`
|
||||
update documents set title = ?, content = ?, status = ?, version = ?, updated_at = ? where id = ? and org_id = ?
|
||||
`).run(input.title.trim(), input.content, input.status, version, now, input.documentId, input.orgId);
|
||||
this.writeVersion({
|
||||
orgId: input.orgId, documentId: input.documentId, version, title: input.title.trim(), content: input.content, status: input.status,
|
||||
changedByType: input.changedByType, changedById: input.changedById, reason, at: now,
|
||||
});
|
||||
return version;
|
||||
}
|
||||
|
||||
versions(orgId: string, documentId: string): DocumentVersion[] {
|
||||
return this.db.prepare(`
|
||||
select version, title, content, status, changed_by_type, changed_by_id, reason, created_at
|
||||
from document_versions where document_id = ? and org_id = ? order by version asc
|
||||
`).all(documentId, orgId) as unknown as DocumentVersion[];
|
||||
}
|
||||
|
||||
delete(input: DeleteDocumentInput): void {
|
||||
const result = this.db.prepare("delete from documents where id = ? and org_id = ?").run(input.documentId, input.orgId);
|
||||
if (result.changes === 0) throw new Error("Document was not found.");
|
||||
}
|
||||
|
||||
private writeVersion(input: {
|
||||
orgId: string; documentId: string; version: number; title: string; content: string; status: string;
|
||||
changedByType: string; changedById: string | null; reason: string; at: string;
|
||||
}): void {
|
||||
this.db.prepare(`
|
||||
insert into document_versions(id, org_id, document_id, version, title, content, status, changed_by_type, changed_by_id, reason, created_at)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(createId("dov"), input.orgId, input.documentId, input.version, input.title, input.content, input.status, input.changedByType, input.changedById, input.reason, input.at);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user