Skip to content

Commit

Permalink
satellite/payments/stripecoinpayments: move Coupons expiration date
Browse files Browse the repository at this point in the history
sorting directly to listing method

Change-Id: I58d8a6ea1feba9ff2d19f21a1dbc87bfb8b49801
  • Loading branch information
mniewrzal authored and stefanbenten committed Jun 4, 2020
1 parent 96286fd commit 2b2efcc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion satellite/payments/stripecoinpayments/coupons.go
Expand Up @@ -30,7 +30,7 @@ type CouponsDB interface {
List(ctx context.Context, status payments.CouponStatus) ([]payments.Coupon, error)
// ListByUserID returns all coupons of specified user.
ListByUserID(ctx context.Context, userID uuid.UUID) ([]payments.Coupon, error)
// ListByUserIDAndStatus returns all coupons of specified user and status.
// ListByUserIDAndStatus returns all coupons of specified user and status. Results are ordered (asc) by expiration date.
ListByUserIDAndStatus(ctx context.Context, userID uuid.UUID, status payments.CouponStatus) ([]payments.Coupon, error)
// ListPending returns paginated list of coupons with specified status.
ListPaged(ctx context.Context, offset int64, limit int, before time.Time, status payments.CouponStatus) (payments.CouponsPage, error)
Expand Down
7 changes: 0 additions & 7 deletions satellite/payments/stripecoinpayments/service.go
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"math"
"math/big"
"sort"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -468,12 +467,6 @@ func (service *Service) processCustomers(ctx context.Context, customers []Custom
return err
}

sort.Slice(coupons, func(i, k int) bool {
iDate := coupons[i].ExpirationDate()
kDate := coupons[k].ExpirationDate()
return iDate.Before(kDate)
})

// Apply any promotional credits (a.k.a. coupons) on the remainder.
for _, coupon := range coupons {
if coupon.Status == payments.CouponExpired {
Expand Down
16 changes: 14 additions & 2 deletions satellite/satellitedb/coupons.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"database/sql"
"fmt"
"sort"
"time"

"github.com/lib/pq"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (coupons *coupons) ListByUserID(ctx context.Context, userID uuid.UUID) (_ [
return couponsFromDbxSlice(dbxCoupons)
}

// ListByUserIDAndStatus returns all coupons of specified user and status.
// ListByUserIDAndStatus returns all coupons of specified user and status. Results are ordered (asc) by expiration date.
func (coupons *coupons) ListByUserIDAndStatus(ctx context.Context, userID uuid.UUID, status payments.CouponStatus) (_ []payments.Coupon, err error) {
defer mon.Task()(&ctx, userID)(&err)

Expand All @@ -121,7 +122,18 @@ func (coupons *coupons) ListByUserIDAndStatus(ctx context.Context, userID uuid.U
return nil, err
}

return couponsFromDbxSlice(dbxCoupons)
result, err := couponsFromDbxSlice(dbxCoupons)
if err != nil {
return nil, err
}

sort.Slice(result, func(i, k int) bool {
iDate := result[i].ExpirationDate()
kDate := result[k].ExpirationDate()
return iDate.Before(kDate)
})

return result, nil
}

// List returns all coupons with specified status.
Expand Down

0 comments on commit 2b2efcc

Please sign in to comment.