|
1 | 1 | import z from "zod"; |
2 | 2 | import { CommentActionInput } from "./inputs/comment.input"; |
| 3 | +import { authID } from "./session.actions"; |
| 4 | +import { ActionException } from "./RepositoryException"; |
| 5 | +import { persistenceRepository } from "../persistence/persistence-repositories"; |
| 6 | +import { eq } from "sqlkit"; |
| 7 | + |
| 8 | +const sql = String.raw; |
3 | 9 |
|
4 | 10 | export const getComments = async ( |
5 | | - resourceId: string, |
6 | | - resourceType: "ARTICLE" | "COMMENT" |
| 11 | + _input: z.infer<typeof CommentActionInput.getComments> |
7 | 12 | ) => { |
8 | | - // Fetch comments from the database based on resourceId and resourceType |
9 | | - // const comments = await db |
10 | | - // .select() |
11 | | - // .from(commentsTable) |
12 | | - // .where(commentsTable.resource_id.eq(resourceId)) |
13 | | - // .and(commentsTable.resource_type.eq(resourceType)) |
14 | | - // .orderBy(commentsTable.created_at.desc()); |
15 | | - // return comments; |
16 | | - return []; // Placeholder for actual database query |
| 13 | + const input = CommentActionInput.getComments.parse(_input); |
| 14 | + |
| 15 | + 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; |
| 89 | + `; |
| 90 | + |
| 91 | + const comments = await pgClient?.executeSQL(query, [ |
| 92 | + input.resource_id, |
| 93 | + input.resource_type, |
| 94 | + ]); |
| 95 | + return comments?.rows?.[0]; // Placeholder for actual database query |
17 | 96 | }; |
18 | 97 |
|
19 | | -export const createComment = async ( |
| 98 | +export const createMyComment = async ( |
20 | 99 | input: z.infer<typeof CommentActionInput.create> |
21 | 100 | ) => { |
| 101 | + const sessionId = await authID(); |
| 102 | + if (!sessionId) { |
| 103 | + throw new ActionException("Unauthorized: No session ID found"); |
| 104 | + } |
22 | 105 | const { resource_id, resource_type, body } = input; |
23 | 106 |
|
24 | | - // Create the comment in the database |
| 107 | + switch (resource_type) { |
| 108 | + case "ARTICLE": |
| 109 | + // Validate that the resource exists |
| 110 | + const [exists] = await persistenceRepository.article.find({ |
| 111 | + where: eq("id", resource_id), |
| 112 | + limit: 1, |
| 113 | + columns: ["id"], |
| 114 | + }); |
| 115 | + if (!exists) { |
| 116 | + throw new ActionException("Resource not found"); |
| 117 | + } |
| 118 | + break; |
| 119 | + case "COMMENT": |
| 120 | + // Validate that the parent comment exists |
| 121 | + const [parentExists] = await persistenceRepository.comment.find({ |
| 122 | + where: eq("id", resource_id), |
| 123 | + limit: 1, |
| 124 | + columns: ["id"], |
| 125 | + }); |
| 126 | + if (!parentExists) { |
| 127 | + throw new ActionException("Parent comment not found"); |
| 128 | + } |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw new ActionException("Invalid resource type"); |
| 132 | + } |
| 133 | + |
| 134 | + const created = await persistenceRepository.comment.insert([ |
| 135 | + { |
| 136 | + body, |
| 137 | + resource_id, |
| 138 | + resource_type, |
| 139 | + user_id: sessionId, |
| 140 | + }, |
| 141 | + ]); |
25 | 142 |
|
26 | | - // return newComment[0]; |
| 143 | + return created?.rows?.[0]; |
27 | 144 | }; |
28 | 145 |
|
29 | 146 | export const deleteComment = async ( |
|
0 commit comments