fix(dashboard): correct overview data and states

This commit is contained in:
2026-08-05 09:29:34 +08:00
parent 14a2ed7df8
commit d88e18bd09
8 changed files with 77 additions and 42 deletions

View File

@@ -1,8 +1,11 @@
package dashboard
import (
"regexp"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
)
func TestNamedStatusesUsesFallbackForUnknownStatus(t *testing.T) {
@@ -12,6 +15,24 @@ func TestNamedStatusesUsesFallbackForUnknownStatus(t *testing.T) {
}
}
func TestLoadPaymentChannelsUsesPaymentOrderChannelColumn(t *testing.T) {
_, mock := setupDashboardDatabase(t)
mock.ExpectQuery(regexp.QuoteMeta(
`SELECT channel AS name, COALESCE(SUM(amount), 0) AS value FROM "payment_order" WHERE (status = $1 AND payment_status = $2) AND "payment_order"."deleted_at" IS NULL GROUP BY "channel" ORDER BY value DESC`,
)).WithArgs(1, 23).WillReturnRows(
sqlmock.NewRows([]string{"name", "value"}).AddRow("wechat", int64(1200)),
)
var result []MetricSlice
if err := loadPaymentChannels(&result); err != nil {
t.Fatal(err)
}
if len(result) != 1 || result[0].Name != "wechat" || result[0].Value != 1200 {
t.Fatalf("payment channels = %#v", result)
}
assertDashboardMockExpectations(t, mock)
}
func TestFillRecentDaysIncludesMissingDays(t *testing.T) {
now := time.Date(2026, 7, 29, 12, 0, 0, 0, time.Local)
got := fillRecentDays([]dailyAggregate{{

View File

@@ -126,10 +126,7 @@ func GetDashboardOverview() (DashboardStatistics, error) {
}
result.ProductStatuses = namedStatuses(productStatuses, productStatusNames)
if err := impl.DBService.Model(&models.PaymentOrder{}).
Select("pay_channel AS name, COALESCE(SUM(amount), 0) AS value").
Where("status = ? AND payment_status = ?", common.StatusEnable, 23).
Group("pay_channel").Order("value DESC").Scan(&result.PaymentChannels).Error; err != nil {
if err := loadPaymentChannels(&result.PaymentChannels); err != nil {
return DashboardStatistics{}, err
}
@@ -144,6 +141,13 @@ func GetDashboardOverview() (DashboardStatistics, error) {
return result, nil
}
func loadPaymentChannels(result *[]MetricSlice) error {
return impl.DBService.Model(&models.PaymentOrder{}).
Select("channel AS name, COALESCE(SUM(amount), 0) AS value").
Where("status = ? AND payment_status = ?", common.StatusEnable, 23).
Group("channel").Order("value DESC").Scan(result).Error
}
func namedStatuses(rows []statusAggregate, names map[int]string) []MetricSlice {
result := make([]MetricSlice, 0, len(rows))
for _, row := range rows {