From c0ec00c6c8dfc5e3e1d668409bf615da52411c08 Mon Sep 17 00:00:00 2001 From: awei Date: Sun, 19 Jul 2026 03:33:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(H):=20GET=20/customer/rides=20=E4=B9=98?= =?UTF-8?q?=E5=AE=A2=E6=AD=B7=E5=8F=B2=E8=A1=8C=E7=A8=8B=E5=88=97=E8=A1=A8?= =?UTF-8?q?=EF=BC=88=E7=95=99=E8=A8=80=E6=9D=BF=E5=85=A5=E5=8F=A3=E8=A3=9C?= =?UTF-8?q?=E9=81=BA=E5=BE=8C=E7=AB=AF=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 乘客端原本沒有「我的行程」列表,chat 入口只綁在進行中行程/協尋單。 加此端點讓乘客能列出過去行程、對有司機的行程事後聯絡(開對話)。 - repo ListRecentByCustomer:LEFT JOIN drivers 取 driver_name(無司機為 null,前端據此決定是否顯示「聯絡司機」);id DESC;limit 預設 20、 硬上限 MaxListRows;只回本人(呼叫端已由 CustomerAuth 綁 customerID) - CustomerRideRow:id/status/上下車地址/時間/車資/driver_id/driver_name - service RideQueryService.ListRecentByCustomer 委派 repo - handler HistoryByCustomer;route GET /customer/rides(與既有 /active、/:id 共存,gin 靜態優先,無衝突——runtime 啟動已驗) - 整合測試:只回本人、新到舊、有司機帶名/車資、無司機不帶、limit go build/vet 通過;repo 整合測試綠;runtime 造單→GET 回傳正確。 Co-Authored-By: Claude Fable 5 --- cmd/server/main.go | 1 + internal/handler/ride.go | 18 +++ internal/repository/repository.go | 37 ++++++ .../ride_history_customer_integration_test.go | 123 ++++++++++++++++++ internal/service/ride.go | 5 + 5 files changed, 184 insertions(+) create mode 100644 internal/repository/ride_history_customer_integration_test.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 9286a54..46c9ab4 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -229,6 +229,7 @@ func main() { { customerAuthed.POST("/rides", rideHandler.Create) customerAuthed.GET("/customer/fees", customerHandler.Fees) + customerAuthed.GET("/customer/rides", rideHandler.HistoryByCustomer) customerAuthed.GET("/customer/rides/active", rideHandler.ActiveByCustomer) customerAuthed.GET("/customer/rides/:id", rideHandler.GetByCustomer) customerAuthed.POST("/rides/:id/cancel-by-customer", rideHandler.CancelByCustomer) diff --git a/internal/handler/ride.go b/internal/handler/ride.go index 8a13eae..2cd6ec0 100644 --- a/internal/handler/ride.go +++ b/internal/handler/ride.go @@ -292,6 +292,24 @@ func (h *RideHandler) ActiveByCustomer(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"ride": ride}) } +// HistoryByCustomer GET /api/customer/rides — 乘客「我的行程」歷史列表(新到舊,只回本人)。 +// 供「事後聯絡司機」的入口:清單每筆有司機者可開對話(留言板入口補遺)。 +func (h *RideHandler) HistoryByCustomer(c *gin.Context) { + customerID := middleware.CustomerIDFromCtx(c) + limit := 0 // 0 = repo 用預設;非法值也退回預設 + if v := c.Query("limit"); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + limit = n + } + } + rows, err := h.rides.ListRecentByCustomer(customerID, limit) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"rides": rows}) +} + // GetByCustomer GET /api/customer/rides/:id — 乘客查自己單一訂單狀態/司機/ETA,非本人訂單回 403/404 func (h *RideHandler) GetByCustomer(c *gin.Context) { rideID, err := strconv.ParseInt(c.Param("id"), 10, 64) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 6e49994..016d431 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -862,6 +862,43 @@ func (r *RideRepository) ListRecent(status *int16, limit int) ([]AdminRideRow, e return rows, err } +// CustomerRideRow 是乘客端「我的行程」列表的單列:夠渲染歷史清單與開對話即可。 +// DriverName 只在有派到司機時非空(LEFT JOIN),前端據此決定要不要顯示「聯絡司機」。 +type CustomerRideRow struct { + ID int64 `json:"id"` + Status int16 `json:"status"` + PickupAddress string `json:"pickup_address"` + DropoffAddress string `json:"dropoff_address"` + RequestedAt time.Time `json:"requested_at"` + CompletedAt *time.Time `json:"completed_at"` + FareAmountCents *int64 `json:"fare_amount_cents"` + DriverID *int64 `json:"driver_id"` + DriverName *string `json:"driver_name"` +} + +const CustomerRideListDefaultLimit = 20 + +// ListRecentByCustomer 取某乘客最近的行程(新到舊)。**只回本人訂單**(呼叫端已由 +// CustomerAuth 綁定 customerID)。limit<=0 用預設、超過硬上限收斂到 MaxListRows。 +func (r *RideRepository) ListRecentByCustomer(customerID int64, limit int) ([]CustomerRideRow, error) { + if limit <= 0 { + limit = CustomerRideListDefaultLimit + } + if limit > MaxListRows { + limit = MaxListRows + } + var rows []CustomerRideRow + err := r.db.Model(&model.Ride{}). + Select("rides.id", "rides.status", "rides.pickup_address", "rides.dropoff_address", + "rides.requested_at", "rides.completed_at", "rides.fare_amount_cents", + "rides.driver_id", "drivers.name AS driver_name"). + Joins("LEFT JOIN drivers ON drivers.id = rides.driver_id"). + Where("rides.customer_id = ?", customerID). + Order("rides.id DESC").Limit(limit). + Scan(&rows).Error + return rows, err +} + type AdminRepository struct { db *gorm.DB } diff --git a/internal/repository/ride_history_customer_integration_test.go b/internal/repository/ride_history_customer_integration_test.go new file mode 100644 index 0000000..4589e9d --- /dev/null +++ b/internal/repository/ride_history_customer_integration_test.go @@ -0,0 +1,123 @@ +package repository + +import ( + "testing" + "time" + + "line-fleet-dispatch/internal/constants" + "line-fleet-dispatch/internal/model" +) + +// TestListRecentByCustomer 乘客「我的行程」歷史:只回本人、新到舊、有司機帶司機名。 +func TestListRecentByCustomer(t *testing.T) { + db := newMigratedTestDB(t) + rideRepo := NewRideRepository(db) + custRepo := NewCustomerRepository(db) + driverRepo := NewDriverRepository(db) + + me, err := custRepo.FindOrCreateByLineUserID("U_hist_me", "本人") + if err != nil { + t.Fatalf("建立客戶失敗: %v", err) + } + other, err := custRepo.FindOrCreateByLineUserID("U_hist_other", "別人") + if err != nil { + t.Fatalf("建立另一客戶失敗: %v", err) + } + driver, err := driverRepo.FindOrCreate("U_hist_driver", "阿明司機") + if err != nil { + t.Fatalf("建立司機失敗: %v", err) + } + + day := func(s string) time.Time { + d, _ := time.Parse("2006-01-02", s) + return d + } + // Create 的 raw INSERT 只寫基本欄位(driver_id/fare 由 Accept/Complete 後填), + // 故 withDriver 直接 UPDATE 補上,隔離測 ListRecentByCustomer 的 JOIN/投影。 + mk := func(cust int64, addr string, status int16, when time.Time, withDriver bool) int64 { + ride := &model.Ride{ + CustomerID: cust, + Status: status, + PickupPoint: model.GeoPoint{Lat: 25.03, Lng: 121.56}, + PickupAddress: addr, + DropoffAddress: "某目的地", + RequestedAt: when, + CreatedAt: when, + UpdatedAt: when, + } + if err := rideRepo.Create(ride); err != nil { + t.Fatalf("建立訂單 %s 失敗: %v", addr, err) + } + if withDriver { + if err := db.Exec("UPDATE rides SET driver_id = ?, fare_amount_cents = ? WHERE id = ?", + driver.ID, 21500, ride.ID).Error; err != nil { + t.Fatalf("設定司機/車資失敗: %v", err) + } + } + return ride.ID + } + + // 本人 2 筆(一筆有司機已完成、一筆取消無司機)+別人 1 筆。 + mk(me.ID, "本人-舊", constants.RideStatusCompleted, day("2026-07-01"), true) + newestID := mk(me.ID, "本人-新", constants.RideStatusCancelled, day("2026-07-05"), false) + mk(other.ID, "別人的", constants.RideStatusCompleted, day("2026-07-03"), true) + + rows, err := rideRepo.ListRecentByCustomer(me.ID, 0) + if err != nil { + t.Fatalf("ListRecentByCustomer 失敗: %v", err) + } + + // 只回本人 2 筆,不含別人。 + if len(rows) != 2 { + t.Fatalf("應只回本人 2 筆,得 %d:%+v", len(rows), rows) + } + // 新到舊:第一筆是最新那筆。 + if rows[0].ID != newestID || rows[0].PickupAddress != "本人-新" { + t.Fatalf("應新到舊,首筆為本人-新,得 %+v", rows[0]) + } + // 取消且無司機:driver_id/driver_name 皆為 nil → 前端不顯示「聯絡司機」。 + if rows[0].DriverID != nil || rows[0].DriverName != nil { + t.Fatalf("無司機的行程不該帶 driver 欄位,得 driver_id=%v name=%v", rows[0].DriverID, rows[0].DriverName) + } + // 有司機的舊行程:driver_name 帶出(LEFT JOIN drivers)。 + if rows[1].DriverName == nil || *rows[1].DriverName != "阿明司機" { + t.Fatalf("有司機的行程應帶司機名,得 %+v", rows[1]) + } + if rows[1].FareAmountCents == nil || *rows[1].FareAmountCents != 21500 { + t.Fatalf("車資應帶出,得 %+v", rows[1].FareAmountCents) + } +} + +// TestListRecentByCustomer_limit 限制筆數。 +func TestListRecentByCustomer_limit(t *testing.T) { + db := newMigratedTestDB(t) + rideRepo := NewRideRepository(db) + custRepo := NewCustomerRepository(db) + + me, err := custRepo.FindOrCreateByLineUserID("U_hist_limit", "本人") + if err != nil { + t.Fatalf("建立客戶失敗: %v", err) + } + for i := 0; i < 5; i++ { + ride := &model.Ride{ + CustomerID: me.ID, + Status: constants.RideStatusCompleted, + PickupPoint: model.GeoPoint{Lat: 25.03, Lng: 121.56}, + PickupAddress: "行程", + RequestedAt: time.Now(), + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } + if err := rideRepo.Create(ride); err != nil { + t.Fatalf("建立訂單失敗: %v", err) + } + } + + rows, err := rideRepo.ListRecentByCustomer(me.ID, 3) + if err != nil { + t.Fatalf("ListRecentByCustomer 失敗: %v", err) + } + if len(rows) != 3 { + t.Fatalf("limit=3 應回 3 筆,得 %d", len(rows)) + } +} diff --git a/internal/service/ride.go b/internal/service/ride.go index d4e295e..a903f19 100644 --- a/internal/service/ride.go +++ b/internal/service/ride.go @@ -312,6 +312,11 @@ func (s *RideQueryService) TrackGeoJSON(rideID int64) (string, error) { return fmt.Sprintf(`{"type":"Feature","properties":{"ride_id":%d},"geometry":%s}`, rideID, geojson), nil } +// ListRecentByCustomer 乘客端「我的行程」歷史列表(新到舊);只回本人訂單。 +func (s *RideQueryService) ListRecentByCustomer(customerID int64, limit int) ([]repository.CustomerRideRow, error) { + return s.rides.ListRecentByCustomer(customerID, limit) +} + // GetActiveRideByCustomer 找乘客目前進行中的訂單(App 啟動/重連取 ride_id 用),無進行中訂單回 (nil, nil) func (s *RideQueryService) GetActiveRideByCustomer(customerID int64) (*CustomerRideView, error) { ride, err := s.rides.FindActiveByCustomer(customerID)