import type { SimulationFunction } from '@x12i/api-simulator'; import { returnBook, LibraryError } from '../state/checkout-ledger.js'; /** POST /books/:bookId/return { memberId } */ export const returnBookHandler: SimulationFunction = ({ request }) => { const bookId = request.params.bookId ?? ''; const memberId = (request.body as { memberId?: string } | undefined)?.memberId; if (!memberId) { return { status: 400, body: { error: 'MEMBER_ID_REQUIRED', message: 'Request body must include memberId.' } }; } try { const record = returnBook(bookId, memberId); return { status: 200, body: { ...record, status: 'returned' } }; } catch (error) { if (error instanceof LibraryError) { return { status: error.status, body: { error: error.code, message: error.message } }; } throw error; } };