github.com/decred/politeia@v1.4.0/politeiawww/legacy/cmsdatabase/database.go (about)

     1  // Copyright (c) 2018 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package cmsdatabase
     6  
     7  import (
     8  	"errors"
     9  
    10  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    11  	www "github.com/decred/politeia/politeiawww/api/www/v1"
    12  )
    13  
    14  var (
    15  	// ErrNoVersionRecord is emitted when no version record exists.
    16  	ErrNoVersionRecord = errors.New("no version record")
    17  
    18  	// ErrWrongVersion is emitted when the version record does not
    19  	// match the implementation version.
    20  	ErrWrongVersion = errors.New("wrong version")
    21  
    22  	// ErrShutdown is emitted when the cache is shutting down.
    23  	ErrShutdown = errors.New("cache is shutting down")
    24  
    25  	// ErrUserNotFound indicates that a user name was not found in the
    26  	// database.
    27  	ErrUserNotFound = errors.New("user not found")
    28  
    29  	// ErrInvoiceNotFound indicates that the invoice was not found in the
    30  	// database.
    31  	ErrInvoiceNotFound = errors.New("invoice not found")
    32  
    33  	// ErrExchangeRateNotFound indicates that an exchange rate for a given month/year was not found
    34  	ErrExchangeRateNotFound = errors.New("exchange rate not found")
    35  
    36  	// ErrDCCNotFound indicates that a DCC was not found from a given token
    37  	ErrDCCNotFound = errors.New("dcc not found")
    38  )
    39  
    40  // Database interface that is required by the web server.
    41  type Database interface {
    42  	// Invoice functions
    43  	NewInvoice(*Invoice) error    // Create new invoice
    44  	UpdateInvoice(*Invoice) error // Update existing invoice
    45  
    46  	RemoveInvoiceLineItems(string) error // Remove invoices line items
    47  
    48  	InvoicesByUserID(string) ([]Invoice, error)
    49  	InvoiceByToken(string) (*Invoice, error)                              // Return invoice given its token
    50  	InvoiceByTokenVersion(token string, version string) (*Invoice, error) // Return invoice by its token and version
    51  	InvoicesByAddress(string) ([]Invoice, error)                          // Return invoice by its address
    52  	InvoiceByKey(string) (*Invoice, error)                                // Return invoice given its key
    53  
    54  	InvoicesByMonthYearStatus(uint16, uint16, int) ([]Invoice, error) // Returns all invoices by month, year and status
    55  	InvoicesByMonthYear(uint16, uint16) ([]Invoice, error)            // Returns all invoice by month, year
    56  	InvoicesByStatus(int) ([]Invoice, error)                          // Returns all invoices by status
    57  	InvoicesAll() ([]Invoice, error)                                  // Returns all invoices
    58  	InvoicesByDateRangeStatus(int64, int64, int) ([]Invoice, error)   // Returns all paid invoice line items from range provided
    59  	InvoicesByDateRange(int64, int64) ([]Invoice, error)              // Returns all invoices from range provided
    60  	InvoicesByLineItemsProposalToken(string) ([]Invoice, error)       // Returns all Invoices with paid line item information based on proposal token.
    61  
    62  	// ExchangeRate functions
    63  	NewExchangeRate(*ExchangeRate) error          // Create new exchange rate
    64  	ExchangeRate(int, int) (*ExchangeRate, error) // Return an exchange rate based on month and year
    65  
    66  	// Update Payments
    67  	UpdatePayments(*Payments) error // Update existing payment information
    68  	PaymentsByAddress(string) (*Payments, error)
    69  	PaymentsByStatus(uint) ([]Payments, error)
    70  
    71  	// DCC
    72  	NewDCC(*DCC) error
    73  	UpdateDCC(*DCC) error
    74  
    75  	DCCByToken(string) (*DCC, error)
    76  	DCCsByStatus(int) ([]*DCC, error)
    77  	DCCsAll() ([]*DCC, error)
    78  
    79  	// Setup the invoice tables
    80  	Setup() error
    81  
    82  	// Build the relevant tables of cmsdb from scratch
    83  	Build([]Invoice, []DCC) error
    84  
    85  	// Close performs cleanup of the backend.
    86  	Close() error
    87  }
    88  
    89  // Invoice is the generic invoice type for invoices being added to or found
    90  // in the cmsdatabase.
    91  type Invoice struct {
    92  	Token              string
    93  	UserID             string
    94  	Username           string // Only populated when reading from the database
    95  	Month              uint
    96  	Year               uint
    97  	ExchangeRate       uint
    98  	Timestamp          int64
    99  	Status             cms.InvoiceStatusT
   100  	StatusChangeReason string
   101  	Files              []www.File
   102  	PublicKey          string
   103  	UserSignature      string
   104  	ServerSignature    string
   105  	Version            string // Version number of this invoice
   106  	ContractorName     string
   107  	ContractorLocation string
   108  	ContractorContact  string
   109  	ContractorRate     uint
   110  	PaymentAddress     string
   111  
   112  	LineItems []LineItem      // All line items parsed from the raw invoice provided.
   113  	Changes   []InvoiceChange // All status changes that the invoice has had.
   114  	Payments  Payments        // All payment information.
   115  }
   116  
   117  // LineItem contains information about the individual line items contained in an
   118  // invoice coming into or out of the cmsdatabase.
   119  type LineItem struct {
   120  	LineNumber     uint
   121  	InvoiceToken   string
   122  	Type           cms.LineItemTypeT
   123  	Domain         string
   124  	Subdomain      string
   125  	Description    string
   126  	ProposalURL    string
   127  	Labor          uint
   128  	Expenses       uint
   129  	ContractorRate uint
   130  	SubUserID      string
   131  }
   132  
   133  // InvoiceChange contains entries for any status update that occurs to a given
   134  // invoice.  This will give a full history of an invoices history.
   135  type InvoiceChange struct {
   136  	AdminPublicKey string
   137  	NewStatus      cms.InvoiceStatusT
   138  	Reason         string
   139  	Timestamp      int64
   140  }
   141  
   142  // ExchangeRate contains cached calculated rates for a given month/year
   143  type ExchangeRate struct {
   144  	Month        uint
   145  	Year         uint
   146  	ExchangeRate uint
   147  }
   148  
   149  // Payments contains information about each invoice's payments.
   150  type Payments struct {
   151  	InvoiceKey      string
   152  	InvoiceToken    string
   153  	Address         string
   154  	TxIDs           string
   155  	TimeStarted     int64
   156  	TimeLastUpdated int64
   157  	AmountNeeded    int64
   158  	AmountReceived  int64
   159  	Status          cms.PaymentStatusT
   160  }
   161  
   162  // DCC contains information about a DCC proposal for issuance or revocation.
   163  type DCC struct {
   164  	Token              string
   165  	SponsorUserID      string
   166  	NomineeUserID      string
   167  	Type               cms.DCCTypeT
   168  	Status             cms.DCCStatusT
   169  	Files              []www.File
   170  	StatusChangeReason string
   171  	TimeSubmitted      int64
   172  	TimeReviewed       int64
   173  	PublicKey          string
   174  	UserSignature      string
   175  	ServerSignature    string
   176  	SponsorStatement   string
   177  	Domain             cms.DomainTypeT
   178  	ContractorType     cms.ContractorTypeT
   179  
   180  	SupportUserIDs    string
   181  	OppositionUserIDs string
   182  }