2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package payments
|
|
|
|
|
|
|
|
import (
|
2020-01-29 00:57:15 +00:00
|
|
|
"context"
|
2019-11-26 17:58:51 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
"storj.io/common/memory"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-26 17:58:51 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
// Coupons exposes all needed functionality to manage coupons.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Coupons interface {
|
|
|
|
// ListByUserID return list of all coupons of specified payment account.
|
|
|
|
ListByUserID(ctx context.Context, userID uuid.UUID) ([]Coupon, error)
|
|
|
|
|
2020-05-14 11:34:42 +01:00
|
|
|
// TotalUsage returns sum of all usage records for specified coupon.
|
|
|
|
TotalUsage(ctx context.Context, couponID uuid.UUID) (int64, error)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
// Create attaches a coupon for payment account.
|
2020-05-19 11:36:13 +01:00
|
|
|
Create(ctx context.Context, coupon Coupon) (coup Coupon, err error)
|
2020-01-29 00:57:15 +00:00
|
|
|
|
|
|
|
// AddPromotionalCoupon is used to add a promotional coupon for specified users who already have
|
|
|
|
// a project and do not have a promotional coupon yet.
|
|
|
|
// And updates project limits to selected size.
|
2020-03-16 19:34:15 +00:00
|
|
|
AddPromotionalCoupon(ctx context.Context, userID uuid.UUID) error
|
2020-01-29 00:57:15 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
PopulatePromotionalCoupons(ctx context.Context, duration int, amount int64, projectLimit memory.Size) error
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// Coupon is an entity that adds some funds to Accounts balance for some fixed period.
|
|
|
|
// Coupon is attached to the project.
|
|
|
|
// At the end of the period, the entire remaining coupon amount will be returned from the account balance.
|
|
|
|
type Coupon struct {
|
2020-01-07 10:41:19 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
UserID uuid.UUID `json:"userId"`
|
|
|
|
Amount int64 `json:"amount"` // Amount is stored in cents.
|
|
|
|
Duration int `json:"duration"` // Duration is stored in number ob billing periods.
|
|
|
|
Description string `json:"description"`
|
|
|
|
Type CouponType `json:"type"`
|
|
|
|
Status CouponStatus `json:"status"`
|
|
|
|
Created time.Time `json:"created"`
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 18:02:51 +01:00
|
|
|
// ExpirationDate returns coupon expiration date.
|
|
|
|
//
|
|
|
|
// A coupon is valid for Duration number of full months. The month the user
|
|
|
|
// signs up is not counted in the duration. The expirated date is at the last
|
|
|
|
// day of the last valid month.
|
|
|
|
func (coupon *Coupon) ExpirationDate() time.Time {
|
|
|
|
return time.Date(coupon.Created.Year(), coupon.Created.Month()+time.Month(coupon.Duration)+1, 0, 0, 0, 0, 0, time.UTC)
|
2020-01-07 10:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CouponType indicates the type of the coupon.
|
|
|
|
type CouponType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CouponTypePromotional defines that this coupon is a promotional coupon.
|
2020-01-29 00:57:15 +00:00
|
|
|
// Promotional coupon is added only once per account.
|
2020-01-07 10:41:19 +00:00
|
|
|
CouponTypePromotional CouponType = 0
|
|
|
|
)
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// CouponStatus indicates the state of the coupon.
|
|
|
|
type CouponStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CouponActive is a default coupon state.
|
|
|
|
CouponActive CouponStatus = 0
|
|
|
|
// CouponUsed status indicates that coupon was used.
|
|
|
|
CouponUsed CouponStatus = 1
|
|
|
|
// CouponExpired status indicates that coupon is expired and unavailable.
|
|
|
|
CouponExpired CouponStatus = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// CouponsPage holds set of coupon and indicates if
|
|
|
|
// there are more coupons to fetch.
|
|
|
|
type CouponsPage struct {
|
|
|
|
Coupons []Coupon
|
|
|
|
Next bool
|
|
|
|
NextOffset int64
|
|
|
|
}
|