feat(task-comments): enhance comment response with user avatar and attachments
- Added functionality to retrieve user avatar URL and comment details, including created_at timestamp. - Implemented logic to fetch and format comment attachments for the response. - Transformed the response structure to include avatar, attachments, and other relevant comment data.
This commit is contained in:
@@ -217,7 +217,43 @@ export default class TaskCommentsController extends WorklenzControllerBase {
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(200).send(new ServerResponse(true, data.comment));
|
||||
// Get user avatar URL from database
|
||||
const avatarQuery = `SELECT avatar_url FROM users WHERE id = $1`;
|
||||
const avatarResult = await db.query(avatarQuery, [req.user?.id]);
|
||||
const avatarUrl = avatarResult.rows[0]?.avatar_url || "";
|
||||
|
||||
// Get comment details including created_at
|
||||
const commentQuery = `SELECT created_at FROM task_comments WHERE id = $1`;
|
||||
const commentResult = await db.query(commentQuery, [response.id]);
|
||||
const commentData = commentResult.rows[0];
|
||||
|
||||
// Get attachments if any
|
||||
const attachmentsQuery = `SELECT id, name, type, size FROM task_comment_attachments WHERE comment_id = $1`;
|
||||
const attachmentsResult = await db.query(attachmentsQuery, [response.id]);
|
||||
const commentAttachments = attachmentsResult.rows.map(att => ({
|
||||
id: att.id,
|
||||
name: att.name,
|
||||
type: att.type,
|
||||
size: att.size
|
||||
}));
|
||||
|
||||
// Transform the response to match the desired format
|
||||
const commentResponse = {
|
||||
attachments: commentAttachments,
|
||||
avatar_url: avatarUrl,
|
||||
content: req.body.content,
|
||||
created_at: commentData?.created_at || new Date().toISOString(),
|
||||
edit: false,
|
||||
id: response.id,
|
||||
member_name: req.user?.name || "",
|
||||
mentions: mentions || [],
|
||||
rawContent: req.body.content,
|
||||
reactions: { likes: {} },
|
||||
team_member_id: req.user?.team_member_id || "",
|
||||
user_id: req.user?.id || ""
|
||||
};
|
||||
|
||||
return res.status(200).send(new ServerResponse(true, commentResponse));
|
||||
}
|
||||
|
||||
@HandleExceptions()
|
||||
|
||||
Reference in New Issue
Block a user