2019-11-26 17:58:51 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-01-18 02:34:06 +00:00
|
|
|
"fmt"
|
2020-06-01 13:48:51 +01:00
|
|
|
"sort"
|
2019-11-26 17:58:51 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
"github.com/lib/pq"
|
2019-11-26 17:58:51 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-01-18 02:34:06 +00:00
|
|
|
"storj.io/common/memory"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-01-18 02:34:06 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-11-26 17:58:51 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2019-11-26 17:58:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that coupons implements payments.CouponsDB.
|
|
|
|
var _ stripecoinpayments.CouponsDB = (*coupons)(nil)
|
|
|
|
|
|
|
|
// coupons is an implementation of payments.CouponsDB.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type coupons struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert inserts a coupon into the database.
|
2020-05-19 13:49:44 +01:00
|
|
|
func (coupons *coupons) Insert(ctx context.Context, coupon payments.Coupon) (_ payments.Coupon, err error) {
|
2019-11-26 17:58:51 +00:00
|
|
|
defer mon.Task()(&ctx, coupon)(&err)
|
|
|
|
|
|
|
|
id, err := uuid.New()
|
|
|
|
if err != nil {
|
2020-05-19 11:36:13 +01:00
|
|
|
return payments.Coupon{}, err
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 11:36:13 +01:00
|
|
|
cpx, err := coupons.db.Create_Coupon(
|
2019-11-26 17:58:51 +00:00
|
|
|
ctx,
|
|
|
|
dbx.Coupon_Id(id[:]),
|
|
|
|
dbx.Coupon_UserId(coupon.UserID[:]),
|
|
|
|
dbx.Coupon_Amount(coupon.Amount),
|
|
|
|
dbx.Coupon_Description(coupon.Description),
|
2020-01-07 10:41:19 +00:00
|
|
|
dbx.Coupon_Type(int(coupon.Type)),
|
2019-11-26 17:58:51 +00:00
|
|
|
dbx.Coupon_Status(int(coupon.Status)),
|
|
|
|
dbx.Coupon_Duration(int64(coupon.Duration)),
|
|
|
|
)
|
2020-05-19 11:36:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return payments.Coupon{}, err
|
|
|
|
}
|
|
|
|
return fromDBXCoupon(cpx)
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates coupon in database.
|
2020-05-19 13:49:44 +01:00
|
|
|
func (coupons *coupons) Update(ctx context.Context, couponID uuid.UUID, status payments.CouponStatus) (_ payments.Coupon, err error) {
|
2019-11-26 17:58:51 +00:00
|
|
|
defer mon.Task()(&ctx, couponID)(&err)
|
|
|
|
|
2020-05-19 11:36:13 +01:00
|
|
|
cpx, err := coupons.db.Update_Coupon_By_Id(
|
2019-11-26 17:58:51 +00:00
|
|
|
ctx,
|
|
|
|
dbx.Coupon_Id(couponID[:]),
|
|
|
|
dbx.Coupon_Update_Fields{
|
|
|
|
Status: dbx.Coupon_Status(int(status)),
|
|
|
|
},
|
|
|
|
)
|
2020-05-19 11:36:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return payments.Coupon{}, err
|
|
|
|
}
|
|
|
|
return fromDBXCoupon(cpx)
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
// Get returns coupon by ID.
|
|
|
|
func (coupons *coupons) Get(ctx context.Context, couponID uuid.UUID) (_ payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx, couponID)(&err)
|
|
|
|
|
|
|
|
dbxCoupon, err := coupons.db.Get_Coupon_By_Id(ctx, dbx.Coupon_Id(couponID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return payments.Coupon{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromDBXCoupon(dbxCoupon)
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:44 +01:00
|
|
|
// Delete removes a coupon from the database by its ID
|
|
|
|
func (coupons *coupons) Delete(ctx context.Context, couponID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx, couponID)(&err)
|
|
|
|
|
|
|
|
_, err = coupons.db.Delete_Coupon_By_Id(ctx, dbx.Coupon_Id(couponID[:]))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// List returns all coupons of specified user.
|
|
|
|
func (coupons *coupons) ListByUserID(ctx context.Context, userID uuid.UUID) (_ []payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
dbxCoupons, err := coupons.db.All_Coupon_By_UserId_OrderBy_Desc_CreatedAt(
|
|
|
|
ctx,
|
|
|
|
dbx.Coupon_UserId(userID[:]),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return couponsFromDbxSlice(dbxCoupons)
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:48:51 +01:00
|
|
|
// ListByUserIDAndStatus returns all coupons of specified user and status. Results are ordered (asc) by expiration date.
|
2020-01-07 10:41:19 +00:00
|
|
|
func (coupons *coupons) ListByUserIDAndStatus(ctx context.Context, userID uuid.UUID, status payments.CouponStatus) (_ []payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
dbxCoupons, err := coupons.db.All_Coupon_By_UserId_And_Status_OrderBy_Desc_CreatedAt(
|
|
|
|
ctx,
|
|
|
|
dbx.Coupon_UserId(userID[:]),
|
|
|
|
dbx.Coupon_Status(int(status)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:48:51 +01:00
|
|
|
result, err := couponsFromDbxSlice(dbxCoupons)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(result, func(i, k int) bool {
|
|
|
|
iDate := result[i].ExpirationDate()
|
|
|
|
kDate := result[k].ExpirationDate()
|
|
|
|
return iDate.Before(kDate)
|
|
|
|
})
|
|
|
|
|
|
|
|
return result, nil
|
2020-01-07 10:41:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// List returns all coupons with specified status.
|
|
|
|
func (coupons *coupons) List(ctx context.Context, status payments.CouponStatus) (_ []payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx, status)(&err)
|
|
|
|
|
|
|
|
dbxCoupons, err := coupons.db.All_Coupon_By_Status_OrderBy_Desc_CreatedAt(
|
|
|
|
ctx,
|
|
|
|
dbx.Coupon_Status(int(status)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return couponsFromDbxSlice(dbxCoupons)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPending returns paginated list of pending transactions.
|
|
|
|
func (coupons *coupons) ListPaged(ctx context.Context, offset int64, limit int, before time.Time, status payments.CouponStatus) (_ payments.CouponsPage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var page payments.CouponsPage
|
|
|
|
|
|
|
|
dbxCoupons, err := coupons.db.Limited_Coupon_By_CreatedAt_LessOrEqual_And_Status_OrderBy_Desc_CreatedAt(
|
|
|
|
ctx,
|
|
|
|
dbx.Coupon_CreatedAt(before.UTC()),
|
|
|
|
dbx.Coupon_Status(coinpayments.StatusPending.Int()),
|
|
|
|
limit+1,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return payments.CouponsPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbxCoupons) == limit+1 {
|
|
|
|
page.Next = true
|
|
|
|
page.NextOffset = offset + int64(limit) + 1
|
|
|
|
|
|
|
|
dbxCoupons = dbxCoupons[:len(dbxCoupons)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
page.Coupons, err = couponsFromDbxSlice(dbxCoupons)
|
|
|
|
if err != nil {
|
|
|
|
return payments.CouponsPage{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromDBXCoupon converts *dbx.Coupon to *payments.Coupon.
|
|
|
|
func fromDBXCoupon(dbxCoupon *dbx.Coupon) (coupon payments.Coupon, err error) {
|
2020-03-31 17:49:16 +01:00
|
|
|
coupon.UserID, err = uuid.FromBytes(dbxCoupon.UserId)
|
2019-11-26 17:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return payments.Coupon{}, err
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
coupon.ID, err = uuid.FromBytes(dbxCoupon.Id)
|
2019-11-26 17:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return payments.Coupon{}, err
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
coupon.Duration = int(dbxCoupon.Duration)
|
2019-11-26 17:58:51 +00:00
|
|
|
coupon.Description = dbxCoupon.Description
|
|
|
|
coupon.Amount = dbxCoupon.Amount
|
|
|
|
coupon.Created = dbxCoupon.CreatedAt
|
|
|
|
coupon.Status = payments.CouponStatus(dbxCoupon.Status)
|
|
|
|
|
|
|
|
return coupon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddUsage creates new coupon usage record in database.
|
|
|
|
func (coupons *coupons) AddUsage(ctx context.Context, usage stripecoinpayments.CouponUsage) (err error) {
|
|
|
|
defer mon.Task()(&ctx, usage)(&err)
|
|
|
|
|
|
|
|
_, err = coupons.db.Create_CouponUsage(
|
|
|
|
ctx,
|
|
|
|
dbx.CouponUsage_CouponId(usage.CouponID[:]),
|
|
|
|
dbx.CouponUsage_Amount(usage.Amount),
|
2020-01-07 10:41:19 +00:00
|
|
|
dbx.CouponUsage_Status(int(usage.Status)),
|
|
|
|
dbx.CouponUsage_Period(usage.Period),
|
2019-11-26 17:58:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalUsage gets 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)
|
|
|
|
|
|
|
|
query := coupons.db.Rebind(
|
|
|
|
`SELECT COALESCE(SUM(amount), 0)
|
|
|
|
FROM coupon_usages
|
|
|
|
WHERE coupon_id = ?;`,
|
|
|
|
)
|
|
|
|
|
|
|
|
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
|
|
|
|
|
|
|
|
var amount int64
|
|
|
|
err = amountRow.Scan(&amount)
|
|
|
|
|
|
|
|
return amount, err
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
// TotalUsage gets sum of all usage records for specified coupon.
|
|
|
|
func (coupons *coupons) TotalUsageForPeriod(ctx context.Context, couponID uuid.UUID, period time.Time) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, couponID)(&err)
|
|
|
|
|
|
|
|
query := coupons.db.Rebind(
|
|
|
|
`SELECT COALESCE(SUM(amount), 0)
|
|
|
|
FROM coupon_usages
|
|
|
|
WHERE coupon_id = ?;`,
|
|
|
|
)
|
|
|
|
|
|
|
|
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
|
|
|
|
|
|
|
|
var amount int64
|
|
|
|
err = amountRow.Scan(&amount)
|
|
|
|
|
|
|
|
return amount, err
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// GetLatest return period_end of latest coupon charge.
|
|
|
|
func (coupons *coupons) GetLatest(ctx context.Context, couponID uuid.UUID) (_ time.Time, err error) {
|
|
|
|
defer mon.Task()(&ctx, couponID)(&err)
|
|
|
|
|
|
|
|
query := coupons.db.Rebind(
|
2020-01-07 10:41:19 +00:00
|
|
|
`SELECT period
|
2019-11-26 17:58:51 +00:00
|
|
|
FROM coupon_usages
|
|
|
|
WHERE coupon_id = ?
|
2020-01-07 10:41:19 +00:00
|
|
|
ORDER BY period DESC
|
2019-11-26 17:58:51 +00:00
|
|
|
LIMIT 1;`,
|
|
|
|
)
|
|
|
|
|
|
|
|
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
|
|
|
|
|
|
|
|
var created time.Time
|
|
|
|
err = amountRow.Scan(&created)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return created, stripecoinpayments.ErrNoCouponUsages.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
return created, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUnapplied returns coupon usage page with unapplied coupon usages.
|
2020-05-12 09:46:48 +01:00
|
|
|
func (coupons *coupons) ListUnapplied(ctx context.Context, offset int64, limit int, period time.Time) (_ stripecoinpayments.CouponUsagePage, err error) {
|
|
|
|
defer mon.Task()(&ctx, offset, limit, period)(&err)
|
2020-01-07 10:41:19 +00:00
|
|
|
|
|
|
|
var page stripecoinpayments.CouponUsagePage
|
|
|
|
|
2020-05-12 09:46:48 +01:00
|
|
|
dbxRecords, err := coupons.db.Limited_CouponUsage_By_Period_And_Status_Equal_Number(
|
2020-01-07 10:41:19 +00:00
|
|
|
ctx,
|
2020-05-12 09:46:48 +01:00
|
|
|
dbx.CouponUsage_Period(period),
|
2020-01-07 10:41:19 +00:00
|
|
|
limit+1,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.CouponUsagePage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbxRecords) == limit+1 {
|
|
|
|
page.Next = true
|
|
|
|
page.NextOffset = offset + int64(limit) + 1
|
|
|
|
|
|
|
|
dbxRecords = dbxRecords[:len(dbxRecords)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbxRecord := range dbxRecords {
|
|
|
|
record, err := couponUsageFromDbxSlice(dbxRecord)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.CouponUsagePage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.Usages = append(page.Usages, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyUsage applies coupon usage and updates its status.
|
|
|
|
func (coupons *coupons) ApplyUsage(ctx context.Context, couponID uuid.UUID, period time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx, couponID, period)(&err)
|
|
|
|
|
|
|
|
_, err = coupons.db.Update_CouponUsage_By_CouponId_And_Period(
|
|
|
|
ctx,
|
|
|
|
dbx.CouponUsage_CouponId(couponID[:]),
|
|
|
|
dbx.CouponUsage_Period(period),
|
|
|
|
dbx.CouponUsage_Update_Fields{
|
|
|
|
Status: dbx.CouponUsage_Status(int(stripecoinpayments.CouponUsageStatusApplied)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// couponsFromDbxSlice is used for creating []payments.Coupon entities from autogenerated []dbx.Coupon struct.
|
|
|
|
func couponsFromDbxSlice(couponsDbx []*dbx.Coupon) (_ []payments.Coupon, err error) {
|
|
|
|
var coupons = make([]payments.Coupon, 0)
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
// Generating []dbo from []dbx and collecting all errors
|
|
|
|
for _, couponDbx := range couponsDbx {
|
|
|
|
coupon, err := fromDBXCoupon(couponDbx)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
coupons = append(coupons, coupon)
|
|
|
|
}
|
|
|
|
|
|
|
|
return coupons, errs.Combine(errors...)
|
|
|
|
}
|
2020-01-07 10:41:19 +00:00
|
|
|
|
|
|
|
// couponUsageFromDbxSlice is used for creating stripecoinpayments.CouponUsage entity from autogenerated dbx.CouponUsage struct.
|
|
|
|
func couponUsageFromDbxSlice(couponUsageDbx *dbx.CouponUsage) (usage stripecoinpayments.CouponUsage, err error) {
|
|
|
|
usage.Status = stripecoinpayments.CouponUsageStatus(couponUsageDbx.Status)
|
|
|
|
usage.Period = couponUsageDbx.Period
|
|
|
|
usage.Amount = couponUsageDbx.Amount
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
usage.CouponID, err = uuid.FromBytes(couponUsageDbx.CouponId)
|
2020-01-07 10:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.CouponUsage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return usage, err
|
|
|
|
}
|
2020-01-18 02:34:06 +00:00
|
|
|
|
|
|
|
// PopulatePromotionalCoupons is used to populate promotional coupons through all active users who already have a project
|
|
|
|
// and do not have a promotional coupon yet. And updates project limits to selected size.
|
|
|
|
func (coupons *coupons) PopulatePromotionalCoupons(ctx context.Context, users []uuid.UUID, duration int, amount int64, projectLimit memory.Size) (err error) {
|
|
|
|
defer mon.Task()(&ctx, users, duration, amount, projectLimit)(&err)
|
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
ids, err := coupons.activeUserWithProjectAndWithoutCoupon(ctx, users)
|
2020-01-18 02:34:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return coupons.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
2020-01-20 12:27:38 +00:00
|
|
|
for _, id := range ids {
|
2020-05-19 11:36:13 +01:00
|
|
|
_, err = coupons.Insert(ctx, payments.Coupon{
|
2020-01-20 12:27:38 +00:00
|
|
|
UserID: id.UserID,
|
2020-01-18 02:34:06 +00:00
|
|
|
Amount: amount,
|
|
|
|
Duration: duration,
|
|
|
|
Description: fmt.Sprintf("Promotional credits (limited time - %d billing periods)", duration),
|
|
|
|
Type: payments.CouponTypePromotional,
|
|
|
|
Status: payments.CouponActive,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:34:15 +00:00
|
|
|
// if projectLimit specified, set it, else omit change the existing value
|
|
|
|
if projectLimit.Int64() > 0 {
|
|
|
|
_, err = coupons.db.Update_Project_By_Id(ctx,
|
|
|
|
dbx.Project_Id(id.ProjectID[:]),
|
|
|
|
dbx.Project_Update_Fields{
|
|
|
|
UsageLimit: dbx.Project_UsageLimit(projectLimit.Int64()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2020-01-18 02:34:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
type userAndProject struct {
|
|
|
|
UserID uuid.UUID
|
|
|
|
ProjectID uuid.UUID
|
|
|
|
}
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
func (coupons *coupons) activeUserWithProjectAndWithoutCoupon(ctx context.Context, users []uuid.UUID) (ids []userAndProject, err error) {
|
|
|
|
var userIDs [][]byte
|
|
|
|
for i := range users {
|
|
|
|
userIDs = append(userIDs, users[i][:])
|
2020-01-18 02:34:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
rows, err := coupons.db.QueryContext(ctx, coupons.db.Rebind(`
|
|
|
|
SELECT users_with_projects.id, users_with_projects.project_id
|
|
|
|
FROM (
|
|
|
|
SELECT selected_users.id, first_proj.id AS project_id
|
|
|
|
FROM (
|
|
|
|
SELECT id, status
|
|
|
|
FROM users
|
|
|
|
WHERE id = any(?)
|
|
|
|
) AS selected_users
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT DISTINCT ON (owner_id) owner_id, id
|
|
|
|
FROM projects
|
|
|
|
ORDER BY owner_id, created_at ASC
|
|
|
|
) AS first_proj
|
|
|
|
ON selected_users.id = first_proj.owner_id
|
|
|
|
WHERE selected_users.status = ?
|
|
|
|
) AS users_with_projects
|
|
|
|
WHERE users_with_projects.id NOT IN (
|
|
|
|
SELECT user_id FROM coupons WHERE type = ?
|
|
|
|
)
|
|
|
|
`), pq.ByteaArray(userIDs), console.Active, payments.CouponTypePromotional)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-18 02:34:06 +00:00
|
|
|
}
|
2020-01-20 12:27:38 +00:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var id userAndProject
|
2020-03-31 17:49:16 +01:00
|
|
|
err = rows.Scan(&id.UserID, &id.ProjectID)
|
2020-01-20 12:27:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
2020-01-18 02:34:06 +00:00
|
|
|
|
2020-01-20 12:27:38 +00:00
|
|
|
return ids, rows.Err()
|
2020-01-18 02:34:06 +00:00
|
|
|
}
|