Skip to content

Commit

Permalink
add db line item approval
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlyp committed Aug 12, 2020
1 parent bd07a20 commit b4ab4e3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions politeiawww/api/cms/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ type LineItemsInput struct {
SubRate uint `json:"subrate"` // The payrate of the subcontractor
Labor uint `json:"labor"` // Number of minutes (if labor)
Expenses uint `json:"expenses"` // Total cost (in USD cents) of line item (if expense or misc)
Approved bool `json:"approved"` // Proposal owner approved this line item (if proposal token specified)
}

// PolicyReply returns the various policy information while in CMS mode.
Expand Down
4 changes: 3 additions & 1 deletion politeiawww/cmsdatabase/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ func (c *cockroachdb) InvoicesByLineItemsProposalToken(token string) ([]database
line_items.proposal_url,
line_items.labor,
line_items.expenses,
line_items.contractor_rate AS sub_rate
line_items.contractor_rate AS sub_rate,
line_items.approved
FROM invoices
INNER JOIN line_items
ON invoices.token = line_items.invoice_token
Expand Down Expand Up @@ -648,6 +649,7 @@ type MatchingLineItems struct {
PublicKey string
ExchangeRate uint
SubRate uint
Approved bool
}

// Close satisfies the database interface.
Expand Down
3 changes: 3 additions & 0 deletions politeiawww/cmsdatabase/cockroachdb/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func EncodeInvoiceLineItem(dbLineItem *database.LineItem) LineItem {
lineItem.Expenses = dbLineItem.Expenses
lineItem.ContractorRate = dbLineItem.ContractorRate
lineItem.SubUserID = dbLineItem.SubUserID
lineItem.Approved = dbLineItem.Approved
return lineItem
}

Expand All @@ -128,6 +129,7 @@ func DecodeInvoiceLineItem(lineItem *LineItem) *database.LineItem {
dbLineItem.Expenses = lineItem.Expenses
dbLineItem.ContractorRate = lineItem.ContractorRate
dbLineItem.SubUserID = lineItem.SubUserID
dbLineItem.Approved = lineItem.Approved

return dbLineItem
}
Expand Down Expand Up @@ -267,6 +269,7 @@ func convertMatchingLineItemToInvoices(matching []MatchingLineItems) []database.
Expenses: vv.Expenses,
ProposalURL: vv.ProposalURL,
ContractorRate: vv.SubRate,
Approved: vv.Approved,
}
inv := database.Invoice{
PublicKey: vv.PublicKey,
Expand Down
1 change: 1 addition & 0 deletions politeiawww/cmsdatabase/cockroachdb/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type LineItem struct {
Expenses uint `gorm:"not null"` // Total cost of line item (in USD cents)
ContractorRate uint `gorm:"not null"` // Optional contractor rate for line item, typically used for Sub Contractors
SubUserID string `gorm:"not null"` // SubContractor User ID if Subcontractor Line Item
Approved bool `gorm:"not null"` // Proposal owner approved line item
}

// TableName returns the table name of the line items table.
Expand Down
1 change: 1 addition & 0 deletions politeiawww/cmsdatabase/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type LineItem struct {
Expenses uint
ContractorRate uint
SubUserID string
Approved bool
}

// InvoiceChange contains entries for any status update that occurs to a given
Expand Down
19 changes: 17 additions & 2 deletions politeiawww/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ func convertDatabaseInvoiceToInvoiceRecord(dbInvoice cmsdatabase.Invoice) (cms.I
Expenses: dbLineItem.Expenses,
SubRate: dbLineItem.ContractorRate,
SubUserID: dbLineItem.SubUserID,
Approved: dbLineItem.Approved,
}
invInputLineItems = append(invInputLineItems, lineItem)
}
Expand Down Expand Up @@ -924,6 +925,7 @@ func convertInvoiceRecordToDatabaseInvoice(invRec *cms.InvoiceRecord) *cmsdataba
Expenses: lineItem.Expenses,
ContractorRate: lineItem.SubRate,
SubUserID: lineItem.SubUserID,
Approved: lineItem.Approved,
}
dbInvoice.LineItems = append(dbInvoice.LineItems, dbLineItem)
}
Expand All @@ -944,6 +946,7 @@ func convertLineItemsToDatabase(token string, l []cms.LineItemsInput) []cmsdatab
Expenses: v.Expenses,
// If subrate is populated, use the existing contractor rate field.
ContractorRate: v.SubRate,
Approved: v.Approved,
})
}
return dl
Expand All @@ -960,6 +963,7 @@ func convertDatabaseToLineItems(dl []cmsdatabase.LineItem) []cms.LineItemsInput
ProposalToken: v.ProposalURL,
Labor: v.Labor,
Expenses: v.Expenses,
Approved: v.Approved,
})
}
return l
Expand All @@ -973,6 +977,7 @@ func convertRecordToDatabaseInvoice(p pd.Record) (*cmsdatabase.Invoice, error) {
Version: p.Version,
}

var approvedProposals []string
// Decode invoice file
for _, v := range p.Files {
if v.Name == invoiceFile {
Expand Down Expand Up @@ -1076,19 +1081,19 @@ func convertRecordToDatabaseInvoice(p pd.Record) (*cmsdatabase.Invoice, error) {
p.CensorshipRecord.Token, err, m)
continue
}
var approvedProposals []string
for _, s := range ipa {
// Check to see if the approved invoice version doesn't match
// current invoice version. If so, the proposal owner needs to
// approve again.
if s.InvoiceVersion != r.Version {
if s.InvoiceVersion != p.Version {
continue
}
if approvedProposals == nil {
approvedProposals = make([]string, 0, 1048)
}
approvedProposals = append(approvedProposals, s.Token)
}

default:
// Log error but proceed
log.Errorf("initializeInventory: invalid "+
Expand All @@ -1097,6 +1102,15 @@ func convertRecordToDatabaseInvoice(p pd.Record) (*cmsdatabase.Invoice, error) {
}
}

// Set all line items to approved based on metadata
for _, approvedProposal := range approvedProposals {
for i := range dbInvoice.LineItems {
if dbInvoice.LineItems[i].ProposalURL == approvedProposal {
dbInvoice.LineItems[i].Approved = true
}
}
}

return &dbInvoice, nil
}

Expand Down Expand Up @@ -1451,6 +1465,7 @@ func convertDatabaseInvoiceToProposalLineItems(inv cmsdatabase.Invoice) cms.Prop
Labor: inv.LineItems[0].Labor,
Expenses: inv.LineItems[0].Expenses,
SubRate: inv.LineItems[0].ContractorRate,
Approved: inv.LineItems[0].Approved,
},
}
}
Expand Down
8 changes: 7 additions & 1 deletion politeiawww/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ func (p *politeiawww) processProposalInvoiceApprove(poa cms.ProposalOwnerApprove
return nil, err
}
proposalFound := false
for _, lineItem := range invRec.Input.LineItems {
for i, lineItem := range invRec.Input.LineItems {
// If the proposal token is empty then don't display it for the
// non invoice owner or admin.
if lineItem.ProposalToken == "" {
Expand All @@ -2139,6 +2139,7 @@ func (p *politeiawww) processProposalInvoiceApprove(poa cms.ProposalOwnerApprove
// list of line items to display.
if stringInSlice(cmsUser.ProposalsOwned, lineItem.ProposalToken) {
proposalFound = true
invRec.Input.LineItems[i].Approved = true
}
}
if !proposalFound {
Expand Down Expand Up @@ -2208,5 +2209,10 @@ func (p *politeiawww) processProposalInvoiceApprove(poa cms.ProposalOwnerApprove
return nil, err
}

// Update Invoice in db so line items are approved
err = p.cmsDB.UpdateInvoice(convertInvoiceRecordToDatabaseInvoice(invRec))
if err != nil {
return nil, err
}
return &cms.ProposalOwnerApproveReply{}, nil
}

0 comments on commit b4ab4e3

Please sign in to comment.