forked from Hahfyeex/Stellar-PolyMarket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentActions.js
More file actions
69 lines (57 loc) · 2.14 KB
/
Copy pathcommentActions.js
File metadata and controls
69 lines (57 loc) · 2.14 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use strict";
const express = require("express");
const router = express.Router();
const db = require("../db");
const logger = require("../utils/logger");
const jwtAuth = require("../middleware/jwtAuth");
// POST /api/comments/:id/thumbs-up (JWT required, one per wallet)
router.post("/:id/thumbs-up", jwtAuth, async (req, res) => {
const commentId = parseInt(req.params.id, 10);
const walletAddress = req.admin?.sub || req.admin?.wallet_address;
try {
// Insert deduplication record — PK constraint prevents duplicates
await db.query("INSERT INTO comment_thumbs_up (comment_id, wallet_address) VALUES ($1, $2)", [
commentId,
walletAddress,
]);
const { rows } = await db.query(
"UPDATE market_comments SET thumbs_up_count = thumbs_up_count + 1 WHERE id = $1 RETURNING thumbs_up_count",
[commentId]
);
if (rows.length === 0) {
return res.status(404).json({ error: "Comment not found" });
}
res.json({ thumbs_up_count: rows[0].thumbs_up_count });
} catch (err) {
if (err.code === "23505") {
return res.status(409).json({ error: "Already thumbed up" });
}
if (err.code === "23503") {
return res.status(404).json({ error: "Comment not found" });
}
logger.error({ err: err.message, commentId }, "Failed to thumbs-up comment");
res.status(500).json({ error: "Internal server error" });
}
});
// DELETE /api/comments/:id (admin JWT required — sets is_hidden = TRUE)
router.delete("/:id", jwtAuth, async (req, res) => {
const commentId = parseInt(req.params.id, 10);
// Require admin role
if (!req.admin?.isAdmin) {
return res.status(403).json({ error: "Admin access required" });
}
try {
const { rows } = await db.query(
"UPDATE market_comments SET is_hidden = TRUE WHERE id = $1 RETURNING id",
[commentId]
);
if (rows.length === 0) {
return res.status(404).json({ error: "Comment not found" });
}
res.json({ success: true });
} catch (err) {
logger.error({ err: err.message, commentId }, "Failed to hide comment");
res.status(500).json({ error: "Internal server error" });
}
});
module.exports = router;