fix: pass cp gift relation type

This commit is contained in:
zhx 2026-06-08 17:35:25 +08:00
parent 4c41c964d2
commit dedf6e8382
3 changed files with 89 additions and 0 deletions

View File

@ -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(),

View File

@ -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),

View File

@ -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
}