blockers.ts
1,564 bytes
| 1 | import { readFile } from 'node:fs/promises' |
|---|---|
| 2 | import path from 'node:path' |
| 3 | import type { FastifyInstance } from 'fastify' |
| 4 | import type { BlockersResponse } from '../../shared/types' |
| 5 | import { parseBlockedFile } from '../blockers/parse' |
| 6 | import type { SessionStore } from '../store/sessionStore' |
| 7 | |
| 8 | export interface BlockerRouteDeps { |
| 9 | store: SessionStore |
| 10 | } |
| 11 | |
| 12 | export function registerBlockerRoutes(app: FastifyInstance, deps: BlockerRouteDeps): void { |
| 13 | const { store } = deps |
| 14 | |
| 15 | app.post<{ Params: { id: string } }>('/api/sessions/:id/blockers', async (request, reply) => { |
| 16 | const session = await store.getSession(request.params.id) |
| 17 | if (!session) return reply.code(404).send({ error: 'session not found' }) |
| 18 | |
| 19 | const filePath = path.join(session.targetDir, 'BLOCKED.md') |
| 20 | const content = await readFile(filePath, 'utf8').catch(() => null) |
| 21 | if (content === null) { |
| 22 | const response: BlockersResponse = { questions: [] } |
| 23 | return reply.send(response) |
| 24 | } |
| 25 | |
| 26 | const questions = parseBlockedFile(content) |
| 27 | .map((entry) => entry.question) |
| 28 | .filter((question) => question.length > 0) |
| 29 | |
| 30 | if (questions.length === 0) { |
| 31 | const response: BlockersResponse = { questions: [] } |
| 32 | return reply.send(response) |
| 33 | } |
| 34 | |
| 35 | const [firstQuestion, ...remaining] = questions |
| 36 | await store.appendSegment(request.params.id, 'interviewer', firstQuestion) |
| 37 | await store.updateTurn(request.params.id, { openBlockers: remaining, status: 'interviewing' }) |
| 38 | |
| 39 | const response: BlockersResponse = { questions } |
| 40 | return reply.send(response) |
| 41 | }) |
| 42 | } |
| 43 | |