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
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions internal/handler/ride.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
123 changes: 123 additions & 0 deletions internal/repository/ride_history_customer_integration_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
5 changes: 5 additions & 0 deletions internal/service/ride.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading