2019-06-04 20:17:01 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
"storj.io/storj/internal/currency"
|
2019-06-24 21:51:54 +01:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2019-06-04 20:17:01 +01:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
var (
|
|
|
|
// offerErr is the default offer errors class
|
|
|
|
offerErr = errs.Class("offers error")
|
|
|
|
)
|
|
|
|
|
|
|
|
type offersDB struct {
|
2019-06-04 20:17:01 +01:00
|
|
|
db *dbx.DB
|
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
// ListAll returns all offersDB from the db
|
|
|
|
func (db *offersDB) ListAll(ctx context.Context) ([]rewards.Offer, error) {
|
|
|
|
offersDbx, err := db.db.All_Offer(ctx)
|
2019-06-04 20:17:01 +01:00
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(err)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return offersFromDBX(offersDbx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrent returns an offer that has not expired based on offer type
|
2019-06-24 21:51:54 +01:00
|
|
|
func (db *offersDB) GetCurrentByType(ctx context.Context, offerType rewards.OfferType) (*rewards.Offer, error) {
|
2019-06-04 20:17:01 +01:00
|
|
|
var statement string
|
|
|
|
const columns = "id, name, description, award_credit_in_cents, invitee_credit_in_cents, award_credit_duration_days, invitee_credit_duration_days, redeemable_cap, num_redeemed, expires_at, created_at, status, type"
|
|
|
|
statement = `
|
|
|
|
WITH o AS (
|
|
|
|
SELECT ` + columns + ` FROM offers WHERE status=? AND type=? AND expires_at>? AND num_redeemed < redeemable_cap
|
|
|
|
)
|
|
|
|
SELECT ` + columns + ` FROM o
|
|
|
|
UNION ALL
|
|
|
|
SELECT ` + columns + ` FROM offers
|
|
|
|
WHERE type=? AND status=?
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT id FROM o
|
|
|
|
) order by created_at desc;`
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
rows := db.db.DB.QueryRowContext(ctx, db.db.Rebind(statement), rewards.Active, offerType, time.Now().UTC(), offerType, rewards.Default)
|
2019-06-04 20:17:01 +01:00
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
var awardCreditInCents, inviteeCreditInCents int
|
2019-06-24 21:51:54 +01:00
|
|
|
o := rewards.Offer{}
|
2019-07-01 20:16:49 +01:00
|
|
|
err := rows.Scan(&o.ID, &o.Name, &o.Description, &awardCreditInCents, &inviteeCreditInCents, &o.AwardCreditDurationDays, &o.InviteeCreditDurationDays, &o.RedeemableCap, &o.NumRedeemed, &o.ExpiresAt, &o.CreatedAt, &o.Status, &o.Type)
|
2019-06-04 20:17:01 +01:00
|
|
|
if err == sql.ErrNoRows {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.New("no current offer")
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(err)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
2019-07-01 20:16:49 +01:00
|
|
|
o.AwardCredit = currency.Cents(awardCreditInCents)
|
|
|
|
o.InviteeCredit = currency.Cents(inviteeCreditInCents)
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
return &o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new offer into the db
|
2019-06-24 21:51:54 +01:00
|
|
|
func (db *offersDB) Create(ctx context.Context, o *rewards.NewOffer) (*rewards.Offer, error) {
|
2019-06-04 20:17:01 +01:00
|
|
|
currentTime := time.Now()
|
|
|
|
if o.ExpiresAt.Before(currentTime) {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.New("expiration time: %v can't be before: %v", o.ExpiresAt, currentTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.Status == rewards.Default {
|
|
|
|
o.ExpiresAt = time.Now().UTC().AddDate(100, 0, 0)
|
|
|
|
o.RedeemableCap = 1
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
tx, err := db.db.Open(ctx)
|
2019-06-04 20:17:01 +01:00
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(err)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there's an existing current offer, update its status to Done and set its expires_at to be NOW()
|
2019-06-24 21:51:54 +01:00
|
|
|
statement := db.db.Rebind(`
|
2019-06-04 20:17:01 +01:00
|
|
|
UPDATE offers SET status=?, expires_at=?
|
|
|
|
WHERE status=? AND type=? AND expires_at>?;
|
|
|
|
`)
|
2019-06-24 21:51:54 +01:00
|
|
|
_, err = tx.Tx.ExecContext(ctx, statement, rewards.Done, currentTime, o.Status, o.Type, currentTime)
|
2019-06-04 20:17:01 +01:00
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
offerDbx, err := tx.Create_Offer(ctx,
|
|
|
|
dbx.Offer_Name(o.Name),
|
|
|
|
dbx.Offer_Description(o.Description),
|
2019-07-01 20:16:49 +01:00
|
|
|
dbx.Offer_AwardCreditInCents(o.AwardCredit.Cents()),
|
|
|
|
dbx.Offer_InviteeCreditInCents(o.InviteeCredit.Cents()),
|
2019-06-04 20:17:01 +01:00
|
|
|
dbx.Offer_AwardCreditDurationDays(o.AwardCreditDurationDays),
|
|
|
|
dbx.Offer_InviteeCreditDurationDays(o.InviteeCreditDurationDays),
|
|
|
|
dbx.Offer_RedeemableCap(o.RedeemableCap),
|
|
|
|
dbx.Offer_ExpiresAt(o.ExpiresAt),
|
|
|
|
dbx.Offer_Status(int(o.Status)),
|
|
|
|
dbx.Offer_Type(int(o.Type)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newOffer, err := convertDBOffer(offerDbx)
|
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
return newOffer, offerErr.Wrap(tx.Commit())
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:53:19 +01:00
|
|
|
// Redeem adds 1 to the amount of offers redeemed based on offer id
|
2019-06-24 21:51:54 +01:00
|
|
|
func (db *offersDB) Redeem(ctx context.Context, oID int, isDefault bool) error {
|
|
|
|
if isDefault {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := db.db.Rebind(
|
2019-06-12 16:53:19 +01:00
|
|
|
`UPDATE offers SET num_redeemed = num_redeemed + 1 where id = ? AND status = ? AND num_redeemed < redeemable_cap`,
|
|
|
|
)
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
_, err := db.db.DB.ExecContext(ctx, statement, oID, rewards.Active)
|
2019-06-12 16:53:19 +01:00
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return offerErr.Wrap(err)
|
2019-06-12 16:53:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish changes the offer status to be Done and its expiration date to be now based on offer id
|
2019-06-24 21:51:54 +01:00
|
|
|
func (db *offersDB) Finish(ctx context.Context, oID int) error {
|
2019-06-04 20:17:01 +01:00
|
|
|
updateFields := dbx.Offer_Update_Fields{
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: dbx.Offer_Status(int(rewards.Done)),
|
2019-06-12 16:53:19 +01:00
|
|
|
ExpiresAt: dbx.Offer_ExpiresAt(time.Now().UTC()),
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:53:19 +01:00
|
|
|
offerID := dbx.Offer_Id(oID)
|
2019-06-04 20:17:01 +01:00
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
_, err := db.db.Update_Offer_By_Id(ctx, offerID, updateFields)
|
2019-06-04 20:17:01 +01:00
|
|
|
if err != nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return offerErr.Wrap(err)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
func offersFromDBX(offersDbx []*dbx.Offer) ([]rewards.Offer, error) {
|
|
|
|
var offers []rewards.Offer
|
|
|
|
errList := new(errs.Group)
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
for _, offerDbx := range offersDbx {
|
|
|
|
|
|
|
|
offer, err := convertDBOffer(offerDbx)
|
|
|
|
if err != nil {
|
|
|
|
errList.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
offers = append(offers, *offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offers, errList.Err()
|
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
func convertDBOffer(offerDbx *dbx.Offer) (*rewards.Offer, error) {
|
2019-06-04 20:17:01 +01:00
|
|
|
if offerDbx == nil {
|
2019-06-24 21:51:54 +01:00
|
|
|
return nil, offerErr.New("offerDbx parameter is nil")
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
o := rewards.Offer{
|
2019-06-04 20:17:01 +01:00
|
|
|
ID: offerDbx.Id,
|
|
|
|
Name: offerDbx.Name,
|
|
|
|
Description: offerDbx.Description,
|
2019-07-01 20:16:49 +01:00
|
|
|
AwardCredit: currency.Cents(offerDbx.AwardCreditInCents),
|
|
|
|
InviteeCredit: currency.Cents(offerDbx.InviteeCreditInCents),
|
2019-06-04 20:17:01 +01:00
|
|
|
RedeemableCap: offerDbx.RedeemableCap,
|
|
|
|
NumRedeemed: offerDbx.NumRedeemed,
|
|
|
|
ExpiresAt: offerDbx.ExpiresAt,
|
|
|
|
AwardCreditDurationDays: offerDbx.AwardCreditDurationDays,
|
|
|
|
InviteeCreditDurationDays: offerDbx.InviteeCreditDurationDays,
|
|
|
|
CreatedAt: offerDbx.CreatedAt,
|
2019-06-24 21:51:54 +01:00
|
|
|
Status: rewards.OfferStatus(offerDbx.Status),
|
|
|
|
Type: rewards.OfferType(offerDbx.Type),
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &o, nil
|
|
|
|
}
|