Repository navigation
fix: 投票率分母改用名冊人數而非已投票人數 - #57
Merged
Merged
Conversation
calculateActivityStatistics 原本用 activity.users.length 當投票率的 分母,但該欄位存的是「已投票的學號清單」,等同於分子,導致投票率 恆為 100%、「合格選民數」顯示成已投票人數。 改用 activity.eligible_voters_count(上傳選舉人名冊 CSV 時寫入的 名冊人數),與後台結果頁「合格選民數」的語意一致。未上傳過名冊的 活動該欄位為 0,既有的除以零保護會讓投票率維持 "0"。 新增 __tests__/statisticsService.test.ts 涵蓋投票率計算、無名冊、 choose_one 計票與活動不存在四種情況。
|
Contributor
There was a problem hiding this comment.
Pull request overview
修正後端統計計算中投票率分母誤用「已投票名單」導致投票率恆為 100% 的問題,改以活動的名冊人數欄位計算,並補上單元測試防止回歸。
Changes:
/api/stats相關統計計算:投票率分母改用eligible_voters_count(名冊人數)而非users.length(已投票人數)。- 新增
statisticsService單元測試,覆蓋名冊分母、無名冊除以零保護、choose_one計票與活動不存在回傳 404。
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/statisticsService.ts | 修正投票率/合格選民數計算基準,改用名冊人數欄位。 |
| tests/statisticsService.test.ts | 新增統計服務測試,涵蓋分母修正與回歸保護。 |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| type: string; | ||
| rule: string; | ||
| users: string[]; | ||
| eligible_voters_count: number; |
Member
|
Hi @ChiuKuanHsun , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



問題
lib/statisticsService.ts計算投票率時,分母用的是activity.users.length:但
activity.users存的是已投票的學號清單(votingService.ts投票成功後$addToSet進去的),也就是分子本身。結果:turnoutRate恆為 100%本地實測(名冊 5 人、投出 1 票)修正前的
/api/stats回應:{ "totalVotes": 1, "totalEligibleVoters": 1, "turnoutRate": "100.00" }修正
改用
activity.eligible_voters_count—— 上傳選舉人名冊 CSV 時寫入的名冊人數。同樣情境修正後:
{ "totalVotes": 1, "totalEligibleVoters": 5, "turnoutRate": "20.00" }沒上傳過名冊的活動
eligible_voters_count為 0,既有的totalEligibleVoters > 0保護會讓投票率維持"0",不會除以零。測試
新增
__tests__/statisticsService.test.ts,涵蓋四種情況:choose_one逐選項計票已確認這些測試在還原修正後會失敗(2 項),確實能擋住回歸。
npm run lint、npm run type-check、npm test(64 tests)皆通過。影響範圍
只改後端統計計算,
/api/stats的回應結構不變,前端不需要調整。唯一的行為差異是投票率與合格選民數會顯示成正確的值。