go/svc/invoice: refactor
We unify calculation logic, move the existing Invoice proto message into
InvoiceData, and create other messages/fields around it to hold
denormalized data.
diff --git a/go/svc/invoice/main.go b/go/svc/invoice/main.go
index 33bc125..11733c4 100644
--- a/go/svc/invoice/main.go
+++ b/go/svc/invoice/main.go
@@ -23,33 +23,33 @@
}
func (s *service) CreateInvoice(ctx context.Context, req *pb.CreateInvoiceRequest) (*pb.CreateInvoiceResponse, error) {
- if req.Invoice == nil {
- return nil, status.Error(codes.InvalidArgument, "invoice must be given")
+ if req.InvoiceData == nil {
+ return nil, status.Error(codes.InvalidArgument, "invoice data must be given")
}
- if len(req.Invoice.Item) < 1 {
- return nil, status.Error(codes.InvalidArgument, "invoice must contain at least one item")
+ if len(req.InvoiceData.Item) < 1 {
+ return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one item")
}
- for i, item := range req.Invoice.Item {
+ for i, item := range req.InvoiceData.Item {
if item.Title == "" {
- return nil, status.Errorf(codes.InvalidArgument, "invoice item %d must have title set", i)
+ return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have title set", i)
}
if item.Count == 0 || item.Count > 1000000 {
- return nil, status.Errorf(codes.InvalidArgument, "invoice item %d must have correct count", i)
+ return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct count", i)
}
if item.UnitPrice == 0 {
- return nil, status.Errorf(codes.InvalidArgument, "invoice item %d must have correct unit price", i)
+ return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct unit price", i)
}
if item.Vat > 100000 {
- return nil, status.Errorf(codes.InvalidArgument, "invoice item %d must have correct vat set", i)
+ return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct vat set", i)
}
}
- if len(req.Invoice.CustomerBilling) < 1 {
- return nil, status.Error(codes.InvalidArgument, "invoice must contain at least one line of the customer's billing address")
+ if len(req.InvoiceData.CustomerBilling) < 1 {
+ return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one line of the customer's billing address")
}
- if len(req.Invoice.InvoicerBilling) < 1 {
- return nil, status.Error(codes.InvalidArgument, "invoice must contain at least one line of the invoicer's billing address")
+ if len(req.InvoiceData.InvoicerBilling) < 1 {
+ return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one line of the invoicer's billing address")
}
- for i, c := range req.Invoice.InvoicerContact {
+ for i, c := range req.InvoiceData.InvoicerContact {
if c.Medium == "" {
return nil, status.Errorf(codes.InvalidArgument, "contact point %d must have medium set", i)
}
@@ -57,11 +57,11 @@
return nil, status.Errorf(codes.InvalidArgument, "contact point %d must have contact set", i)
}
}
- if req.Invoice.InvoicerVatId == "" {
- return nil, status.Error(codes.InvalidArgument, "invoice must contain invoicer's vat id")
+ if req.InvoiceData.InvoicerVatId == "" {
+ return nil, status.Error(codes.InvalidArgument, "invoice data must contain invoicer's vat id")
}
- uid, err := s.m.createInvoice(ctx, req.Invoice)
+ uid, err := s.m.createInvoice(ctx, req.InvoiceData)
if err != nil {
if _, ok := status.FromError(err); ok {
return nil, err
@@ -83,25 +83,9 @@
glog.Errorf("getInvoice(_, %q): %v", req.Uid, err)
return nil, status.Error(codes.Unavailable, "internal server error")
}
- sealedUid, err := s.m.getSealedUid(ctx, req.Uid)
- if err != nil {
- if _, ok := status.FromError(err); ok {
- return nil, err
- }
- glog.Errorf("getSealedUid(_, %q): %v", req.Uid, err)
- return nil, status.Error(codes.Unavailable, "internal server error")
- }
-
res := &pb.GetInvoiceResponse{
Invoice: invoice,
}
- if sealedUid == "" {
- res.State = pb.GetInvoiceResponse_STATE_PROFORMA
- } else {
- res.State = pb.GetInvoiceResponse_STATE_SEALED
- res.FinalUid = sealedUid
- }
-
return res, nil
}
@@ -131,7 +115,7 @@
return nil, err
}
- rendered, err = renderInvoicePDF(invoice, "xxxx", true)
+ rendered, err = renderInvoicePDF(invoice)
if err != nil {
return nil, err
}