Skip to content

Commit

Permalink
Optimize BillingRecordQuery to query separate recharge and arrival (r…
Browse files Browse the repository at this point in the history
…echarge + gift) amounts (#4167)

* Optimize BillingRecordQuery to query separate recharge and arrival (recharge + gift) amounts

* add custom cancel & success stripe return url
  • Loading branch information
bxy4543 committed Oct 26, 2023
1 parent f45e6a9 commit bb5abee
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
9 changes: 8 additions & 1 deletion controllers/account/api/v1/billingrecordquery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ type BillingRecordQueryItemInline struct {
Type Type `json:"type" bson:"type"`
AppType string `json:"appType,omitempty" bson:"appType,omitempty"`
Costs Costs `json:"costs,omitempty" bson:"costs,omitempty"`
Amount int64 `json:"amount,omitempty" bson:"amount"`
//Amount = PaymentAmount + GiftAmount
Amount int64 `json:"amount,omitempty" bson:"amount"`
// when Type = Recharge, PaymentAmount is the amount of recharge
Payment *PaymentForQuery `json:"payment,omitempty" bson:"payment,omitempty"`
}

type PaymentForQuery struct {
Amount int64 `json:"amount,omitempty" bson:"amount,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ spec:
items:
properties:
amount:
description: Amount = PaymentAmount + GiftAmount
format: int64
type: integer
appType:
Expand All @@ -90,6 +91,14 @@ spec:
type: string
order_id:
type: string
payment:
description: when Type = Recharge, PaymentAmount is the amount
of recharge
properties:
amount:
format: int64
type: integer
type: object
time:
format: date-time
type: string
Expand Down
9 changes: 9 additions & 0 deletions controllers/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ spec:
items:
properties:
amount:
description: Amount = PaymentAmount + GiftAmount
format: int64
type: integer
appType:
Expand All @@ -257,6 +258,14 @@ spec:
type: string
order_id:
type: string
payment:
description: when Type = Recharge, PaymentAmount is the amount
of recharge
properties:
amount:
format: int64
type: integer
type: object
time:
format: date-time
type: string
Expand Down
45 changes: 33 additions & 12 deletions controllers/pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,41 @@ func (m *MongoDB) queryBillingRecordsByOrderID(billingRecordQuery *accountv1.Bil
if err := cursor.Decode(&bsonRecord); err != nil {
return fmt.Errorf("failed to decode billing record: %w", err)
}
for _, cost := range bsonRecord.AppCosts {
billingRecord := accountv1.BillingRecordQueryItem{
Time: metav1.NewTime(bsonRecord.Time),
BillingRecordQueryItemInline: accountv1.BillingRecordQueryItemInline{
OrderID: bsonRecord.OrderID,
Type: bsonRecord.Type,
Namespace: bsonRecord.Namespace,
AppType: resources.AppTypeReverse[bsonRecord.AppType],
Costs: resources.ConvertEnumUsedToString(cost.UsedAmount),
Amount: cost.Amount,
Name: cost.Name,
},
var billingRecord = accountv1.BillingRecordQueryItem{
Time: metav1.NewTime(bsonRecord.Time),
BillingRecordQueryItemInline: accountv1.BillingRecordQueryItemInline{
OrderID: bsonRecord.OrderID,
Type: bsonRecord.Type,
Amount: bsonRecord.Amount,
Namespace: bsonRecord.Namespace,
},
}
switch bsonRecord.Type {
case accountv1.Recharge:
paymentAmount := billingRecord.Amount
if bsonRecord.Payment != nil {
paymentAmount = bsonRecord.Payment.Amount
}
billingRecord.Payment = &accountv1.PaymentForQuery{Amount: paymentAmount}
billingRecords = append(billingRecords, billingRecord)
case accountv1.TransferOut, accountv1.TransferIn:
billingRecords = append(billingRecords, billingRecord)
default:
for _, cost := range bsonRecord.AppCosts {
billingRecord = accountv1.BillingRecordQueryItem{
Time: metav1.NewTime(bsonRecord.Time),
BillingRecordQueryItemInline: accountv1.BillingRecordQueryItemInline{
OrderID: bsonRecord.OrderID,
Type: bsonRecord.Type,
Namespace: bsonRecord.Namespace,
AppType: resources.AppTypeReverse[bsonRecord.AppType],
Costs: resources.ConvertEnumUsedToString(cost.UsedAmount),
Amount: cost.Amount,
Name: cost.Name,
},
}
billingRecords = append(billingRecords, billingRecord)
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions controllers/pkg/database/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,38 @@ func TestMongoDB_QueryBillingRecords(t *testing.T) {
}
}

func TestMongoDB_QueryBillingRecords1(t *testing.T) {
dbCTX := context.Background()
m, err := NewMongoDB(dbCTX, os.Getenv("MONGODB_URI"))
if err != nil {
t.Errorf("failed to connect mongo: error = %v", err)
}
defer func() {
if err = m.Disconnect(dbCTX); err != nil {
t.Errorf("failed to disconnect mongo: error = %v", err)
}
}()
//now := time.Now().UTC()
billquery := &accountv1.BillingRecordQuery{
Spec: accountv1.BillingRecordQuerySpec{
StartTime: metav1.Time{Time: time.Now().UTC().Add(-time.Hour * 24 * 30)},
EndTime: metav1.Time{Time: time.Now().UTC().Add(time.Hour * 24 * 30)},
Page: 1,
PageSize: 5,
Type: 1,
},
}
err = m.QueryBillingRecords(billquery, "")
if err != nil {
t.Errorf("failed to query billing records: error = %v", err)
}
data, err := yaml.Marshal(billquery)
if err != nil {
t.Errorf("failed to marshal billingRecordQuery: error = %v", err)
}
t.Logf("billingRecordQuery: %s\n", string(data))
}

var testTime = time.Date(2023, 5, 9, 5, 0, 0, 0, time.UTC)

func TestMongoDB_QueryBillingRecords2(t *testing.T) {
Expand Down

0 comments on commit bb5abee

Please sign in to comment.