2019-11-05 13:16:02 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-05 13:16:02 +00:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2019-11-05 13:16:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ensure that invoiceProjectRecords implements stripecoinpayments.ProjectRecordsDB.
|
|
|
|
var _ stripecoinpayments.ProjectRecordsDB = (*invoiceProjectRecords)(nil)
|
|
|
|
|
|
|
|
// invoiceProjectRecordState defines states of the invoice project record.
|
|
|
|
type invoiceProjectRecordState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// invoice project record is not yet applied to customer invoice.
|
|
|
|
invoiceProjectRecordStateUnapplied invoiceProjectRecordState = 0
|
|
|
|
// invoice project record has been used during creating customer invoice.
|
|
|
|
invoiceProjectRecordStateConsumed invoiceProjectRecordState = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Int returns intent state as int.
|
|
|
|
func (intent invoiceProjectRecordState) Int() int {
|
|
|
|
return int(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoiceProjectRecords is stripecoinpayments project records DB.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type invoiceProjectRecords struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2019-11-05 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates new invoice project record in the DB.
|
2020-02-11 14:48:28 +00:00
|
|
|
func (db *invoiceProjectRecords) Create(ctx context.Context, records []stripecoinpayments.CreateProjectRecord, couponUsages []stripecoinpayments.CouponUsage, creditsSpendings []stripecoinpayments.CreditsSpending, start, end time.Time) (err error) {
|
2019-11-05 13:16:02 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
|
|
for _, record := range records {
|
|
|
|
id, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.db.Create_StripecoinpaymentsInvoiceProjectRecord(ctx,
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Id(id[:]),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_ProjectId(record.ProjectID[:]),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Storage(record.Storage),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Egress(record.Egress),
|
2020-01-07 10:41:19 +00:00
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Objects(int64(record.Objects)),
|
2019-11-05 13:16:02 +00:00
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodStart(start),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodEnd(end),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_State(invoiceProjectRecordStateUnapplied.Int()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
for _, couponUsage := range couponUsages {
|
|
|
|
_, err = db.db.Create_CouponUsage(
|
|
|
|
ctx,
|
|
|
|
dbx.CouponUsage_CouponId(couponUsage.CouponID[:]),
|
|
|
|
dbx.CouponUsage_Amount(couponUsage.Amount),
|
|
|
|
dbx.CouponUsage_Status(int(couponUsage.Status)),
|
|
|
|
dbx.CouponUsage_Period(couponUsage.Period),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 14:48:28 +00:00
|
|
|
|
|
|
|
for _, creditsSpending := range creditsSpendings {
|
|
|
|
_, err = db.db.Create_CreditsSpending(
|
|
|
|
ctx,
|
|
|
|
dbx.CreditsSpending_Id(creditsSpending.ID[:]),
|
|
|
|
dbx.CreditsSpending_UserId(creditsSpending.UserID[:]),
|
|
|
|
dbx.CreditsSpending_ProjectId(creditsSpending.ProjectID[:]),
|
|
|
|
dbx.CreditsSpending_Amount(creditsSpending.Amount),
|
|
|
|
dbx.CreditsSpending_Status(int(creditsSpending.Status)),
|
2020-05-12 09:46:48 +01:00
|
|
|
dbx.CreditsSpending_Period(creditsSpending.Period),
|
2020-02-11 14:48:28 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check checks if invoice project record for specified project and billing period exists.
|
|
|
|
func (db *invoiceProjectRecords) Check(ctx context.Context, projectID uuid.UUID, start, end time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.db.Get_StripecoinpaymentsInvoiceProjectRecord_By_ProjectId_And_PeriodStart_And_PeriodEnd(ctx,
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_ProjectId(projectID[:]),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodStart(start),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodEnd(end),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stripecoinpayments.ErrProjectRecordExists
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
// Get returns record for specified project and billing period.
|
|
|
|
func (db *invoiceProjectRecords) Get(ctx context.Context, projectID uuid.UUID, start, end time.Time) (record *stripecoinpayments.ProjectRecord, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
dbxRecord, err := db.db.Get_StripecoinpaymentsInvoiceProjectRecord_By_ProjectId_And_PeriodStart_And_PeriodEnd(ctx,
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_ProjectId(projectID[:]),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodStart(start),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodEnd(end),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromDBXInvoiceProjectRecord(dbxRecord)
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
// Consume consumes invoice project record.
|
|
|
|
func (db *invoiceProjectRecords) Consume(ctx context.Context, id uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.db.Update_StripecoinpaymentsInvoiceProjectRecord_By_Id(ctx,
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Id(id[:]),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_Update_Fields{
|
|
|
|
State: dbx.StripecoinpaymentsInvoiceProjectRecord_State(invoiceProjectRecordStateConsumed.Int()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUnapplied returns project records page with unapplied project records.
|
2020-05-12 09:46:48 +01:00
|
|
|
func (db *invoiceProjectRecords) ListUnapplied(ctx context.Context, offset int64, limit int, start, end time.Time) (_ stripecoinpayments.ProjectRecordsPage, err error) {
|
2019-11-05 13:16:02 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var page stripecoinpayments.ProjectRecordsPage
|
|
|
|
|
2020-05-12 09:46:48 +01:00
|
|
|
dbxRecords, err := db.db.Limited_StripecoinpaymentsInvoiceProjectRecord_By_PeriodStart_And_PeriodEnd_And_State(ctx,
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodStart(start),
|
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_PeriodEnd(end),
|
2019-11-05 13:16:02 +00:00
|
|
|
dbx.StripecoinpaymentsInvoiceProjectRecord_State(invoiceProjectRecordStateUnapplied.Int()),
|
|
|
|
limit+1,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.ProjectRecordsPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbxRecords) == limit+1 {
|
|
|
|
page.Next = true
|
2020-02-10 14:44:48 +00:00
|
|
|
page.NextOffset = offset + int64(limit)
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
dbxRecords = dbxRecords[:len(dbxRecords)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbxRecord := range dbxRecords {
|
|
|
|
record, err := fromDBXInvoiceProjectRecord(dbxRecord)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.ProjectRecordsPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.Records = append(page.Records, *record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromDBXInvoiceProjectRecord converts *dbx.StripecoinpaymentsInvoiceProjectRecord to *stripecoinpayments.ProjectRecord
|
|
|
|
func fromDBXInvoiceProjectRecord(dbxRecord *dbx.StripecoinpaymentsInvoiceProjectRecord) (*stripecoinpayments.ProjectRecord, error) {
|
2020-03-31 17:49:16 +01:00
|
|
|
id, err := uuid.FromBytes(dbxRecord.Id)
|
2019-11-05 13:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
2020-03-31 17:49:16 +01:00
|
|
|
projectID, err := uuid.FromBytes(dbxRecord.ProjectId)
|
2019-11-05 13:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stripecoinpayments.ProjectRecord{
|
|
|
|
ID: id,
|
|
|
|
ProjectID: projectID,
|
|
|
|
Storage: dbxRecord.Storage,
|
|
|
|
Egress: dbxRecord.Egress,
|
2020-01-07 10:41:19 +00:00
|
|
|
Objects: float64(dbxRecord.Objects),
|
2019-11-05 13:16:02 +00:00
|
|
|
PeriodStart: dbxRecord.PeriodStart,
|
|
|
|
PeriodEnd: dbxRecord.PeriodEnd,
|
|
|
|
}, nil
|
|
|
|
}
|