2025-08-05 05:37:48 +08:00
|
|
|
import { getReport } from '@buster/database';
|
|
|
|
import type { GetReportIndividualResponse } from '@buster/server-shared/reports';
|
2025-08-06 02:02:40 +08:00
|
|
|
import { markdownToPlatejs } from '@buster/server-utils/report';
|
2025-08-03 08:38:50 +08:00
|
|
|
import { Hono } from 'hono';
|
|
|
|
import { HTTPException } from 'hono/http-exception';
|
2025-08-05 09:27:18 +08:00
|
|
|
import { standardErrorHandler } from '../../../../utils/response';
|
2025-08-03 08:38:50 +08:00
|
|
|
|
2025-08-05 05:37:48 +08:00
|
|
|
export async function getReportHandler(
|
2025-08-03 08:38:50 +08:00
|
|
|
reportId: string,
|
2025-08-05 05:37:48 +08:00
|
|
|
user: { id: string }
|
2025-08-03 11:48:15 +08:00
|
|
|
): Promise<GetReportIndividualResponse> {
|
2025-08-05 05:37:48 +08:00
|
|
|
const report = await getReport({ reportId, userId: user.id });
|
|
|
|
|
2025-08-08 05:09:49 +08:00
|
|
|
try {
|
|
|
|
const platejsResult = await markdownToPlatejs(report.content);
|
|
|
|
|
|
|
|
if (platejsResult.error) {
|
|
|
|
console.error('Error converting markdown to PlateJS:', platejsResult.error);
|
|
|
|
throw new HTTPException(500, {
|
|
|
|
message: 'Error converting markdown to PlateJS',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = platejsResult.elements ?? [];
|
|
|
|
|
|
|
|
const response: GetReportIndividualResponse = {
|
|
|
|
...report,
|
|
|
|
content,
|
|
|
|
};
|
2025-08-06 02:02:40 +08:00
|
|
|
|
2025-08-08 05:09:49 +08:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error converting markdown to PlateJS:', error);
|
2025-08-06 02:02:40 +08:00
|
|
|
throw new HTTPException(500, {
|
2025-08-08 05:09:49 +08:00
|
|
|
message: 'Error converting markdown',
|
2025-08-06 02:02:40 +08:00
|
|
|
});
|
|
|
|
}
|
2025-08-03 08:38:50 +08:00
|
|
|
}
|
|
|
|
|
2025-08-05 09:27:18 +08:00
|
|
|
const app = new Hono()
|
|
|
|
.get('/', async (c) => {
|
|
|
|
const reportId = c.req.param('id');
|
|
|
|
const user = c.get('busterUser');
|
2025-08-03 10:15:48 +08:00
|
|
|
|
2025-08-05 09:27:18 +08:00
|
|
|
if (!reportId) {
|
|
|
|
throw new HTTPException(404, { message: 'Report ID is required' });
|
|
|
|
}
|
2025-08-03 10:15:48 +08:00
|
|
|
|
2025-08-05 09:27:18 +08:00
|
|
|
const response: GetReportIndividualResponse = await getReportHandler(reportId, user);
|
|
|
|
return c.json(response);
|
|
|
|
})
|
|
|
|
.onError(standardErrorHandler);
|
2025-08-03 08:38:50 +08:00
|
|
|
|
|
|
|
export default app;
|