satellite/payments: available coupon value feature

Change-Id: Ieae9385fbd917230298afff91a6be2838ad9b313
This commit is contained in:
VitaliiShpital 2020-05-14 13:34:42 +03:00 committed by Vitalii Shpital
parent e8c4010311
commit c272872d5d
4 changed files with 24 additions and 0 deletions

View File

@ -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"`

View File

@ -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,

View File

@ -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)

View File

@ -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.