import { KnowledgeEntry } from '@prisma/client'; import { NotFoundError, ConflictError } from '@/common/errors'; import { knowledgeRepository, KnowledgeRepository, CreateKnowledgeEntryData, RetrieveFilters, } from '../repository'; export class KnowledgeService { constructor(private readonly repo: KnowledgeRepository = knowledgeRepository) {} /** FR-002: new entries always default to status: draft, version: 1 — set by the repository. */ async create(data: CreateKnowledgeEntryData): Promise { return this.repo.create(data); } async publish(code: string, effectiveDate?: Date): Promise { const updated = await this.repo.publish(code, effectiveDate); if (!updated) throw new NotFoundError('Knowledge entry not found.'); return updated; } async unpublish(code: string): Promise { const updated = await this.repo.unpublish(code); if (!updated) throw new NotFoundError('Knowledge entry not found.'); return updated; } async setValidationStatus(code: string, validationStatus: string): Promise { const updated = await this.repo.setValidationStatus(code, validationStatus); if (!updated) throw new NotFoundError('Knowledge entry not found.'); return updated; } /** FR-004: an edit always creates a new version; a stale `expectedVersion` is rejected. */ async edit( code: string, expectedVersion: number, content: Omit, ): Promise { const updated = await this.repo.createNewVersion(code, expectedVersion, content); if (!updated) { throw new ConflictError( 'Knowledge entry was modified by another request — refresh and retry.', ); } return updated; } async listVersions(code: string): Promise { const versions = await this.repo.findAllVersionsByCode(code); if (versions.length === 0) throw new NotFoundError('Knowledge entry not found.'); return versions; } async retrieve(filters: RetrieveFilters): Promise { return this.repo.retrieve(filters); } /** 012-admin-list-views follow-up: every entry for a product, any status — the governance * screen's own data source (unlike `retrieve`, which is published-only). */ async listForGovernance(productId: string): Promise { return this.repo.findAllForProduct(productId); } } export const knowledgeService = new KnowledgeService();