diff --git a/storagenode/console/consoleheldamount/heldamountapi.go b/storagenode/console/consoleheldamount/heldamountapi.go index b3cdee0cb..bd7091acc 100644 --- a/storagenode/console/consoleheldamount/heldamountapi.go +++ b/storagenode/console/consoleheldamount/heldamountapi.go @@ -71,6 +71,29 @@ func (heldamount *HeldAmount) GetMonthlyHeldAmount(w http.ResponseWriter, r *htt heldamount.writeData(w, paystubData) } +// GetMonthlyPayment returns payment data from satellite for specific month. +func (heldamount *HeldAmount) GetMonthlyPayment(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + defer mon.Task()(&ctx)(nil) + + period := r.URL.Query().Get("period") + id := r.URL.Query().Get("satelliteID") + + satelliteID, err := storj.NodeIDFromString(id) + if err != nil { + heldamount.writeError(w, http.StatusBadRequest, Error.Wrap(err)) + return + } + + paymentData, err := heldamount.service.GetPayment(ctx, satelliteID, period) + if err != nil { + heldamount.writeError(w, http.StatusInternalServerError, Error.Wrap(err)) + return + } + + heldamount.writeData(w, paymentData) +} + // writeData is helper method to write JSON to http.ResponseWriter and log encoding error. func (heldamount *HeldAmount) writeData(w http.ResponseWriter, data interface{}) { w.Header().Set(contentType, applicationJSON) diff --git a/storagenode/console/consoleserver/server.go b/storagenode/console/consoleserver/server.go index 45db1ca16..a493496b0 100644 --- a/storagenode/console/consoleserver/server.go +++ b/storagenode/console/consoleserver/server.go @@ -90,7 +90,8 @@ func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifi notificationRouter.Handle("/list", http.HandlerFunc(notificationController.ListNotifications)).Methods(http.MethodGet) notificationRouter.Handle("/{id}/read", http.HandlerFunc(notificationController.ReadNotification)).Methods(http.MethodPost) notificationRouter.Handle("/readall", http.HandlerFunc(notificationController.ReadAllNotifications)).Methods(http.MethodPost) - heldamountRouter.Handle("/{period}", http.HandlerFunc(heldamountController.GetMonthlyHeldAmount)).Methods(http.MethodGet) + heldamountRouter.Handle("/paystub/{period}/{satelliteID}", http.HandlerFunc(heldamountController.GetMonthlyHeldAmount)).Methods(http.MethodGet) + heldamountRouter.Handle("/payment/{period}/{satelliteID}", http.HandlerFunc(heldamountController.GetMonthlyPayment)).Methods(http.MethodGet) server.server = http.Server{ Handler: router, diff --git a/storagenode/heldamount/service.go b/storagenode/heldamount/service.go index 6136c1fef..ddb50b53e 100644 --- a/storagenode/heldamount/service.go +++ b/storagenode/heldamount/service.go @@ -101,6 +101,37 @@ func (service *Service) GetPaystubStats(ctx context.Context, satelliteID storj.N }, nil } +// GetPayment retrieves payment data from particular satellite +func (service *Service) GetPayment(ctx context.Context, satelliteID storj.NodeID, period string) (_ *Payment, err error) { + defer mon.Task()(&ctx)(&err) + + client, err := service.dial(ctx, satelliteID) + if err != nil { + return nil, HeldAmountServiceErr.Wrap(err) + } + defer func() { err = errs.Combine(err, client.Close()) }() + + requestedPeriod, err := stringToTime(period) + if err != nil { + return nil, HeldAmountServiceErr.Wrap(err) + } + + resp, err := client.GetPayment(ctx, &pb.GetPaymentRequest{Period: requestedPeriod}) + if err != nil { + return nil, HeldAmountServiceErr.Wrap(err) + } + + return &Payment{ + ID: resp.Id, + Created: resp.CreatedAt, + SatelliteID: satelliteID, + Period: period, + Amount: resp.Amount, + Receipt: resp.Receipt, + Notes: resp.Notes, + }, nil +} + // dial dials the HeldAmount client for the satellite by id func (service *Service) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client, err error) { defer mon.Task()(&ctx)(&err)