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

     1  // Copyright (c) 2017-2019 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 cockroachdb
     6  
     7  import (
     8  	"strconv"
     9  	"time"
    10  
    11  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    12  	www "github.com/decred/politeia/politeiawww/api/www/v1"
    13  	database "github.com/decred/politeia/politeiawww/legacy/cmsdatabase"
    14  )
    15  
    16  // EncodeInvoice encodes a generic database.Invoice instance into a cockroachdb
    17  // Invoice.
    18  func EncodeInvoice(dbInvoice *database.Invoice) *Invoice {
    19  	invoice := Invoice{}
    20  
    21  	invoice.Key = dbInvoice.Token + dbInvoice.Version
    22  	invoice.Token = dbInvoice.Token
    23  	invoice.UserID = dbInvoice.UserID
    24  	invoice.Month = dbInvoice.Month
    25  	invoice.Year = dbInvoice.Year
    26  	invoice.ExchangeRate = dbInvoice.ExchangeRate
    27  	invoice.Status = uint(dbInvoice.Status)
    28  	invoice.StatusChangeReason = dbInvoice.StatusChangeReason
    29  	invoice.Timestamp = time.Unix(dbInvoice.Timestamp, 0)
    30  
    31  	files := make([]www.File, len(dbInvoice.Files))
    32  	for i := 0; i < len(dbInvoice.Files); i++ {
    33  		file := www.File{
    34  			Payload: dbInvoice.Files[i].Payload,
    35  			MIME:    dbInvoice.Files[i].MIME,
    36  			Digest:  dbInvoice.Files[i].Digest,
    37  		}
    38  		files[i] = file
    39  	}
    40  	invoice.PublicKey = dbInvoice.PublicKey
    41  	invoice.UserSignature = dbInvoice.UserSignature
    42  	invoice.ServerSignature = dbInvoice.ServerSignature
    43  	invoice.Version = dbInvoice.Version
    44  	invoice.ContractorName = dbInvoice.ContractorName
    45  	invoice.ContractorLocation = dbInvoice.ContractorLocation
    46  	invoice.PaymentAddress = dbInvoice.PaymentAddress
    47  	invoice.ContractorContact = dbInvoice.ContractorContact
    48  	invoice.ContractorRate = dbInvoice.ContractorRate
    49  
    50  	for i, dbInvoiceLineItem := range dbInvoice.LineItems {
    51  		invoiceLineItem := EncodeInvoiceLineItem(&dbInvoiceLineItem)
    52  		invoiceLineItem.LineItemKey = dbInvoice.Token + dbInvoice.Version + strconv.Itoa(i)
    53  		invoiceLineItem.InvoiceToken = dbInvoice.Token
    54  		invoice.LineItems = append(invoice.LineItems, invoiceLineItem)
    55  	}
    56  
    57  	for _, dbInvoiceChange := range dbInvoice.Changes {
    58  		invoiceChange := encodeInvoiceChange(&dbInvoiceChange)
    59  		invoiceChange.InvoiceToken = dbInvoice.Token
    60  		invoice.Changes = append(invoice.Changes, invoiceChange)
    61  	}
    62  
    63  	invoice.Payments = encodePayments(&dbInvoice.Payments)
    64  	return &invoice
    65  }
    66  
    67  // DecodeInvoice decodes a cockroachdb Invoice instance into a generic database.Invoice.
    68  func DecodeInvoice(invoice *Invoice) (*database.Invoice, error) {
    69  	dbInvoice := database.Invoice{}
    70  
    71  	dbInvoice.Token = invoice.Token
    72  	dbInvoice.UserID = invoice.UserID
    73  	dbInvoice.Username = invoice.Username
    74  	dbInvoice.Month = invoice.Month
    75  	dbInvoice.Year = invoice.Year
    76  	dbInvoice.ExchangeRate = invoice.ExchangeRate
    77  	dbInvoice.Status = cms.InvoiceStatusT(invoice.Status)
    78  	dbInvoice.StatusChangeReason = invoice.StatusChangeReason
    79  	dbInvoice.Timestamp = invoice.Timestamp.Unix()
    80  	dbInvoice.PublicKey = invoice.PublicKey
    81  	dbInvoice.UserSignature = invoice.UserSignature
    82  	dbInvoice.ServerSignature = invoice.ServerSignature
    83  	dbInvoice.Version = invoice.Version
    84  	dbInvoice.ContractorName = invoice.ContractorName
    85  	dbInvoice.ContractorLocation = invoice.ContractorLocation
    86  	dbInvoice.PaymentAddress = invoice.PaymentAddress
    87  	dbInvoice.ContractorContact = invoice.ContractorContact
    88  	dbInvoice.ContractorRate = invoice.ContractorRate
    89  
    90  	for _, invoiceLineItem := range invoice.LineItems {
    91  		dbInvoiceLineItem := DecodeInvoiceLineItem(&invoiceLineItem)
    92  		dbInvoice.LineItems = append(dbInvoice.LineItems, *dbInvoiceLineItem)
    93  	}
    94  
    95  	for _, invoiceChange := range invoice.Changes {
    96  		dbInvoiceChanges := decodeInvoiceChange(&invoiceChange)
    97  		dbInvoice.Changes = append(dbInvoice.Changes, *dbInvoiceChanges)
    98  	}
    99  
   100  	dbInvoice.Payments = decodePayment(&invoice.Payments)
   101  	return &dbInvoice, nil
   102  }
   103  
   104  // EncodeInvoiceLineItem encodes a database.LineItem into a cockroachdb line item.
   105  func EncodeInvoiceLineItem(dbLineItem *database.LineItem) LineItem {
   106  	lineItem := LineItem{}
   107  	lineItem.InvoiceToken = dbLineItem.InvoiceToken
   108  	lineItem.Type = uint(dbLineItem.Type)
   109  	lineItem.Domain = dbLineItem.Domain
   110  	lineItem.Subdomain = dbLineItem.Subdomain
   111  	lineItem.Description = dbLineItem.Description
   112  	lineItem.ProposalURL = dbLineItem.ProposalURL
   113  	lineItem.Labor = dbLineItem.Labor
   114  	lineItem.Expenses = dbLineItem.Expenses
   115  	lineItem.ContractorRate = dbLineItem.ContractorRate
   116  	lineItem.SubUserID = dbLineItem.SubUserID
   117  	return lineItem
   118  }
   119  
   120  // DecodeInvoiceLineItem decodes a cockroachdb line item into a generic database.LineItem
   121  func DecodeInvoiceLineItem(lineItem *LineItem) *database.LineItem {
   122  	dbLineItem := &database.LineItem{}
   123  	dbLineItem.InvoiceToken = lineItem.InvoiceToken
   124  	dbLineItem.Type = cms.LineItemTypeT(lineItem.Type)
   125  	dbLineItem.Domain = lineItem.Domain
   126  	dbLineItem.Subdomain = lineItem.Subdomain
   127  	dbLineItem.Description = lineItem.Description
   128  	dbLineItem.ProposalURL = lineItem.ProposalURL
   129  	dbLineItem.Labor = lineItem.Labor
   130  	dbLineItem.Expenses = lineItem.Expenses
   131  	dbLineItem.ContractorRate = lineItem.ContractorRate
   132  	dbLineItem.SubUserID = lineItem.SubUserID
   133  
   134  	return dbLineItem
   135  }
   136  
   137  func encodeInvoiceChange(dbInvoiceChange *database.InvoiceChange) InvoiceChange {
   138  	invoiceChange := InvoiceChange{}
   139  	invoiceChange.AdminPublicKey = dbInvoiceChange.AdminPublicKey
   140  	invoiceChange.NewStatus = uint(dbInvoiceChange.NewStatus)
   141  	invoiceChange.Reason = dbInvoiceChange.Reason
   142  	invoiceChange.Timestamp = time.Unix(dbInvoiceChange.Timestamp, 0)
   143  	return invoiceChange
   144  }
   145  
   146  func decodeInvoiceChange(invoiceChange *InvoiceChange) *database.InvoiceChange {
   147  	dbInvoiceChange := &database.InvoiceChange{}
   148  	dbInvoiceChange.AdminPublicKey = invoiceChange.AdminPublicKey
   149  	dbInvoiceChange.NewStatus = cms.InvoiceStatusT(invoiceChange.NewStatus)
   150  	dbInvoiceChange.Reason = invoiceChange.Reason
   151  	dbInvoiceChange.Timestamp = invoiceChange.Timestamp.Unix()
   152  	return dbInvoiceChange
   153  }
   154  
   155  // DecodeInvoices decodes an array of cockroachdb Invoice instances into
   156  // generic database.Invoices.
   157  func DecodeInvoices(invoices []Invoice) ([]database.Invoice, error) {
   158  	dbInvoices := make([]database.Invoice, 0, len(invoices))
   159  
   160  	for _, invoice := range invoices {
   161  		dbInvoice, err := DecodeInvoice(&invoice)
   162  		if err != nil {
   163  			return nil, err
   164  		}
   165  
   166  		dbInvoices = append(dbInvoices, *dbInvoice)
   167  	}
   168  
   169  	return dbInvoices, nil
   170  }
   171  
   172  func encodeExchangeRate(dbExchangeRate *database.ExchangeRate) ExchangeRate {
   173  	exchangeRate := ExchangeRate{}
   174  	exchangeRate.Month = dbExchangeRate.Month
   175  	exchangeRate.Year = dbExchangeRate.Year
   176  	exchangeRate.ExchangeRate = dbExchangeRate.ExchangeRate
   177  	return exchangeRate
   178  }
   179  
   180  func decodeExchangeRate(exchangeRate ExchangeRate) *database.ExchangeRate {
   181  	dbExchangeRate := &database.ExchangeRate{}
   182  	dbExchangeRate.Month = exchangeRate.Month
   183  	dbExchangeRate.Year = exchangeRate.Year
   184  	dbExchangeRate.ExchangeRate = exchangeRate.ExchangeRate
   185  	return dbExchangeRate
   186  }
   187  
   188  func encodePayments(dbPayments *database.Payments) Payments {
   189  	payments := Payments{}
   190  	payments.InvoiceKey = dbPayments.InvoiceKey
   191  	payments.InvoiceToken = dbPayments.InvoiceToken
   192  	payments.Address = dbPayments.Address
   193  	payments.TxIDs = dbPayments.TxIDs
   194  	payments.TimeStarted = dbPayments.TimeStarted
   195  	payments.TimeLastUpdated = dbPayments.TimeLastUpdated
   196  	payments.AmountNeeded = dbPayments.AmountNeeded
   197  	payments.AmountReceived = dbPayments.AmountReceived
   198  	payments.Status = uint(dbPayments.Status)
   199  	return payments
   200  }
   201  
   202  func decodePayment(payments *Payments) database.Payments {
   203  	dbPayments := database.Payments{}
   204  	dbPayments.InvoiceKey = payments.InvoiceKey
   205  	dbPayments.InvoiceToken = payments.InvoiceToken
   206  	dbPayments.Address = payments.Address
   207  	dbPayments.TxIDs = payments.TxIDs
   208  	dbPayments.TimeStarted = payments.TimeStarted
   209  	dbPayments.TimeLastUpdated = payments.TimeLastUpdated
   210  	dbPayments.AmountNeeded = payments.AmountNeeded
   211  	dbPayments.AmountReceived = payments.AmountReceived
   212  	dbPayments.Status = cms.PaymentStatusT(payments.Status)
   213  	return dbPayments
   214  }
   215  
   216  func encodeDCC(dbDCC *database.DCC) *DCC {
   217  	dcc := DCC{
   218  		Token:              dbDCC.Token,
   219  		SponsorUserID:      dbDCC.SponsorUserID,
   220  		NomineeUserID:      dbDCC.NomineeUserID,
   221  		Type:               int(dbDCC.Type),
   222  		Status:             int(dbDCC.Status),
   223  		StatusChangeReason: dbDCC.StatusChangeReason,
   224  		TimeSubmitted:      dbDCC.TimeSubmitted,
   225  		TimeReviewed:       dbDCC.TimeReviewed,
   226  		PublicKey:          dbDCC.PublicKey,
   227  		UserSignature:      dbDCC.UserSignature,
   228  		ServerSignature:    dbDCC.ServerSignature,
   229  		SponsorStatement:   dbDCC.SponsorStatement,
   230  		Domain:             int(dbDCC.Domain),
   231  		ContractorType:     int(dbDCC.ContractorType),
   232  
   233  		SupportUserIDs:    dbDCC.SupportUserIDs,
   234  		OppositionUserIDs: dbDCC.OppositionUserIDs,
   235  	}
   236  	return &dcc
   237  }
   238  
   239  func decodeDCC(dcc *DCC) *database.DCC {
   240  	dbDCC := database.DCC{
   241  		Token:              dcc.Token,
   242  		SponsorUserID:      dcc.SponsorUserID,
   243  		NomineeUserID:      dcc.NomineeUserID,
   244  		Type:               cms.DCCTypeT(dcc.Type),
   245  		Status:             cms.DCCStatusT(dcc.Status),
   246  		StatusChangeReason: dcc.StatusChangeReason,
   247  		TimeSubmitted:      dcc.TimeSubmitted,
   248  		TimeReviewed:       dcc.TimeReviewed,
   249  		PublicKey:          dcc.PublicKey,
   250  		UserSignature:      dcc.UserSignature,
   251  		ServerSignature:    dcc.ServerSignature,
   252  		SponsorStatement:   dcc.SponsorStatement,
   253  		Domain:             cms.DomainTypeT(dcc.Domain),
   254  		ContractorType:     cms.ContractorTypeT(dcc.ContractorType),
   255  
   256  		SupportUserIDs:    dcc.SupportUserIDs,
   257  		OppositionUserIDs: dcc.OppositionUserIDs,
   258  	}
   259  	return &dbDCC
   260  }
   261  
   262  func convertMatchingLineItemToInvoices(matching []MatchingLineItems) []database.Invoice {
   263  	// Each invoice added will include just 1 line item.
   264  	dbInvoices := make([]database.Invoice, 0, len(matching))
   265  	for _, vv := range matching {
   266  		li := make([]database.LineItem, 1)
   267  		li[0] = database.LineItem{
   268  			Type:           cms.LineItemTypeT(vv.Type),
   269  			Domain:         vv.Domain,
   270  			Subdomain:      vv.Subdomain,
   271  			Description:    vv.Description,
   272  			Labor:          vv.Labor,
   273  			Expenses:       vv.Expenses,
   274  			ProposalURL:    vv.ProposalURL,
   275  			ContractorRate: vv.SubRate,
   276  			SubUserID:      vv.SubUser,
   277  		}
   278  		inv := database.Invoice{
   279  			PublicKey:      vv.PublicKey,
   280  			Token:          vv.InvoiceToken,
   281  			Month:          vv.Month,
   282  			Year:           vv.Year,
   283  			UserID:         vv.UserID,
   284  			LineItems:      li,
   285  			ContractorRate: vv.ContractorRate,
   286  			ExchangeRate:   vv.ExchangeRate,
   287  		}
   288  		dbInvoices = append(dbInvoices, inv)
   289  	}
   290  	return dbInvoices
   291  }