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-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/currency"
|
2019-12-19 09:20:52 +00:00
|
|
|
"storj.io/storj/private/dbutil/txutil"
|
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-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
// ListAll returns all offersDB from the db
|
2019-07-10 18:12:40 +01:00
|
|
|
func (db *offersDB) ListAll(ctx context.Context) (rewards.Offers, error) {
|
2019-07-12 15:19:38 +01:00
|
|
|
offersDbx, err := db.db.All_Offer_OrderBy_Asc_Id(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)
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:46:33 +01:00
|
|
|
// GetCurrent returns offers that has not expired based on offer type
|
|
|
|
func (db *offersDB) GetActiveOffersByType(ctx context.Context, offerType rewards.OfferType) (rewards.Offers, error) {
|
2019-06-04 20:17:01 +01:00
|
|
|
var statement string
|
2019-07-12 14:19:18 +01:00
|
|
|
const columns = "id, name, description, award_credit_in_cents, invitee_credit_in_cents, award_credit_duration_days, invitee_credit_duration_days, redeemable_cap, expires_at, created_at, status, type"
|
2019-06-04 20:17:01 +01:00
|
|
|
statement = `
|
|
|
|
WITH o AS (
|
2019-07-30 14:21:00 +01:00
|
|
|
SELECT ` + columns + ` FROM offers WHERE status=? AND type=? AND expires_at>?
|
2019-06-04 20:17:01 +01:00
|
|
|
)
|
|
|
|
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-08-01 18:46:33 +01:00
|
|
|
rows, err := db.db.DB.QueryContext(ctx, db.db.Rebind(statement), rewards.Active, offerType, time.Now().UTC(), offerType, rewards.Default)
|
|
|
|
if err != nil {
|
2019-11-06 18:37:53 +00:00
|
|
|
return nil, rewards.ErrOfferNotExist.Wrap(err)
|
2019-08-01 18:46:33 +01:00
|
|
|
}
|
2019-06-04 20:17:01 +01:00
|
|
|
|
2019-07-12 15:19:38 +01:00
|
|
|
var (
|
|
|
|
awardCreditInCents int
|
|
|
|
inviteeCreditInCents int
|
|
|
|
awardCreditDurationDays sql.NullInt64
|
|
|
|
inviteeCreditDurationDays sql.NullInt64
|
|
|
|
redeemableCap sql.NullInt64
|
|
|
|
)
|
|
|
|
|
2019-08-01 18:46:33 +01:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2019-08-22 12:40:15 +01:00
|
|
|
results := rewards.Offers{}
|
2019-08-01 18:46:33 +01:00
|
|
|
for rows.Next() {
|
|
|
|
o := rewards.Offer{}
|
|
|
|
err := rows.Scan(&o.ID, &o.Name, &o.Description, &awardCreditInCents, &inviteeCreditInCents, &awardCreditDurationDays, &inviteeCreditDurationDays, &redeemableCap, &o.ExpiresAt, &o.CreatedAt, &o.Status, &o.Type)
|
|
|
|
if err != nil {
|
|
|
|
return results, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
o.AwardCredit = currency.Cents(awardCreditInCents)
|
|
|
|
o.InviteeCredit = currency.Cents(inviteeCreditInCents)
|
|
|
|
if redeemableCap.Valid {
|
|
|
|
o.RedeemableCap = int(redeemableCap.Int64)
|
|
|
|
}
|
|
|
|
if awardCreditDurationDays.Valid {
|
|
|
|
o.AwardCreditDurationDays = int(awardCreditDurationDays.Int64)
|
|
|
|
}
|
|
|
|
if inviteeCreditDurationDays.Valid {
|
|
|
|
o.InviteeCreditDurationDays = int(inviteeCreditDurationDays.Int64)
|
|
|
|
}
|
2019-12-05 03:54:43 +00:00
|
|
|
o.ExpiresAt = o.ExpiresAt.UTC()
|
|
|
|
o.CreatedAt = o.CreatedAt.UTC()
|
|
|
|
|
2019-08-01 18:46:33 +01:00
|
|
|
results = append(results, o)
|
2019-07-18 18:26:04 +01:00
|
|
|
}
|
2019-06-04 20:17:01 +01:00
|
|
|
|
2019-08-01 18:46:33 +01:00
|
|
|
if len(results) < 1 {
|
2019-11-06 18:37:53 +00:00
|
|
|
return results, rewards.ErrOfferNotExist.New("offerType: %d", offerType)
|
2019-08-01 18:46:33 +01:00
|
|
|
}
|
|
|
|
return results, nil
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-05 03:54:43 +00:00
|
|
|
currentTime := time.Now().UTC()
|
2019-06-04 20:17:01 +01:00
|
|
|
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)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 03:54:43 +00:00
|
|
|
var id int64
|
|
|
|
|
2020-01-15 07:25:26 +00:00
|
|
|
err := txutil.WithTx(ctx, db.db.DB.DB, nil, func(ctx context.Context, tx *sql.Tx) error {
|
2019-12-05 03:54:43 +00:00
|
|
|
// If there's an existing current offer, update its status to Done and set its expires_at to be NOW()
|
|
|
|
switch o.Type {
|
|
|
|
case rewards.Partner:
|
|
|
|
statement := `
|
|
|
|
UPDATE offers SET status=?, expires_at=?
|
|
|
|
WHERE status=? AND type=? AND expires_at>? AND name=?;`
|
|
|
|
_, err := tx.ExecContext(ctx, db.db.Rebind(statement), rewards.Done, currentTime, o.Status, o.Type, currentTime, o.Name)
|
|
|
|
if err != nil {
|
|
|
|
return offerErr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
statement := `
|
|
|
|
UPDATE offers SET status=?, expires_at=?
|
|
|
|
WHERE status=? AND type=? AND expires_at>?;`
|
|
|
|
_, err := tx.ExecContext(ctx, db.db.Rebind(statement), rewards.Done, currentTime, o.Status, o.Type, currentTime)
|
|
|
|
if err != nil {
|
|
|
|
return offerErr.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 17:01:18 +01:00
|
|
|
statement := `
|
2020-01-15 07:25:26 +00:00
|
|
|
INSERT INTO offers (name, description, award_credit_in_cents, invitee_credit_in_cents, award_credit_duration_days,
|
2019-12-05 03:54:43 +00:00
|
|
|
invitee_credit_duration_days, redeemable_cap, expires_at, created_at, status, type)
|
|
|
|
VALUES (?::TEXT, ?::TEXT, ?::INT, ?::INT, ?::INT, ?::INT, ?::INT, ?::timestamptz, ?::timestamptz, ?::INT, ?::INT)
|
|
|
|
RETURNING id;
|
|
|
|
`
|
|
|
|
row := tx.QueryRowContext(ctx, db.db.Rebind(statement),
|
|
|
|
o.Name,
|
|
|
|
o.Description,
|
|
|
|
o.AwardCredit.Cents(),
|
|
|
|
o.InviteeCredit.Cents(),
|
|
|
|
o.AwardCreditDurationDays,
|
|
|
|
o.InviteeCreditDurationDays,
|
|
|
|
o.RedeemableCap,
|
|
|
|
o.ExpiresAt,
|
|
|
|
currentTime,
|
|
|
|
o.Status,
|
|
|
|
o.Type,
|
|
|
|
)
|
2019-06-04 20:17:01 +01:00
|
|
|
|
2019-12-05 03:54:43 +00:00
|
|
|
return row.Scan(&id)
|
|
|
|
})
|
|
|
|
|
|
|
|
return &rewards.Offer{
|
|
|
|
ID: int(id),
|
|
|
|
Name: o.Name,
|
|
|
|
Description: o.Description,
|
|
|
|
AwardCredit: o.AwardCredit,
|
|
|
|
InviteeCredit: o.InviteeCredit,
|
|
|
|
AwardCreditDurationDays: o.AwardCreditDurationDays,
|
|
|
|
InviteeCreditDurationDays: o.InviteeCreditDurationDays,
|
|
|
|
RedeemableCap: o.RedeemableCap,
|
|
|
|
ExpiresAt: o.ExpiresAt,
|
|
|
|
CreatedAt: currentTime,
|
|
|
|
Status: o.Status,
|
|
|
|
Type: o.Type,
|
|
|
|
}, offerErr.Wrap(err)
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:53:19 +01:00
|
|
|
// 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-09-12 18:31:50 +01:00
|
|
|
return offerErr.Wrap(
|
|
|
|
db.db.UpdateNoReturn_Offer_By_Id(ctx,
|
|
|
|
dbx.Offer_Id(oID), dbx.Offer_Update_Fields{
|
|
|
|
Status: dbx.Offer_Status(int(rewards.Done)),
|
|
|
|
ExpiresAt: dbx.Offer_ExpiresAt(time.Now().UTC()),
|
|
|
|
}))
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
2019-07-10 18:12:40 +01:00
|
|
|
func offersFromDBX(offersDbx []*dbx.Offer) (rewards.Offers, error) {
|
2019-06-24 21:51:54 +01:00
|
|
|
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-07-16 00:30:00 +01:00
|
|
|
|
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-07-12 15:19:38 +01:00
|
|
|
var redeemableCap, awardCreditDurationDays, inviteeCreditDurationDays int
|
|
|
|
if offerDbx.RedeemableCap != nil {
|
|
|
|
redeemableCap = *offerDbx.RedeemableCap
|
|
|
|
}
|
|
|
|
if offerDbx.AwardCreditDurationDays != nil {
|
|
|
|
awardCreditDurationDays = *offerDbx.AwardCreditDurationDays
|
|
|
|
}
|
|
|
|
if offerDbx.InviteeCreditDurationDays != nil {
|
|
|
|
inviteeCreditDurationDays = *offerDbx.InviteeCreditDurationDays
|
|
|
|
}
|
|
|
|
|
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-07-12 15:19:38 +01:00
|
|
|
RedeemableCap: redeemableCap,
|
2019-12-05 03:54:43 +00:00
|
|
|
ExpiresAt: offerDbx.ExpiresAt.UTC(),
|
2019-07-12 15:19:38 +01:00
|
|
|
AwardCreditDurationDays: awardCreditDurationDays,
|
|
|
|
InviteeCreditDurationDays: inviteeCreditDurationDays,
|
2019-12-05 03:54:43 +00:00
|
|
|
CreatedAt: offerDbx.CreatedAt.UTC(),
|
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
|
|
|
|
}
|