Files
online-backend/internal/payment/paymenthistory/handler.go
GuwandykA 99788a853f
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
added payment history
2026-09-21 18:12:08 +05:00

99 lines
2.8 KiB
Go

package paymenthistory
import (
"net/http"
"online-back/internal/appresult"
"online-back/internal/handlers"
"online-back/internal/identity/rbac"
"online-back/internal/shared"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
)
const baseURL = "/payment-history"
type handler struct {
logger *logging.Logger
repository Repository
}
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
return &handler{logger: logger, repository: repository}
}
func (h *handler) Register(router *gin.RouterGroup) {
group := router.Group(baseURL, appresult.JwtTokenCheck())
group.GET("", h.list)
}
// list godoc
// @Summary List payment history
// @Description Returns a paginated, branch-scoped audit trail of visa/master/sber payout attempts (paid, failed, or pending), across orders
// @Tags payment-history
// @Produce json
// @Security BearerAuth
// @Param order_id query int false "Filter by order ID"
// @Param branch_id query int false "Filter by branch ID"
// @Param user_id query int false "Filter by order owner (client) user ID"
// @Param status query string false "Filter by gateway status (pending, success, failed)"
// @Param search query string false "Filter by passport name/surname or order unique ID"
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Success 200 {object} ListPaymentHistoryDTO
// @Failure 400 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /payment-history [get]
func (h *handler) list(c *gin.Context) {
filter, ok := parseListFilter(c)
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
result, err := h.repository.List(c.Request.Context(), filter)
if err != nil {
h.logger.Errorf("list payment history failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
func parseListFilter(c *gin.Context) (ListFilter, bool) {
page, limit, ok := shared.ParsePageLimit(c)
if !ok {
return ListFilter{}, false
}
filter := ListFilter{Page: page, Limit: limit}
if id, ok := shared.OptionalQueryID(c, "order_id", "orderId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.OrderId = id
}
if id, ok := shared.OptionalQueryID(c, "branch_id", "branchId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.BranchId = id
}
if id, ok := shared.OptionalQueryID(c, "user_id", "userId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.UserId = id
}
if raw := shared.QueryValue(c, "status"); raw != "" {
filter.Status = &raw
}
filter.Search = shared.QueryValue(c, "search", "q")
rbac.BindBranchIDs(c, &filter.BranchIds)
return filter, true
}