Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions apps/page/pages/api/roadmap/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { getVisitorId } from "../../../lib/visitor-auth";

export default async function voteOnRoadmapItem(
req: NextApiRequest,
res: NextApiResponse<{ ok: boolean; vote_count?: number; error?: string }>
res: NextApiResponse<{ success: boolean; vote_count?: number; error?: string }>
) {
if (req.method !== "POST") {
return res.status(405).json({ ok: false, error: "Method not allowed" });
return res.status(405).json({ success: false, error: "Method not allowed" });
}

const { item_id } = req.body;

if (!item_id) {
return res.status(400).json({ ok: false, error: "Missing item_id" });
return res.status(400).json({ success: false, error: "Missing item_id" });
}

const visitor_id = await getVisitorId(req);
Expand All @@ -30,7 +30,7 @@ export default async function voteOnRoadmapItem(
if (itemCheckError || !itemCheck) {
return res
.status(404)
.json({ ok: false, error: "Item not found or not public" });
.json({ success: false, error: "Item not found or not public" });
}

// Check if user has already voted
Expand All @@ -52,7 +52,7 @@ export default async function voteOnRoadmapItem(
console.error("voteOnRoadmapItem [Delete Error]", deleteError);
return res
.status(500)
.json({ ok: false, error: "Failed to remove vote" });
.json({ success: false, error: "Failed to remove vote" });
}
} else {
// Add vote
Expand All @@ -66,7 +66,7 @@ export default async function voteOnRoadmapItem(

if (insertError) {
console.error("voteOnRoadmapItem [Insert Error]", insertError);
return res.status(500).json({ ok: false, error: "Failed to add vote" });
return res.status(500).json({ success: false, error: "Failed to add vote" });
}
}

Expand All @@ -81,11 +81,11 @@ export default async function voteOnRoadmapItem(
}

res.status(200).json({
ok: true,
success: true,
vote_count: count || 0,
});
} catch (e: Error | any) {
console.log("voteOnRoadmapItem [Error]", e);
res.status(500).json({ ok: false, error: "Internal server error" });
res.status(500).json({ success: false, error: "Internal server error" });
}
}
20 changes: 10 additions & 10 deletions apps/page/pages/api/roadmap/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { v4 } from "uuid";

type BulkVotesResponse = {
ok: boolean;
success: boolean;
votes: Record<string, { vote_count: number; user_voted: boolean }>;
};

Expand All @@ -18,31 +18,31 @@ export default async function getBulkRoadmapItemVotes(
// Validate HTTP method
if (req.method !== "POST") {
res.setHeader("Allow", "POST");
return res.status(405).json({ ok: false, votes: {} });
return res.status(405).json({ success: false, votes: {} });
}

const { item_ids } = req.body;
let { cp_pa_vid: visitor_id } = req.cookies;

// Input validation
if (!item_ids || !Array.isArray(item_ids)) {
return res.status(400).json({ ok: false, votes: {} });
return res.status(400).json({ success: false, votes: {} });
}

// Prevent abuse with max array length
if (item_ids.length > 100) {
return res.status(400).json({ ok: false, votes: {} });
return res.status(400).json({ success: false, votes: {} });
}

// Validate all item_ids are valid UUIDs
if (!item_ids.every((id) => typeof id === "string" && UUID_REGEX.test(id))) {
return res.status(400).json({ ok: false, votes: {} });
return res.status(400).json({ success: false, votes: {} });
}

// De-duplicate to keep queries lean
const distinctItemIds: string[] = Array.from(new Set(item_ids));
if (distinctItemIds.length === 0) {
return res.status(200).json({ ok: true, votes: {} });
return res.status(200).json({ success: true, votes: {} });
}

if (!visitor_id) {
Expand Down Expand Up @@ -76,7 +76,7 @@ export default async function getBulkRoadmapItemVotes(
"getBulkRoadmapItemVotes [User Error]",
userVoteResult.error
);
return res.status(500).json({ ok: false, votes: {} });
return res.status(500).json({ success: false, votes: {} });
}

// Check for any errors in vote count queries
Expand All @@ -87,7 +87,7 @@ export default async function getBulkRoadmapItemVotes(
distinctItemIds[i],
voteCountResults[i].error
);
return res.status(500).json({ ok: false, votes: {} });
return res.status(500).json({ success: false, votes: {} });
}
}

Expand All @@ -111,11 +111,11 @@ export default async function getBulkRoadmapItemVotes(
});

res.status(200).json({
ok: true,
success: true,
votes,
});
} catch (e: Error | any) {
console.log("getBulkRoadmapItemVotes [Error]", e);
res.status(500).json({ ok: false, votes: {} });
res.status(500).json({ success: false, votes: {} });
}
}