优化结构
This commit is contained in:
@@ -18,12 +18,14 @@ func Migrations() []migration.Step {
|
||||
}},
|
||||
{ID: "202608210001_communication_integration_defaults", Migrate: ensureCommunicationIntegrationConfigs},
|
||||
{ID: "202608200004_payment_defaults", Migrate: ensurePaymentIntegrationConfigs},
|
||||
{ID: "202608290004_kafka_integration_default", Migrate: ensureCommunicationIntegrationConfigs},
|
||||
}
|
||||
}
|
||||
|
||||
func ensureCommunicationIntegrationConfigs(db *gorm.DB) error {
|
||||
defaults := []struct{ kind, provider string }{
|
||||
{integrationbiz.IntegrationKindMQ, "emqx"},
|
||||
{integrationbiz.IntegrationKindMQ, "kafka"},
|
||||
{integrationbiz.IntegrationKindMQ, "rabbitmq"},
|
||||
{integrationbiz.IntegrationKindWebSocket, "melody"},
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@ func SeedDemoOrders(db *gorm.DB, now time.Time) (*DemoSeedResult, error) {
|
||||
if !db.Migrator().HasTable(&paymentOrderPO{}) || !db.Migrator().HasTable(&paymentEventPO{}) {
|
||||
return nil, fmt.Errorf("payment tables are not migrated")
|
||||
}
|
||||
now = now.UTC().Truncate(time.Second)
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC().Truncate(time.Second)
|
||||
} else {
|
||||
now = now.UTC().Truncate(time.Second)
|
||||
}
|
||||
|
||||
type demoOrder struct {
|
||||
@@ -37,7 +38,7 @@ func SeedDemoOrders(db *gorm.DB, now time.Time) (*DemoSeedResult, error) {
|
||||
makeOrder := func(index int, provider, subject, status, fulfillment, refund string, amount int64, currency string, created time.Time) paymentOrderPO {
|
||||
tradeNo := fmt.Sprintf("%s%02d", DemoTradePrefix, index)
|
||||
return paymentOrderPO{
|
||||
TradeNo: tradeNo, Provider: provider, BusinessType: "demo_subscription", BusinessID: fmt.Sprintf("DEMO-LICENSE-%02d", index),
|
||||
TradeNo: tradeNo, Provider: provider, BusinessType: bizpayment.DemoBusinessType, BusinessID: fmt.Sprintf("DEMO-LICENSE-%02d", index),
|
||||
Subject: subject, PaymentMode: bizpayment.PaymentModeExternal, OriginalAmount: amount, Amount: amount,
|
||||
Currency: currency, PaymentStatus: status, FulfillmentStatus: fulfillment, RefundStatus: refund,
|
||||
ConfirmationID: uuid.NewString(), RequestFingerprint: fmt.Sprintf("demo-fingerprint-%02d", index),
|
||||
|
||||
@@ -2,6 +2,7 @@ package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
bizpayment "kra/internal/biz/payment"
|
||||
"time"
|
||||
|
||||
@@ -48,6 +49,10 @@ func appendPaymentEvent(ctx context.Context, tx *gorm.DB, order *paymentOrderPO,
|
||||
if tx == nil || order == nil || event == nil {
|
||||
return nil
|
||||
}
|
||||
eventType := trimTo(event.Type, 64)
|
||||
if eventType == "" {
|
||||
return errors.New("支付事件类型为空")
|
||||
}
|
||||
operation := bizpayment.PaymentOperationFromContext(ctx)
|
||||
source := trimTo(event.Source, 64)
|
||||
if source == "" {
|
||||
@@ -81,7 +86,7 @@ func appendPaymentEvent(ctx context.Context, tx *gorm.DB, order *paymentOrderPO,
|
||||
deviceID = trimTo(operation.DeviceID, 128)
|
||||
}
|
||||
return tx.Create(&paymentEventPO{
|
||||
Provider: order.Provider, TradeNo: order.TradeNo, Type: trimTo(event.Type, 64), Source: source,
|
||||
Provider: order.Provider, TradeNo: order.TradeNo, Type: eventType, Source: source,
|
||||
Status: trimTo(event.Status, 64), ProviderStatus: trimTo(event.ProviderStatus, 64), Message: message,
|
||||
EventID: trimTo(event.EventID, 128), PayloadHash: trimTo(event.PayloadHash, 64), Amount: event.Amount,
|
||||
Currency: trimTo(event.Currency, 16), OperatorID: operatorID, OperatorName: operatorName,
|
||||
|
||||
@@ -152,8 +152,7 @@ func (r *paymentOrderRepo) CreatePaymentOrder(ctx context.Context, order *bizpay
|
||||
}
|
||||
return appendPaymentEvent(ctx, tx, po, &bizpayment.PaymentEvent{
|
||||
Type: bizpayment.PaymentEventOrderCreated, Source: "client", Status: po.PaymentStatus,
|
||||
Amount: po.Amount, Currency: po.Currency, ClientIP: order.ClientIP,
|
||||
UserAgent: order.UserAgent, DeviceID: order.DeviceID,
|
||||
Amount: po.Amount, Currency: po.Currency,
|
||||
})
|
||||
})
|
||||
if err == nil {
|
||||
@@ -637,8 +636,9 @@ func defaultVersion(value uint64) uint64 {
|
||||
|
||||
func trimTo(value string, max int) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if len(value) > max {
|
||||
return value[:max]
|
||||
runes := []rune(value)
|
||||
if len(runes) > max {
|
||||
return string(runes[:max])
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ import (
|
||||
bizpayment "kra/internal/biz/payment"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func newPaymentOrderRepoForTest(t *testing.T) *paymentOrderRepo {
|
||||
t.Helper()
|
||||
db, err := openWithDriver("sqlite", "file:"+t.Name()+"?mode=memory&cache=shared")
|
||||
db, err := openWithDriver("sqlite", "file:"+t.Name()+"-"+uuid.NewString()+"?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -119,11 +121,9 @@ func TestPaymentOrderRepositorySummarizesAndListsEvents(t *testing.T) {
|
||||
repo := newPaymentOrderRepoForTest(t)
|
||||
ctx := bizpayment.WithPaymentOperation(context.Background(), bizpayment.PaymentOperation{
|
||||
Source: "admin", Reason: "客户重复购买", OperatorID: 7, OperatorName: "operator",
|
||||
ClientIP: "127.0.0.1", DeviceID: "device-1",
|
||||
ClientIP: "127.0.0.1", UserAgent: "payment-test/1.0", DeviceID: "device-1",
|
||||
})
|
||||
order := testPaymentOrder()
|
||||
order.ClientIP = "10.0.0.8"
|
||||
order.UserAgent = "payment-client/1.0"
|
||||
if _, _, err := repo.CreatePaymentOrder(ctx, order); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -168,3 +168,9 @@ func TestPaymentOrderRepositorySummarizesAndListsEvents(t *testing.T) {
|
||||
t.Fatalf("refund audit event missing operation context: %#v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimToPreservesUTF8(t *testing.T) {
|
||||
if got := trimTo(" 退款处理失败 ", 4); got != "退款处理" {
|
||||
t.Fatalf("trimTo() = %q, want %q", got, "退款处理")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@ package system
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
platformmodule "kra/pkg/module"
|
||||
)
|
||||
|
||||
func TestEnsureAdminSurfaceAndPolicyInheritance(t *testing.T) {
|
||||
db, err := openWithDriver("sqlite", "file:"+t.Name()+"?mode=memory&cache=shared")
|
||||
db, err := openWithDriver("sqlite", "file:"+t.Name()+"-"+uuid.NewString()+"?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user