github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/setinvoicestatus.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 main
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"strconv"
    11  	"strings"
    12  
    13  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    14  	"github.com/decred/politeia/politeiawww/cmd/shared"
    15  )
    16  
    17  type SetInvoiceStatusCmd struct {
    18  	Args struct {
    19  		Version string `positional-arg-name:"version"`
    20  		Token   string `positional-arg-name:"token"`
    21  		Status  string `positional-arg-name:"status"`
    22  		Reason  string `positional-arg-name:"reason"`
    23  	} `positional-args:"true" optional:"true"`
    24  }
    25  
    26  func (cmd *SetInvoiceStatusCmd) Execute(args []string) error {
    27  	InvoiceStatus := map[string]cms.InvoiceStatusT{
    28  		"rejected": cms.InvoiceStatusRejected,
    29  		"approved": cms.InvoiceStatusApproved,
    30  		"disputed": cms.InvoiceStatusDisputed,
    31  		"paid":     cms.InvoiceStatusPaid,
    32  	}
    33  	// Check for user identity
    34  	if cfg.Identity == nil {
    35  		return shared.ErrUserIdentityNotFound
    36  	}
    37  
    38  	// Parse the invoice status. This can be either the numeric status
    39  	// code or the human readable equivalent.
    40  	var status cms.InvoiceStatusT
    41  	s, err := strconv.ParseUint(cmd.Args.Status, 10, 32)
    42  	if err == nil {
    43  		// Numeric status code found
    44  		status = cms.InvoiceStatusT(s)
    45  	} else if s, ok := InvoiceStatus[strings.ToLower(cmd.Args.Status)]; ok {
    46  		// Human readable status code found
    47  		status = s
    48  	} else {
    49  		return fmt.Errorf("Invalid status: '%v'.  "+
    50  			"Valid statuses are:\n"+
    51  			"  rejected  reject the invoice\n"+
    52  			"  approved  approve the invoice\n"+
    53  			"  disputed  mark the invoice as disputed\n"+
    54  			"  paid      mark the invoice as paid\n",
    55  			cmd.Args.Status)
    56  	}
    57  
    58  	// Setup request
    59  	sig := cfg.Identity.SignMessage([]byte(cmd.Args.Token + cmd.Args.Version +
    60  		strconv.Itoa(int(status)) + cmd.Args.Reason))
    61  
    62  	sis := &cms.SetInvoiceStatus{
    63  		Token:     cmd.Args.Token,
    64  		Status:    status,
    65  		Reason:    cmd.Args.Reason,
    66  		PublicKey: hex.EncodeToString(cfg.Identity.Public.Key[:]),
    67  		Signature: hex.EncodeToString(sig[:]),
    68  	}
    69  
    70  	// Print request details
    71  	err = shared.PrintJSON(sis)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	// Send request
    77  	sisr, err := client.SetInvoiceStatus(sis)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return shared.PrintJSON(sisr)
    83  }
    84  
    85  // setInvoiceStatusHelpMsg is the output of the help command when
    86  // "setinvoicestatus" is specified.
    87  const setInvoiceStatusHelpMsg = `setinvoicestatus "token" "status"
    88  
    89  Set the status of a invoice. Requires admin privileges.
    90  
    91  Arguments:
    92  1. version    (string, required)   Current version of the invoice record
    93  1. token      (string, required)   Invoice censorship token
    94  2. status     (string, required)   New status (approved, disputed, rejected)
    95  3. message    (string)             Status change message
    96  
    97  Request:
    98  {
    99    "token":           (string)          Censorship token
   100    "invoicestatus":   (InvoiceStatusT)  Invoice status code    
   101    "signature":       (string)          Signature of invoice status change
   102    "publickey":       (string)          Public key of user changing invoice status
   103  }
   104  
   105  Response:
   106  {
   107    "invoice": {
   108  	  "month":         (uint16)       Month of invoice
   109  	  "year":          (uint16)       Year of invoice
   110  	  "state":         (PropStateT)   Current state of invoice
   111  	  "status":        (PropStatusT)  Current status of invoice
   112  	  "timestamp":     (int64)        Timestamp of last update of invoice
   113  	  "userid":        (string)       ID of user who submitted invoice
   114  	  "username":      (string)       Username of user who submitted invoice
   115  	  "publickey":     (string)       Public key used to sign invoice
   116  	  "signature":     (string)       Signature of merkle root
   117  	  "files": [
   118  		{
   119  		  "name":      (string)       Filename 
   120  		  "mime":      (string)       Mime type 
   121  		  "digest":    (string)       File digest 
   122  		  "payload":   (string)       File payload 
   123  		}
   124  	  ],
   125  	  "numcomments":   (uint)    Number of comments on the invoice
   126  	  "version": 		 (string)  Version of invoice
   127  	  "censorshiprecord": {	
   128  		"token":       (string)  Censorship token
   129  		"merkle":      (string)  Merkle root of invoice
   130  		"signature":   (string)  Server side signature of []byte(Merkle+Token)
   131  	  }
   132  	}
   133  }`