Skip to content

Commit

Permalink
cms: Fix Invoice payment check and records (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlyp authored Dec 7, 2020
1 parent 296112d commit a3a16ab
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
36 changes: 30 additions & 6 deletions politeiawww/cmsaddresswatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ func (p *politeiawww) restartCMSAddressesWatching(ctx context.Context) error {
p.addWatchAddress(payments.Address)
}
}

// Also check to make sure all paid invoices in the database have
// corresponding payment data saved in records.
paidPayments, err := p.cmsDB.PaymentsByStatus(uint(cms.PaymentStatusPaid))
if err != nil {
return err
}
for _, payments := range paidPayments {
if payments.TxIDs == "" && payments.AmountReceived == 0 {
payments.Address = strings.TrimSpace(payments.Address)
paid := p.checkHistoricalPayments(ctx, &payments)
if !paid {
log.Errorf("found payment for invoice that is set to paid "+
"no payment found %v", payments.InvoiceToken)
}
} else {
log.Debugf("payment for %v has is proper txids %v and "+
"amount received %v", payments.InvoiceToken, payments.TxIDs,
payments.AmountReceived)
}
}

return nil
}

Expand Down Expand Up @@ -222,6 +244,9 @@ func (p *politeiawww) checkHistoricalPayments(ctx context.Context, payment *data
} else {
txIDs += ", " + tx.TxID
}
if payment.TimeLastUpdated < tx.Timestamp {
payment.TimeLastUpdated = tx.Timestamp
}
}
payment.TxIDs = txIDs

Expand All @@ -238,9 +263,8 @@ func (p *politeiawww) checkHistoricalPayments(ctx context.Context, payment *data
payment.Status = cms.PaymentStatusPaid
}
payment.AmountReceived = int64(amountReceived)
payment.TimeLastUpdated = time.Now().Unix()

err = p.cmsDB.UpdatePayments(payment)
err = p.updateInvoicePayment(ctx, payment)
if err != nil {
log.Errorf("Error updating payments information for: %v %v",
payment.Address, err)
Expand All @@ -249,7 +273,7 @@ func (p *politeiawww) checkHistoricalPayments(ctx context.Context, payment *data
if payment.Status == cms.PaymentStatusPaid {
log.Debugf("Updating invoice %v status to paid", payment.InvoiceToken)
// Update invoice status here
err := p.invoiceStatusPaid(ctx, payment.InvoiceToken)
err := p.invoiceStatusPaid(ctx, payment.InvoiceToken, payment.InvoiceKey)
if err != nil {
log.Errorf("error updating invoice status to paid %v", err)
}
Expand Down Expand Up @@ -313,7 +337,7 @@ func (p *politeiawww) checkPayments(ctx context.Context, payment *database.Payme
if payment.Status == cms.PaymentStatusPaid {
log.Debugf("Updating invoice %v status to paid", payment.InvoiceToken)
// Update invoice status here
err := p.invoiceStatusPaid(ctx, payment.InvoiceToken)
err := p.invoiceStatusPaid(ctx, payment.InvoiceToken, payment.InvoiceKey)
if err != nil {
log.Errorf("error updating invoice status to paid %v", err)
}
Expand Down Expand Up @@ -377,8 +401,8 @@ func (p *politeiawww) updateInvoicePayment(ctx context.Context, payment *databas
}
return nil
}
func (p *politeiawww) invoiceStatusPaid(ctx context.Context, token string) error {
dbInvoice, err := p.cmsDB.InvoiceByToken(token)
func (p *politeiawww) invoiceStatusPaid(ctx context.Context, token, key string) error {
dbInvoice, err := p.cmsDB.InvoiceByKey(key)
if err != nil {
if errors.Is(err, cache.ErrRecordNotFound) {
err = www.UserError{
Expand Down
25 changes: 24 additions & 1 deletion politeiawww/cmsdatabase/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ func (c *cockroachdb) InvoiceByToken(token string) (*database.Invoice, error) {
return DecodeInvoice(&invoice)
}

// InvoiceByKey Return invoice by its key.
func (c *cockroachdb) InvoiceByKey(key string) (*database.Invoice, error) {
log.Debugf("InvoiceByKey: %v", key)

invoice := Invoice{}
err := c.recordsdb.
Where("key = ?", key).
Order("version desc").
Limit(1).
Preload("LineItems").
Preload("Changes").
Preload("Payments").
Find(&invoice).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
err = database.ErrInvoiceNotFound
}
return nil, err
}

return DecodeInvoice(&invoice)
}

// InvoiceByTokenVersion Return invoice by its token and version
func (c *cockroachdb) InvoiceByTokenVersion(token string, version string) (*database.Invoice, error) {
log.Debugf("InvoiceByTokenVersion: %v", token)
Expand Down Expand Up @@ -916,7 +939,7 @@ func New(host, net, rootCert, cert, key string) (*cockroachdb, error) {
func (c *cockroachdb) UpdatePayments(dbPayments *database.Payments) error {
payments := encodePayments(dbPayments)

log.Debugf("UpdatePayments: %v", payments.InvoiceToken)
log.Debugf("UpdatePayments: %v", payments.InvoiceKey)

return c.recordsdb.Save(&payments).Error
}
Expand Down
1 change: 1 addition & 0 deletions politeiawww/cmsdatabase/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Database interface {
InvoiceByToken(string) (*Invoice, error) // Return invoice given its token
InvoiceByTokenVersion(token string, version string) (*Invoice, error) // Return invoice by its token and version
InvoicesByAddress(string) ([]Invoice, error) // Return invoice by its address
InvoiceByKey(string) (*Invoice, error) // Return invoice given its key

InvoicesByMonthYearStatus(uint16, uint16, int) ([]Invoice, error) // Returns all invoices by month, year and status
InvoicesByMonthYear(uint16, uint16) ([]Invoice, error) // Returns all invoice by month, year
Expand Down
3 changes: 1 addition & 2 deletions politeiawww/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,6 @@ func convertRecordToDatabaseInvoice(p pd.Record) (*cmsdatabase.Invoice, error) {
payment.Status = cms.PaymentStatusPaid
}
}
dbInvoice.Changes = invChanges

case mdstream.IDInvoicePayment:
ip, err := mdstream.DecodeInvoicePayment([]byte(m.Payload))
Expand All @@ -1071,14 +1070,14 @@ func convertRecordToDatabaseInvoice(p pd.Record) (*cmsdatabase.Invoice, error) {
payment.TimeLastUpdated = s.Timestamp
payment.AmountReceived = s.AmountReceived
}
dbInvoice.Payments = payment
default:
// Log error but proceed
log.Errorf("convertRecordToInvoiceDB: invalid "+
"metadata stream ID %v token %v",
m.ID, p.CensorshipRecord.Token)
}
}
dbInvoice.Payments = payment

return &dbInvoice, nil
}
Expand Down
1 change: 1 addition & 0 deletions politeiawww/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ func (p *politeiawww) processSetInvoiceStatus(ctx context.Context, sis cms.SetIn
// If approved then update Invoice's Payment table in DB
if c.NewStatus == cms.InvoiceStatusApproved {
dbInvoice.Payments = database.Payments{
InvoiceToken: dbInvoice.Token,
Address: strings.TrimSpace(dbInvoice.PaymentAddress),
TimeStarted: time.Now().Unix(),
Status: cms.PaymentStatusWatching,
Expand Down

0 comments on commit a3a16ab

Please # to comment.