Skip to content

Commit

Permalink
satellite/payments: available coupon value feature
Browse files Browse the repository at this point in the history
Change-Id: Ieae9385fbd917230298afff91a6be2838ad9b313
  • Loading branch information
VitaliiShpital committed Jun 2, 2020
1 parent e8c4010 commit c272872
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions satellite/console/billinghistoryitem.go
Expand Up @@ -12,6 +12,7 @@ type BillingHistoryItem struct {
ID string `json:"id"`
Description string `json:"description"`
Amount int64 `json:"amount"`
Remaining int64 `json:"remaining"`
Received int64 `json:"received"`
Status string `json:"status"`
Link string `json:"link"`
Expand Down
11 changes: 11 additions & 0 deletions satellite/console/service.go
Expand Up @@ -299,11 +299,22 @@ func (paymentService PaymentsService) BillingHistory(ctx context.Context) (billi
}

for _, coupon := range coupons {
alreadyUsed, err := paymentService.service.accounts.Coupons().TotalUsage(ctx, coupon.ID)
if err != nil {
return nil, Error.Wrap(err)
}

remaining := coupon.Amount - alreadyUsed
if coupon.Status == payments.CouponExpired {
remaining = 0
}

billingHistory = append(billingHistory,
&BillingHistoryItem{
ID: coupon.ID.String(),
Description: coupon.Description,
Amount: coupon.Amount,
Remaining: remaining,
Status: "Added to balance",
Link: "",
Start: coupon.Created,
Expand Down
3 changes: 3 additions & 0 deletions satellite/payments/coupons.go
Expand Up @@ -18,6 +18,9 @@ type Coupons interface {
// ListByUserID return list of all coupons of specified payment account.
ListByUserID(ctx context.Context, userID uuid.UUID) ([]Coupon, error)

// TotalUsage returns sum of all usage records for specified coupon.
TotalUsage(ctx context.Context, couponID uuid.UUID) (int64, error)

// Create attaches a coupon for payment account.
Create(ctx context.Context, coupon Coupon) (coup Coupon, err error)

Expand Down
9 changes: 9 additions & 0 deletions satellite/payments/stripecoinpayments/coupons.go
Expand Up @@ -106,6 +106,15 @@ func (coupons *coupons) ListByUserID(ctx context.Context, userID uuid.UUID) (_ [
return couponList, Error.Wrap(err)
}

// TotalUsage returns sum of all usage records for specified coupon.
func (coupons *coupons) TotalUsage(ctx context.Context, couponID uuid.UUID) (_ int64, err error) {
defer mon.Task()(&ctx, couponID)(&err)

totalUsage, err := coupons.service.db.Coupons().TotalUsage(ctx, couponID)

return totalUsage, Error.Wrap(err)
}

// PopulatePromotionalCoupons is used to populate promotional coupons through all active users who already have
// a project, payment method and do not have a promotional coupon yet.
// And updates project limits to selected size.
Expand Down

0 comments on commit c272872

Please sign in to comment.