Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

get.ts

33 lines · 1.4 KB · TypeScript
/**
 * Read one factory report with its findings and the current state of every
 * card it filed or linked. Read-only.
 *
 * @param input - `reportId` (`rp_…`)
 * @returns `{ report, url, cards: [{ cardId, title, relation, state, needsKent, cardUrl }] }`
 *
 * @example
 * import getReport from 'kody:@kody/kody-factory/reports/get'
 * await getReport({ reportId: 'rp_0123456789abcdef' })
 */
import { appBaseUrl } from '../../lib/links.ts'
import { getReport } from '../../reports/store.ts'
import { reportUrl } from '../../reports/post.ts'
import { loadJson, storageKeys } from '../../lib/storage.ts'
import { emptyLedger, type Ledger } from '../../core/work-item.ts'
import { cardUrl } from '../../cards/report.ts'

export default async function get(input: { reportId: string }) {
	const id = typeof input?.reportId === 'string' ? input.reportId.trim() : ''
	if (!id) throw new Error('reports/get: reportId is required')
	const report = await getReport(id)
	if (!report) throw new Error(`reports/get: report not found: ${id}`)
	const [ledger, base] = await Promise.all([loadJson<Ledger>(storageKeys.LEDGER_KEY, emptyLedger()), appBaseUrl()])
	return {
		report,
		url: reportUrl(report.id, base),
		cards: report.cards.map((c) => {
			const item = ledger.items[c.cardId]
			return { ...c, state: item?.state ?? 'missing', needsKent: item?.needsKent ?? c.needsKent, cardUrl: cardUrl(c.cardId, base) }
		}),
	}
}