Skip to content

Commit

Permalink
Fix console purchase scoping and pagination issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito committed Apr 16, 2021
1 parent 435d73f commit 52ab63b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion console/ui/dist/index.html
Expand Up @@ -10,5 +10,5 @@
<link rel="stylesheet" href="static/styles.14b882f135e080634619.css"></head>
<body class="h-100">
<app-root></app-root>
<script src="static/runtime.79a86fd8e31d575369c1.js" defer=""></script><script src="static/polyfills.e509efdf859445d7ea46.js" defer=""></script><script src="static/main.8ba0af2f5b7c1546f25b.js" defer=""></script></body>
<script src="static/runtime.79a86fd8e31d575369c1.js" defer=""></script><script src="static/polyfills.e509efdf859445d7ea46.js" defer=""></script><script src="static/main.695001f2f04b0d8e5c57.js" defer=""></script></body>
</html>

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions console/ui/src/app/account/purchases/purchases.component.ts
Expand Up @@ -37,7 +37,7 @@ export class PurchasesComponent implements OnInit {
) {}

ngOnInit(): void {
this.userID = this.route.snapshot.paramMap.get('id');
this.userID = this.route.parent.snapshot.paramMap.get('id');
this.route.data.subscribe(data => {
this.purchases.push(...data[0].validated_purchases);
this.nextCursor = data[0].cursor;
Expand Down Expand Up @@ -73,7 +73,7 @@ export class PurchasesResolver implements Resolve<ApiPurchaseList> {
constructor(private readonly consoleService: ConsoleService) {}

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<ApiPurchaseList> {
const userId = route.paramMap.get('id');
const userId = route.parent.paramMap.get('id');
return this.consoleService.listPurchases('', userId, 100, '');
}
}
34 changes: 20 additions & 14 deletions server/core_purchase.go
Expand Up @@ -31,6 +31,7 @@ import (
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -231,9 +232,9 @@ func ValidatePurchaseHuawei(ctx context.Context, logger *zap.Logger, db *sql.DB,
}

type purchasesListCursor struct {
transactionId string
purchaseTime int64
userId string
TransactionId string
PurchaseTime *timestamppb.Timestamp
UserId string
}

func GetPurchaseByTransactionID(ctx context.Context, logger *zap.Logger, db *sql.DB, transactionID string) (string, *api.ValidatedPurchase, error) {
Expand Down Expand Up @@ -283,15 +284,15 @@ WHERE
func ListPurchases(ctx context.Context, logger *zap.Logger, db *sql.DB, userID string, limit int, cursor string) (*api.PurchaseList, error) {
var incomingCursor *purchasesListCursor
if cursor != "" {
cb, err := base64.StdEncoding.DecodeString(cursor)
cb, err := base64.URLEncoding.DecodeString(cursor)
if err != nil {
return nil, ErrPurchasesListInvalidCursor
}
incomingCursor = &purchasesListCursor{}
if err := gob.NewDecoder(bytes.NewReader(cb)).Decode(incomingCursor); err != nil {
return nil, ErrPurchasesListInvalidCursor
}
if userID != "" && userID != incomingCursor.userId {
if userID != "" && userID != incomingCursor.UserId {
// userID filter was set and has changed, cursor is now invalid
return nil, ErrPurchasesListInvalidCursor
}
Expand All @@ -314,13 +315,18 @@ FROM
`
if incomingCursor != nil {
if userID == "" {
query += " WHERE (user_id, purchase_time, transaction_id) >= ($1, to_timestamp($2), $3)"
query += " WHERE (user_id, purchase_time, transaction_id) <= ($1, $2, $3)"
} else {
query += " WHERE user_id = $1 AND (purchase_time, transaction_id) >= (to_timestamp($2), $3)"
query += " WHERE user_id = $1 AND (purchase_time, transaction_id) <= ($2, $3)"
}
params = append(params, incomingCursor.UserId)
params = append(params, incomingCursor.PurchaseTime.AsTime())
params = append(params, incomingCursor.TransactionId)
} else {
if userID != "" {
query += " WHERE user_id = $1"
params = append(params, userID)
}
params = append(params, incomingCursor.userId)
params = append(params, incomingCursor.purchaseTime)
params = append(params, incomingCursor.transactionId)
}
query += " ORDER BY purchase_time DESC"
if limit > 0 {
Expand Down Expand Up @@ -359,14 +365,14 @@ FROM
if len(purchases) >= limit {
cursorBuf := new(bytes.Buffer)
if err := gob.NewEncoder(cursorBuf).Encode(&purchasesListCursor{
transactionId: transactionId,
purchaseTime: purchaseTime.Time.Unix(),
userId: userID.String(),
TransactionId: transactionId,
PurchaseTime: timestamppb.New(purchaseTime.Time),
UserId: userID.String(),
}); err != nil {
logger.Error("Error creating purchases list cursor", zap.Error(err))
return nil, err
}
outgoingCursor = base64.StdEncoding.EncodeToString(cursorBuf.Bytes())
outgoingCursor = base64.URLEncoding.EncodeToString(cursorBuf.Bytes())
break
}

Expand Down

0 comments on commit 52ab63b

Please sign in to comment.