-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathgetFileSystemTreeRoute.ts
More file actions
34 lines (31 loc) · 1.59 KB
/
getFileSystemTreeRoute.ts
File metadata and controls
34 lines (31 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import type { AppFastifyInstance } from '#app/applicationTypes';
import { CodeTaskServiceImpl } from '#codeTask/codeTaskServiceImpl';
import { sendNotFound, sendServerError } from '#fastify/responses';
import { logger } from '#o11y/logger';
import { CODE_TASK_API } from '#shared/codeTask/codeTask.api';
import { currentUser } from '#user/userContext';
import { registerApiRoute } from '../routeUtils';
export async function getFileSystemTreeRoute(fastify: AppFastifyInstance): Promise<void> {
const codeTaskService = new CodeTaskServiceImpl(fastify.codeTaskRepository);
registerApiRoute(fastify, CODE_TASK_API.getFileSystemTree, async (request, reply) => {
const userId = currentUser().id;
const { codeTaskId } = request.params;
const { path } = request.query; // path is optional
try {
const tree = await codeTaskService.getFileSystemTree(userId, codeTaskId, path);
if (!tree) {
// This case might occur if the root path itself is invalid or inaccessible,
// though FileSystemService might throw before this.
return sendNotFound(reply, `File system tree not found for path: ${path || '/'}`);
}
return reply.sendJSON(tree);
} catch (error: any) {
logger.error(error, `Error getting file system tree for codeTask ${codeTaskId} (path: ${path || '/'}), user ${userId}`);
if (error.message?.includes('not found')) {
return sendNotFound(reply, `Code task or path not found: ${error.message}`);
}
// Add more specific error handling if service throws typed errors for state issues
return sendServerError(reply, error.message || 'Failed to get file system tree');
}
});
}