Files
payments/internal/api/v3/handler_payment_initiations_list.go

65 lines
1.9 KiB
Go

package v3
import (
"net/http"
"github.com/hanzo-fi/go-libs/v5/pkg/storage/bun/paginate"
"github.com/hanzo-fi/go-libs/v5/pkg/transport/api"
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
"github.com/hanzo-fi/payments/internal/api/backend"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/hanzo-fi/payments/internal/otel"
"github.com/hanzo-fi/payments/internal/storage"
)
func paymentInitiationsList(backend backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "v3_paymentInitiationsList")
defer span.End()
query, err := paginate.Extract[storage.ListPaymentInitiationsQuery](r, func() (*storage.ListPaymentInitiationsQuery, error) {
options, err := getPagination(span, r, storage.PaymentInitiationQuery{})
if err != nil {
return nil, err
}
return pointer.For(storage.NewListPaymentInitiationsQuery(*options)), nil
})
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}
cursor, err := backend.PaymentInitiationsList(ctx, *query)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
pis := make([]models.PaymentInitiationExpanded, 0, len(cursor.Data))
for _, pi := range cursor.Data {
lastAdjustment, err := backend.PaymentInitiationAdjustmentsGetLast(ctx, pi.ID)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
pis = append(pis, models.PaymentInitiationExpanded{
PaymentInitiation: pi,
Status: lastAdjustment.Status,
Error: lastAdjustment.Error,
})
}
api.RenderCursor(w, paginate.Cursor[models.PaymentInitiationExpanded]{
PageSize: cursor.PageSize,
HasMore: cursor.HasMore,
Previous: cursor.Previous,
Next: cursor.Next,
Data: pis,
})
}
}