From dedf6e83827f08e0fcfbb164cf41e5f6f793cc42 Mon Sep 17 00:00:00 2001 From: zhx Date: Mon, 8 Jun 2026 17:35:25 +0800 Subject: [PATCH] fix: pass cp gift relation type --- server/admin/internal/modules/resource/dto.go | 2 + .../internal/modules/resource/request.go | 24 +++++++ .../internal/modules/resource/request_test.go | 63 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 server/admin/internal/modules/resource/request_test.go diff --git a/server/admin/internal/modules/resource/dto.go b/server/admin/internal/modules/resource/dto.go index be9e9e36..43716072 100644 --- a/server/admin/internal/modules/resource/dto.go +++ b/server/admin/internal/modules/resource/dto.go @@ -71,6 +71,7 @@ type giftDTO struct { PresentationJSON string `json:"presentationJson"` PriceVersion string `json:"priceVersion"` GiftTypeCode string `json:"giftTypeCode"` + CPRelationType string `json:"cpRelationType"` ChargeAssetType string `json:"chargeAssetType"` CoinPrice int64 `json:"coinPrice"` EffectiveFromMS int64 `json:"effectiveFromMs"` @@ -293,6 +294,7 @@ func giftFromProto(gift *walletv1.GiftConfig) giftDTO { PresentationJSON: gift.GetPresentationJson(), PriceVersion: gift.GetPriceVersion(), GiftTypeCode: gift.GetGiftTypeCode(), + CPRelationType: gift.GetCpRelationType(), ChargeAssetType: gift.GetChargeAssetType(), CoinPrice: gift.GetCoinPrice(), EffectiveFromMS: gift.GetEffectiveFromMs(), diff --git a/server/admin/internal/modules/resource/request.go b/server/admin/internal/modules/resource/request.go index 86ae1f8d..fa896263 100644 --- a/server/admin/internal/modules/resource/request.go +++ b/server/admin/internal/modules/resource/request.go @@ -131,6 +131,7 @@ type giftRequest struct { PresentationJSON string `json:"presentationJson"` PriceVersion string `json:"priceVersion"` GiftTypeCode string `json:"giftTypeCode"` + CPRelationType string `json:"cpRelationType"` ChargeAssetType string `json:"chargeAssetType"` CoinPrice int64 `json:"coinPrice"` HeatValue int64 `json:"heatValue"` // 兼容旧后台请求;新后台不再展示或提交热度。 @@ -366,6 +367,7 @@ func (r giftRequest) createProto(c *gin.Context) *walletv1.CreateGiftConfigReque PresentationJson: strings.TrimSpace(r.PresentationJSON), PriceVersion: strings.TrimSpace(r.PriceVersion), GiftTypeCode: strings.TrimSpace(r.GiftTypeCode), + CpRelationType: r.cpRelationType(), ChargeAssetType: strings.TrimSpace(r.ChargeAssetType), CoinPrice: r.CoinPrice, GiftPointAmount: 0, @@ -391,6 +393,7 @@ func (r giftRequest) updateProto(c *gin.Context, giftID string) *walletv1.Update PresentationJson: strings.TrimSpace(r.PresentationJSON), PriceVersion: strings.TrimSpace(r.PriceVersion), GiftTypeCode: strings.TrimSpace(r.GiftTypeCode), + CpRelationType: r.cpRelationType(), ChargeAssetType: strings.TrimSpace(r.ChargeAssetType), CoinPrice: r.CoinPrice, GiftPointAmount: 0, @@ -404,6 +407,27 @@ func (r giftRequest) updateProto(c *gin.Context, giftID string) *walletv1.Update } } +func (r giftRequest) cpRelationType() string { + if strings.TrimSpace(r.GiftTypeCode) != "cp" { + return "" + } + // CP 关系类型最终由 wallet-service 写回 presentation_json;兼容旧前端只提交 JSON 的请求,避免保存时把兄弟/姐妹关系清掉。 + if value := strings.TrimSpace(r.CPRelationType); value != "" { + return value + } + var payload map[string]any + if err := json.Unmarshal([]byte(strings.TrimSpace(r.PresentationJSON)), &payload); err != nil { + return "" + } + if value, ok := payload["cp_relation_type"].(string); ok { + return strings.TrimSpace(value) + } + if value, ok := payload["cpRelationType"].(string); ok { + return strings.TrimSpace(value) + } + return "" +} + func (r giftTypeRequest) upsertProto(c *gin.Context, typeCode string) *walletv1.UpsertGiftTypeConfigRequest { return &walletv1.UpsertGiftTypeConfigRequest{ RequestId: middleware.CurrentRequestID(c), diff --git a/server/admin/internal/modules/resource/request_test.go b/server/admin/internal/modules/resource/request_test.go new file mode 100644 index 00000000..46174d39 --- /dev/null +++ b/server/admin/internal/modules/resource/request_test.go @@ -0,0 +1,63 @@ +package resource + +import ( + "net/http/httptest" + "testing" + + "hyapp-admin-server/internal/appctx" + "hyapp-admin-server/internal/middleware" + + "github.com/gin-gonic/gin" +) + +func TestGiftRequestProtoKeepsExplicitCPRelationType(t *testing.T) { + req := giftRequest{ + GiftTypeCode: "cp", + CPRelationType: "brother", + PresentationJSON: `{"cp_relation_type":"sister"}`, + } + + proto := req.updateProto(newGiftRequestTestContext(), "love") + + if proto.GetCpRelationType() != "brother" { + t.Fatalf("explicit cp relation type should win, got %q", proto.GetCpRelationType()) + } +} + +func TestGiftRequestProtoFallsBackToPresentationCPRelationType(t *testing.T) { + req := giftRequest{ + GiftID: "love", + GiftTypeCode: "cp", + PresentationJSON: `{"cp_relation_type":"sister"}`, + } + + proto := req.createProto(newGiftRequestTestContext()) + + if proto.GetCpRelationType() != "sister" { + t.Fatalf("presentation cp relation type should be forwarded, got %q", proto.GetCpRelationType()) + } +} + +func TestGiftRequestProtoClearsCPRelationTypeForNonCPGift(t *testing.T) { + req := giftRequest{ + GiftTypeCode: "normal", + CPRelationType: "brother", + PresentationJSON: `{"cp_relation_type":"brother"}`, + } + + proto := req.updateProto(newGiftRequestTestContext(), "rose") + + if proto.GetCpRelationType() != "" { + t.Fatalf("non-cp gift should not forward cp relation type, got %q", proto.GetCpRelationType()) + } +} + +func newGiftRequestTestContext() *gin.Context { + gin.SetMode(gin.TestMode) + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + request := httptest.NewRequest("POST", "/admin/gifts", nil) + c.Request = request.WithContext(appctx.WithContext(request.Context(), "lalu")) + c.Set(middleware.ContextRequestID, "resource-test-request") + return c +}