Skip to content

Commit fe018bb

Browse files
committed
fix: evoting reported issues
1 parent 7952d68 commit fe018bb

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

platforms/eVoting/src/app/(app)/[id]/page.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ export default function Vote({ params }: { params: Promise<{ id: string }> }) {
421421
<span className="font-medium text-gray-900">
422422
{result.optionText || `Option ${index + 1}`}
423423
</span>
424-
{result.isTied && (
424+
{result.isTied && blindVoteResults.totalVotes > 0 && (
425425
<Badge variant="success" className="bg-blue-500 text-white">
426426
🏆 Tied
427427
</Badge>
428428
)}
429-
{isWinner && !result.isTied && (
429+
{isWinner && !result.isTied && blindVoteResults.totalVotes > 0 && (
430430
<Badge variant="success" className="bg-green-500 text-white">
431431
🏆 Winner
432432
</Badge>
@@ -480,6 +480,9 @@ export default function Vote({ params }: { params: Promise<{ id: string }> }) {
480480
</div>
481481
<div className="text-xs text-blue-600">
482482
{resultsData.totalVotes || 0} of {resultsData.totalEligibleVoters} eligible voters
483+
{(resultsData.mode === "ereputation" || selectedPoll?.votingWeight === "ereputation") && resultsData.pointsVoted !== undefined && resultsData.totalEligiblePoints !== undefined && (
484+
<> ({resultsData.pointsVoted} points of {resultsData.totalEligiblePoints} total eligible points)</>
485+
)}
483486
</div>
484487
</div>
485488
</div>
@@ -521,7 +524,8 @@ export default function Vote({ params }: { params: Promise<{ id: string }> }) {
521524
} else {
522525
// Normal voting (including eReputation weighted normal voting): show votes and percentage
523526
const voteCount = result.votes || 0;
524-
displayValue = `${voteCount} votes`;
527+
// For eReputation mode, show "Points" instead of "votes"
528+
displayValue = resultsData.mode === "ereputation" ? `${voteCount} Points` : `${voteCount} votes`;
525529
// Calculate total from results array for percentage (handles both weighted and non-weighted)
526530
const totalVotesForPercentage = resultsData.results.reduce((sum, r) => sum + (r.votes || 0), 0);
527531
isWinner = voteCount === Math.max(...resultsData.results.map(r => r.votes || 0));
@@ -541,12 +545,12 @@ export default function Vote({ params }: { params: Promise<{ id: string }> }) {
541545
<span className="font-medium text-gray-900">
542546
{result.option}
543547
</span>
544-
{result.isTied && (
548+
{result.isTied && resultsData.totalVotes > 0 && (
545549
<Badge variant="success" className="bg-blue-500 text-white">
546550
🏆 Tied
547551
</Badge>
548552
)}
549-
{isWinner && !result.isTied && (
553+
{isWinner && !result.isTied && resultsData.totalVotes > 0 && (
550554
<Badge variant="success" className="bg-green-500 text-white">
551555
🏆 Winner
552556
</Badge>

platforms/eVoting/src/app/(app)/page.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ export default function Home() {
1717
const [currentPage, setCurrentPage] = useState(1);
1818
const [searchTerm, setSearchTerm] = useState("");
1919
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState("");
20-
const [sortField, setSortField] = useState<string>("deadline");
21-
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc");
20+
const [sortField, setSortField] = useState<string>(() => {
21+
if (typeof window !== "undefined") {
22+
return localStorage.getItem("evoting_sortField") || "deadline";
23+
}
24+
return "deadline";
25+
});
26+
const [sortDirection, setSortDirection] = useState<"asc" | "desc">(() => {
27+
if (typeof window !== "undefined") {
28+
return (localStorage.getItem("evoting_sortDirection") as "asc" | "desc") || "asc";
29+
}
30+
return "asc";
31+
});
2232
const itemsPerPage = 15;
2333

2434
useEffect(() => {
@@ -75,10 +85,18 @@ export default function Home() {
7585

7686
const handleSort = (field: string) => {
7787
if (sortField === field) {
78-
setSortDirection(sortDirection === "asc" ? "desc" : "asc");
88+
const newDirection = sortDirection === "asc" ? "desc" : "asc";
89+
setSortDirection(newDirection);
90+
if (typeof window !== "undefined") {
91+
localStorage.setItem("evoting_sortDirection", newDirection);
92+
}
7993
} else {
8094
setSortField(field);
8195
setSortDirection("asc");
96+
if (typeof window !== "undefined") {
97+
localStorage.setItem("evoting_sortField", field);
98+
localStorage.setItem("evoting_sortDirection", "asc");
99+
}
82100
}
83101
};
84102

platforms/evoting-api/src/services/PollService.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ export class PollService {
8989
const aIsActive = !a.deadline || new Date(a.deadline) > now;
9090
const bIsActive = !b.deadline || new Date(b.deadline) > now;
9191

92-
// ALWAYS show active polls first, UNLESS sorting by status
93-
if (sortField !== "status") {
94-
if (aIsActive && !bIsActive) return -1;
95-
if (!aIsActive && bIsActive) return 1;
96-
}
97-
98-
// If both are active or both are ended, apply the user's chosen sorting
92+
// Apply the user's chosen sorting
9993
let comparison = 0;
10094

10195
switch (sortField) {

platforms/evoting-api/src/services/VoteService.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,25 @@ export class VoteService {
275275
? Object.values(finalResults).reduce((sum, r) => sum + r.votes, 0)
276276
: Object.values(optionCounts).reduce((sum, count) => sum + count, 0);
277277

278+
// Calculate totalEligiblePoints and pointsVoted for eReputation
279+
let totalEligiblePoints: number | undefined;
280+
let pointsVoted: number | undefined;
281+
if (isWeighted && reputationResults) {
282+
// eReputation weighted normal voting: sum of all scores
283+
totalEligiblePoints = reputationResults.results.reduce((sum, r) => sum + r.score, 0);
284+
pointsVoted = totalWeightedVotes; // Sum of weighted votes
285+
}
286+
278287
return {
279288
pollId,
280289
totalVotes: votes.length, // Actual number of votes cast
281290
totalWeightedVotes: totalWeightedVotes, // Sum of all weighted votes (if weighted)
282291
totalEligibleVoters,
283292
turnout: totalEligibleVoters > 0 ? (votes.length / totalEligibleVoters) * 100 : 0,
284293
mode: isWeighted ? "ereputation" : "normal",
285-
results: finalResults
294+
results: finalResults,
295+
...(totalEligiblePoints !== undefined && { totalEligiblePoints }),
296+
...(pointsVoted !== undefined && { pointsVoted })
286297
};
287298
} else if (poll.mode === "point") {
288299
// STEP 1: Calculate point-based results normally (without eReputation weighting)
@@ -341,14 +352,25 @@ export class VoteService {
341352
// Sort by total points (highest first)
342353
finalResults.sort((a, b) => b.totalPoints - a.totalPoints);
343354

355+
// Calculate totalEligiblePoints and pointsVoted for eReputation weighted points-based voting
356+
let totalEligiblePoints: number | undefined;
357+
let pointsVoted: number | undefined;
358+
if (reputationResults) {
359+
// eReputation weighted points-based voting: sum of all scores * 100 (each user has 100 points)
360+
totalEligiblePoints = reputationResults.results.reduce((sum, r) => sum + r.score, 0) * 100;
361+
pointsVoted = totalWeightedPoints; // Sum of weighted points
362+
}
363+
344364
return {
345365
pollId,
346366
totalVotes,
347367
totalWeightedPoints,
348368
totalEligibleVoters,
349369
turnout: totalEligibleVoters > 0 ? (totalVotes / totalEligibleVoters) * 100 : 0,
350370
mode: "ereputation",
351-
results: finalResults
371+
results: finalResults,
372+
...(totalEligiblePoints !== undefined && { totalEligiblePoints }),
373+
...(pointsVoted !== undefined && { pointsVoted })
352374
};
353375
} else {
354376
// No weighting - use normal points
@@ -369,14 +391,21 @@ export class VoteService {
369391
// Sort by total points (highest first)
370392
finalResults.sort((a, b) => b.totalPoints - a.totalPoints);
371393

394+
// Calculate totalEligiblePoints for simple points-based voting (not eReputation)
395+
// Each user has 100 points by default
396+
const totalEligiblePoints = totalEligibleVoters * 100;
397+
const pointsVoted = totalPoints; // Sum of all points distributed
398+
372399
return {
373400
pollId,
374401
totalVotes,
375402
totalWeightedPoints: totalPoints,
376403
totalEligibleVoters,
377404
turnout: totalEligibleVoters > 0 ? (totalVotes / totalEligibleVoters) * 100 : 0,
378405
mode: "point",
379-
results: finalResults
406+
results: finalResults,
407+
totalEligiblePoints,
408+
pointsVoted
380409
};
381410
}
382411
} else if (poll.mode === "rank") {

0 commit comments

Comments
 (0)