-
Notifications
You must be signed in to change notification settings - Fork 30
/
billing_invoice.go
119 lines (104 loc) · 3.32 KB
/
billing_invoice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package v1beta1
import (
"context"
"github.com/raystack/frontier/billing/invoice"
"google.golang.org/protobuf/types/known/timestamppb"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
)
type InvoiceService interface {
List(ctx context.Context, filter invoice.Filter) ([]invoice.Invoice, error)
ListAll(ctx context.Context, filter invoice.Filter) ([]invoice.Invoice, error)
GetUpcoming(ctx context.Context, customerID string) (invoice.Invoice, error)
}
func (h Handler) ListAllInvoices(ctx context.Context, request *frontierv1beta1.ListAllInvoicesRequest) (*frontierv1beta1.ListAllInvoicesResponse, error) {
logger := grpczap.Extract(ctx)
invoices, err := h.invoiceService.ListAll(ctx, invoice.Filter{})
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
var invoicePBs []*frontierv1beta1.Invoice
for _, v := range invoices {
invoicePB, err := transformInvoiceToPB(v)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
invoicePBs = append(invoicePBs, invoicePB)
}
return &frontierv1beta1.ListAllInvoicesResponse{
Invoices: invoicePBs,
}, nil
}
func (h Handler) ListInvoices(ctx context.Context, request *frontierv1beta1.ListInvoicesRequest) (*frontierv1beta1.ListInvoicesResponse, error) {
logger := grpczap.Extract(ctx)
invoices, err := h.invoiceService.List(ctx, invoice.Filter{
CustomerID: request.GetBillingId(),
NonZeroOnly: request.GetNonzeroAmountOnly(),
})
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
var invoicePBs []*frontierv1beta1.Invoice
for _, v := range invoices {
invoicePB, err := transformInvoiceToPB(v)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
invoicePBs = append(invoicePBs, invoicePB)
}
return &frontierv1beta1.ListInvoicesResponse{
Invoices: invoicePBs,
}, nil
}
func (h Handler) GetUpcomingInvoice(ctx context.Context, request *frontierv1beta1.GetUpcomingInvoiceRequest) (*frontierv1beta1.GetUpcomingInvoiceResponse, error) {
logger := grpczap.Extract(ctx)
invoice, err := h.invoiceService.GetUpcoming(ctx, request.GetBillingId())
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
invoicePB, err := transformInvoiceToPB(invoice)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
return &frontierv1beta1.GetUpcomingInvoiceResponse{
Invoice: invoicePB,
}, nil
}
func transformInvoiceToPB(i invoice.Invoice) (*frontierv1beta1.Invoice, error) {
metaData, err := i.Metadata.ToStructPB()
if err != nil {
return &frontierv1beta1.Invoice{}, err
}
pb := &frontierv1beta1.Invoice{
Id: i.ID,
CustomerId: i.CustomerID,
ProviderId: i.ProviderID,
State: i.State,
Currency: i.Currency,
Amount: i.Amount,
HostedUrl: i.HostedURL,
Metadata: metaData,
}
if !i.DueAt.IsZero() {
pb.DueDate = timestamppb.New(i.DueAt)
}
if !i.EffectiveAt.IsZero() {
pb.EffectiveAt = timestamppb.New(i.EffectiveAt)
}
if !i.CreatedAt.IsZero() {
pb.CreatedAt = timestamppb.New(i.CreatedAt)
}
if !i.PeriodStartAt.IsZero() {
pb.PeriodStartAt = timestamppb.New(i.PeriodStartAt)
}
if !i.PeriodEndAt.IsZero() {
pb.PeriodEndAt = timestamppb.New(i.PeriodEndAt)
}
return pb, nil
}