@@ -13,86 +13,14 @@ export const getComments = async (
1313 const input = CommentActionInput . getComments . parse ( _input ) ;
1414
1515 const query = sql `
16- WITH RECURSIVE comment_tree AS (
17- -- Root comments
18- SELECT
19- id, body, user_id, created_at, resource_id, resource_type,
20- 0 as level,
21- id as root_id
22- FROM comments
23- WHERE resource_id = $1
24- AND resource_type = $2
25-
26- UNION ALL
27-
28- -- Nested replies (up to level 3)
29- SELECT
30- c.id, c.body, c.user_id, c.created_at, c.resource_id, c.resource_type,
31- ct.level + 1,
32- ct.root_id
33- FROM comments c
34- JOIN comment_tree ct ON c.resource_id = ct.id
35- WHERE c.resource_type = 'COMMENT'
36- AND ct.level < 3 -- This allows up to level 3 (0, 1, 2, 3)
37- ),
38- -- Function to build replies recursively
39- build_replies(parent_id, max_level) AS (
40- SELECT
41- ct.resource_id as parent_id,
42- 3 as max_level,
43- json_agg(
44- json_build_object(
45- 'id', ct.id,
46- 'body', ct.body,
47- 'level', ct.level,
48- 'created_at', ct.created_at,
49- 'parent_id', ct.resource_id,
50- 'author', json_build_object(
51- 'name', u.name,
52- 'email', u.email
53- ),
54- 'replies', CASE
55- WHEN ct.level < 3 THEN
56- COALESCE(
57- (SELECT json_agg(child_reply ORDER BY (child_reply->>'created_at')::timestamp)
58- FROM build_replies(ct.id, 3) br
59- CROSS JOIN json_array_elements(br.replies) as child_reply),
60- '[]'::json
61- )
62- ELSE '[]'::json
63- END
64- ) ORDER BY ct.created_at
65- ) as replies
66- FROM comment_tree ct
67- JOIN users u ON ct.user_id = u.id
68- WHERE ct.resource_id = parent_id AND ct.level > 0
69- GROUP BY ct.resource_id
70- )
71- SELECT json_agg(
72- json_build_object(
73- 'id', ct.id,
74- 'body', ct.body,
75- 'level', ct.level,
76- 'created_at', ct.created_at,
77- 'parent_id', null,
78- 'author', json_build_object(
79- 'name', u.name,
80- 'email', u.email
81- ),
82- 'replies', COALESCE(br.replies, '[]'::json)
83- ) ORDER BY ct.created_at
84- ) as comments
85- FROM comment_tree ct
86- JOIN users u ON ct.user_id = u.id
87- LEFT JOIN build_replies(ct.id, 3) br ON true
88- WHERE ct.level = 0;
16+ SELECT get_comments($1, $2);
8917 ` ;
9018
91- const comments = await pgClient ?. executeSQL ( query , [
19+ const execution_response : any = await pgClient ?. executeSQL ( query , [
9220 input . resource_id ,
9321 input . resource_type ,
9422 ] ) ;
95- return comments ?. rows ?. [ 0 ] ; // Placeholder for actual database query
23+ return execution_response ?. rows ?. [ 0 ] ?. get_comments ;
9624} ;
9725
9826export const createMyComment = async (
0 commit comments