Repository navigation
feat(S): 乘客評分司機(B5)——ride_ratings + 評分端點 + 三處讀回 - #45
Merged
Merged
Conversation
App 完成卡的「留下評分(即將開放)」從 2026-07-08 佔位至今,註記「真實 API 待 Phase C」。後端當時確實沒有評分資料表與端點(grep -i rating 全 repo 零命中), 本批補齊。對應 backend-api-gaps.md P4 #16。付款(#19)仍待真金流,不在本批。 - migration 000023 ride_ratings:一趟一評、不可重評(唯一索引 uq_ride_ratings_ride_id)。 允許改分會讓司機平均分變成可被事後情緒左右的浮動值,也讓「評過沒」失去明確答案。 customer_id/driver_id 是寫入當下的快照,不靠 rides 反查——行程資料日後若調整 不該改寫歷史評分。CHECK:score 1–5、comment ≤ 500 字(服務層另有 200 字上限)。 - POST /api/customer/rides/:id/rating(customer JWT):驗證順序「輸入 → 歸屬 → 狀態 → 重複」。非本人 403(訂單存在,只是不是你的)、不存在 404、 已評過/未完成 409(狀態衝突不是輸入格式錯,App 據此知道不必改參數重送)。 - 讀回三處(沒有讀回,App 無從知道評過沒): · CustomerRideView.rating——只在 status=completed 時才查,這條路徑也服務乘客端的 active 輪詢,對永遠不可能有評分的進行中訂單每幾秒打一次 ride_ratings 是純浪費。 · CustomerRideRow.rating_score(歷史列表 LEFT JOIN)——完成卡關掉後唯一的補評路徑。 · GET /driver/me 的 rating_avg/rating_count,查失敗不擋 /me(加值資訊,不該讓 司機因此載不出首頁);新司機無評分回 (0,0) 而非錯誤。 - 三處注入都是可選的 SetRating/SetRatings,未注入時既有回應形狀一個欄位不少。 驗收:go build/go vet/gofmt 乾淨。 - internal/service/rating_test.go 3 案(主流程+四道守門與 trim/平均分含無評分 (0,0)/ 訂單視圖與歷史列表讀回),真 PostGIS 跑全部 migration,順帶驗證 000023 可套用。 - internal/handler/rating_test.go 2 案(401/403/壞 id 400/壞 JSON 400/星等越界 含缺 score 等同 0 也擋/未啟用 503)。 - internal/handler/route_shape_test.go:POST /customer/rides/estimate(靜態)與 /customer/rides/:id/rating(參數)同層並存——gin 舊版對此在註冊當下 panic, 是啟動即死、單元測試照樣全綠的失敗模式。gin 1.12 實測四條路徑都解析正確。 - 既有 ride_history_customer_integration_test(涵蓋改過的 ListRecentByCustomer) 與 handler 的 Driver/Me/Profile/Vehicle 測試皆綠。 App 端對應:fleet-app PR #45。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
為什麼是這個任務
App 完成卡的「留下評分(即將開放)」從 2026-07-08 佔位至今,註記「真實 API 待 Phase C」——
那是全 App 最後一個還印著「即將開放」的出貨畫面。後端當時確實沒有評分資料表與端點
(
grep -i rating全 repo 零命中)。對應docs/backend-api-gaps.mdP4 #16。付款(P4 #19)仍待真金流,不在本批。
改了什麼
migration 000023
ride_ratingsuq_ride_ratings_ride_id):分數送出即定論。允許改分會讓司機平均分變成可被事後情緒左右的浮動值,也讓「評過沒」失去明確答案
(App 完成卡要據此決定顯示星星還是評分按鈕)。
customer_id/driver_id是寫入當下的快照,不靠 rides 反查——評分屬於「這位乘客給這位司機」的事實,行程資料日後若調整不該改寫歷史評分。
score BETWEEN 1 AND 5、char_length(comment) <= 500(服務層另有 200 字上限;DB 這層是直打 SQL 也擋得住的最後防線,比照
lost_item_requests)。POST /api/customer/rides/:id/rating(customer JWT)驗證順序刻意是「輸入 → 歸屬 → 狀態 → 重複」:先擋不必查 DB 的輸入錯誤,
再依序回答「這趟是不是你的」「完成了沒」「你評過了沒」。
沒司機的行程(派單前取消)沒有評分對象,與「未完成」同走
ErrRideNotCompleted。讀回三處(沒有讀回,App 無從知道評過沒)
CustomerRideView.rating— 只在status=completed時才查。這條路徑也服務乘客端的active 輪詢,對永遠不可能有評分的進行中訂單每幾秒打一次
ride_ratings是純浪費。CustomerRideRow.rating_score(歷史列表 LEFT JOIN)— 完成卡關掉後唯一的補評路徑。GET /driver/me的rating_avg/rating_count— 查失敗不擋/me(加值資訊,不該讓司機因此載不出首頁);新司機無評分回
(0, 0)而非錯誤。三處注入都是可選的
SetRating/SetRatings,未注入時既有回應形狀一個欄位不少。驗收
go build ./.../go vet ./.../gofmt -l全乾淨。internal/service/rating_test.go3 案(主流程+四道守門與 trim/平均分含無評分(0,0)/訂單視圖與歷史列表讀回)——真 PostGIS 跑全部 migration,順帶驗證 000023 本身可套用。
internal/handler/rating_test.go2 案(401/403/壞 id 400/壞 JSON 400/星等越界含「缺 score 等同 0 也擋」/未啟用 503)。
internal/handler/route_shape_test.go:POST /customer/rides/estimate(靜態)與/customer/rides/:id/rating(參數)同層並存——gin 舊版對此會在註冊當下 panic,那是啟動即死、單元測試照樣全綠、一路到部署才發現的失敗模式。gin 1.12 實測四條路徑都解析正確。
ride_history_customer_integration_test(涵蓋改過的ListRecentByCustomer)與 handler 的
Driver|Me|Profile|Vehicle測試皆綠。未做(誠實揭露)
沒有 docker compose live E2E——本批只有單元/整合測試。App 端 PR 已把
「B5 評分的模擬器實跑 E2E」列為下次任務最優先(含重複評分撞 409 這條只有直打 API 才遇得到的路徑)。
App 端對應:thothawei/fleet-app#45
🤖 Generated with Claude Code