feat: Refactor order snapshot handling and introduce structured snapshot types
- Added new structured snapshot types for orders and order items to improve data integrity and clarity. - Updated the Order and OrderItem models to use the new JSONType for snapshots. - Refactored tests to accommodate the new snapshot structure, ensuring compatibility with legacy data. - Enhanced the OrdersSnapshot struct to support multiple snapshot types and maintain backward compatibility. - Introduced new fields for order items and orders to capture detailed snapshot information for auditing and historical display.
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/fields"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
@@ -27,6 +28,13 @@ import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func newLegacyOrderSnapshot() types.JSONType[fields.OrdersSnapshot] {
|
||||
return types.NewJSONType(fields.OrdersSnapshot{
|
||||
Kind: "legacy",
|
||||
Data: json.RawMessage([]byte("{}")),
|
||||
})
|
||||
}
|
||||
|
||||
type OrderTestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
@@ -144,11 +152,14 @@ func (s *OrderTestSuite) Test_AdminTopupUser() {
|
||||
So(orderModel.Status, ShouldEqual, consts.OrderStatusPaid)
|
||||
So(orderModel.AmountPaid, ShouldEqual, 300)
|
||||
|
||||
var snap map[string]any
|
||||
So(json.Unmarshal([]byte(orderModel.Snapshot), &snap), ShouldBeNil)
|
||||
So(snap["operator_user_id"], ShouldEqual, float64(operatorUserID))
|
||||
So(snap["target_user_id"], ShouldEqual, float64(targetUserID))
|
||||
So(snap["amount"], ShouldEqual, float64(300))
|
||||
snap := orderModel.Snapshot.Data()
|
||||
So(snap.Kind, ShouldEqual, string(consts.OrderTypeTopup))
|
||||
|
||||
var snapData fields.OrdersTopupSnapshot
|
||||
So(json.Unmarshal(snap.Data, &snapData), ShouldBeNil)
|
||||
So(snapData.OperatorUserID, ShouldEqual, operatorUserID)
|
||||
So(snapData.TargetUserID, ShouldEqual, targetUserID)
|
||||
So(snapData.Amount, ShouldEqual, int64(300))
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, targetUserID).First(&tu).Error, ShouldBeNil)
|
||||
@@ -211,7 +222,7 @@ func (s *OrderTestSuite) Test_MyOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -224,7 +235,7 @@ func (s *OrderTestSuite) Test_MyOrderPage() {
|
||||
ContentID: 111,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -236,7 +247,7 @@ func (s *OrderTestSuite) Test_MyOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now.Add(time.Minute),
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
UpdatedAt: now.Add(time.Minute),
|
||||
@@ -249,7 +260,7 @@ func (s *OrderTestSuite) Test_MyOrderPage() {
|
||||
ContentID: 222,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
UpdatedAt: now.Add(time.Minute),
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -310,7 +321,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now.Add(-time.Hour),
|
||||
CreatedAt: now.Add(-time.Hour),
|
||||
UpdatedAt: now.Add(-time.Hour),
|
||||
@@ -323,7 +334,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: 333,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now.Add(-time.Hour),
|
||||
UpdatedAt: now.Add(-time.Hour),
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -335,7 +346,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -348,7 +359,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: 444,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -373,7 +384,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -386,7 +397,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: 555,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -429,7 +440,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -442,7 +453,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -499,7 +510,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -512,7 +523,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: c1.ID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -524,7 +535,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -537,7 +548,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: c2.ID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -560,7 +571,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now.Add(-time.Hour),
|
||||
UpdatedAt: now.Add(-time.Hour),
|
||||
@@ -574,7 +585,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -601,7 +612,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now.Add(-time.Hour),
|
||||
UpdatedAt: now.Add(-time.Hour),
|
||||
@@ -615,7 +626,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -653,7 +664,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -667,7 +678,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 200,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -697,7 +708,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -710,7 +721,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: contentID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -723,7 +734,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 50,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -736,7 +747,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: contentID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 50,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -749,7 +760,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusCreated,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -762,7 +773,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: contentID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -775,7 +786,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -788,7 +799,7 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
ContentID: contentID,
|
||||
ContentUserID: 1,
|
||||
AmountPaid: 500,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
@@ -859,7 +870,7 @@ func (s *OrderTestSuite) Test_AdminOrderExportCSV() {
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountPaid: 123,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -1028,7 +1039,7 @@ func (s *OrderTestSuite) Test_AdminRefundOrder() {
|
||||
AmountOriginal: 100,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -1055,7 +1066,7 @@ func (s *OrderTestSuite) Test_AdminRefundOrder() {
|
||||
AmountOriginal: 100,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now.Add(-consts.DefaultOrderRefundWindow).Add(-time.Second),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -1079,7 +1090,7 @@ func (s *OrderTestSuite) Test_AdminRefundOrder() {
|
||||
AmountOriginal: 300,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 300,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: newLegacyOrderSnapshot(),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -1093,7 +1104,7 @@ func (s *OrderTestSuite) Test_AdminRefundOrder() {
|
||||
ContentID: contentID,
|
||||
ContentUserID: 999,
|
||||
AmountPaid: 300,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
@@ -1217,11 +1228,14 @@ func (s *OrderTestSuite) Test_PurchaseContent() {
|
||||
So(res1.Access, ShouldNotBeNil)
|
||||
So(res1.Access.Status, ShouldEqual, consts.ContentAccessStatusActive)
|
||||
|
||||
var snap map[string]any
|
||||
So(json.Unmarshal([]byte(res1.Order.Snapshot), &snap), ShouldBeNil)
|
||||
So(snap["content_id"], ShouldEqual, float64(content.ID))
|
||||
So(snap["content_title"], ShouldEqual, content.Title)
|
||||
So(snap["amount_paid"], ShouldEqual, float64(0))
|
||||
snap := res1.Order.Snapshot.Data()
|
||||
So(snap.Kind, ShouldEqual, string(consts.OrderTypeContentPurchase))
|
||||
|
||||
var snapData fields.OrdersContentPurchaseSnapshot
|
||||
So(json.Unmarshal(snap.Data, &snapData), ShouldBeNil)
|
||||
So(snapData.ContentID, ShouldEqual, content.ID)
|
||||
So(snapData.ContentTitle, ShouldEqual, content.Title)
|
||||
So(snapData.AmountPaid, ShouldEqual, int64(0))
|
||||
|
||||
res2, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
@@ -1264,16 +1278,18 @@ func (s *OrderTestSuite) Test_PurchaseContent() {
|
||||
So(res1.Access, ShouldNotBeNil)
|
||||
So(res1.Access.Status, ShouldEqual, consts.ContentAccessStatusActive)
|
||||
|
||||
var snap map[string]any
|
||||
So(json.Unmarshal([]byte(res1.Order.Snapshot), &snap), ShouldBeNil)
|
||||
So(snap["content_id"], ShouldEqual, float64(content.ID))
|
||||
So(snap["amount_paid"], ShouldEqual, float64(300))
|
||||
So(snap["amount_original"], ShouldEqual, float64(300))
|
||||
snap := res1.Order.Snapshot.Data()
|
||||
So(snap.Kind, ShouldEqual, string(consts.OrderTypeContentPurchase))
|
||||
|
||||
var itemSnap map[string]any
|
||||
So(json.Unmarshal([]byte(res1.OrderItem.Snapshot), &itemSnap), ShouldBeNil)
|
||||
So(itemSnap["content_id"], ShouldEqual, float64(content.ID))
|
||||
So(itemSnap["amount_paid"], ShouldEqual, float64(300))
|
||||
var snapData fields.OrdersContentPurchaseSnapshot
|
||||
So(json.Unmarshal(snap.Data, &snapData), ShouldBeNil)
|
||||
So(snapData.ContentID, ShouldEqual, content.ID)
|
||||
So(snapData.AmountPaid, ShouldEqual, int64(300))
|
||||
So(snapData.AmountOriginal, ShouldEqual, int64(300))
|
||||
|
||||
itemSnap := res1.OrderItem.Snapshot.Data()
|
||||
So(itemSnap.ContentID, ShouldEqual, content.ID)
|
||||
So(itemSnap.AmountPaid, ShouldEqual, int64(300))
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, buyerUserID).First(&tu).Error, ShouldBeNil)
|
||||
|
||||
Reference in New Issue
Block a user