From ac94e0dd8a2e9a44c0d358a43089265a8e4855dd Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Mon, 21 Sep 2026 19:52:10 +0500 Subject: [PATCH] add /order-id --- docs/docs.go | 58 +++++++++++++++++++ docs/swagger.json | 58 +++++++++++++++++++ docs/swagger.yaml | 39 +++++++++++++ .../payment/paymenthistory/db/postgresql.go | 42 ++++++++++++++ internal/payment/paymenthistory/dto.go | 15 +++++ internal/payment/paymenthistory/handler.go | 57 ++++++++++++++++++ internal/payment/paymenthistory/storage.go | 5 ++ 7 files changed, 274 insertions(+) diff --git a/docs/docs.go b/docs/docs.go index b8d17ce..d3990c6 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4980,6 +4980,64 @@ const docTemplate = `{ } } }, + "/payment-history/{id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Returns the full step-by-step audit trail (visa_master_payment_logs) recorded while processing an order's payout - evidence of what actually happened, independent of the summary rows returned by the list endpoint", + "produces": [ + "application/json" + ], + "tags": [ + "payment-history" + ], + "summary": "Get payment logs for an order", + "parameters": [ + { + "type": "integer", + "description": "Order ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/appresult.AppSuccess" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + } + } + } + }, "/payment-settings": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index 87b4d7e..6ddf331 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4974,6 +4974,64 @@ } } }, + "/payment-history/{id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Returns the full step-by-step audit trail (visa_master_payment_logs) recorded while processing an order's payout - evidence of what actually happened, independent of the summary rows returned by the list endpoint", + "produces": [ + "application/json" + ], + "tags": [ + "payment-history" + ], + "summary": "Get payment logs for an order", + "parameters": [ + { + "type": "integer", + "description": "Order ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/appresult.AppSuccess" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + } + } + } + }, "/payment-settings": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 965eacf..129be1d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4999,6 +4999,45 @@ paths: summary: List payment history tags: - payment-history + /payment-history/{id}: + get: + description: Returns the full step-by-step audit trail (visa_master_payment_logs) + recorded while processing an order's payout - evidence of what actually happened, + independent of the summary rows returned by the list endpoint + parameters: + - description: Order ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/appresult.AppSuccess' + "400": + description: Bad Request + schema: + $ref: '#/definitions/appresult.AppError' + "403": + description: Forbidden + schema: + $ref: '#/definitions/appresult.AppError' + "404": + description: Not Found + schema: + $ref: '#/definitions/appresult.AppError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/appresult.AppError' + security: + - BearerAuth: [] + summary: Get payment logs for an order + tags: + - payment-history /payment-settings: get: description: Returns the admin-configurable bank and GBÜS commission fees used diff --git a/internal/payment/paymenthistory/db/postgresql.go b/internal/payment/paymenthistory/db/postgresql.go index 03fbc68..b342e99 100644 --- a/internal/payment/paymenthistory/db/postgresql.go +++ b/internal/payment/paymenthistory/db/postgresql.go @@ -75,6 +75,48 @@ func (r *repository) List(ctx context.Context, filter paymenthistory.ListFilter) return paymenthistory.ListPaymentHistoryDTO{Count: count, Items: items}, nil } +// OwnerInfo returns orderID's owning user/branch, so the handler can run +// rbac.GuardOrder before returning its logs to a branch/own-scoped caller. +func (r *repository) OwnerInfo(ctx context.Context, orderID int64) (*int64, int64, error) { + var userID *int64 + var branchID int64 + err := r.client.QueryRow(ctx, + `SELECT user_id, branch_id FROM visa_master_payment_orders WHERE id = $1 AND deleted_at IS NULL`, orderID, + ).Scan(&userID, &branchID) + return userID, branchID, err +} + +func (r *repository) Logs(ctx context.Context, orderID int64) ([]paymenthistory.PaymentLogDTO, error) { + rows, err := r.client.Query(ctx, + `SELECT l.id, l.order_id, l.step, l.detail, l.created_by, `+shared.UserDisplaySQL+`, l.created_at + FROM visa_master_payment_logs l + LEFT JOIN users u ON u.id = l.created_by + WHERE l.order_id = $1 + ORDER BY l.id ASC`, + orderID, + ) + if err != nil { + return nil, err + } + defer rows.Close() + + items := []paymenthistory.PaymentLogDTO{} + for rows.Next() { + var item paymenthistory.PaymentLogDTO + if err := rows.Scan( + &item.Id, &item.OrderId, &item.Step, &item.Detail, &item.CreatedById, &item.CreatedByName, &item.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, item) + } + if err := rows.Err(); err != nil { + return nil, err + } + + return items, nil +} + func buildListQuery(filter paymenthistory.ListFilter) (whereClause string, args []interface{}) { conditions := []string{"p.deleted_at IS NULL", "o.deleted_at IS NULL"} diff --git a/internal/payment/paymenthistory/dto.go b/internal/payment/paymenthistory/dto.go index 30c65c8..dc1c031 100644 --- a/internal/payment/paymenthistory/dto.go +++ b/internal/payment/paymenthistory/dto.go @@ -7,6 +7,7 @@ package paymenthistory import ( + "encoding/json" "time" "online-back/internal/shared" @@ -67,3 +68,17 @@ type ListPaymentHistoryDTO struct { Count int64 `json:"count"` Items []PaymentHistoryItemDTO `json:"items"` } + +// PaymentLogDTO is one step-by-step entry from visa_master_payment_logs - +// the granular audit trail recorded while processing an order's payout +// (see visamasterpay's logStep), independent of and more detailed than +// the summary rows in visa_master_order_payments. +type PaymentLogDTO struct { + Id int64 `json:"id"` + OrderId int64 `json:"orderId"` + Step string `json:"step"` + Detail json.RawMessage `json:"detail,omitempty"` + CreatedById *int64 `json:"createdById,omitempty"` + CreatedByName string `json:"createdByName,omitempty"` + CreatedAt time.Time `json:"createdAt"` +} diff --git a/internal/payment/paymenthistory/handler.go b/internal/payment/paymenthistory/handler.go index 5600d50..a7c63fe 100644 --- a/internal/payment/paymenthistory/handler.go +++ b/internal/payment/paymenthistory/handler.go @@ -1,6 +1,7 @@ package paymenthistory import ( + "errors" "net/http" "online-back/internal/appresult" @@ -10,6 +11,7 @@ import ( "online-back/pkg/logging" "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v4" ) const baseURL = "/payment-history" @@ -26,6 +28,7 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler func (h *handler) Register(router *gin.RouterGroup) { group := router.Group(baseURL, appresult.JwtTokenCheck()) group.GET("", h.list) + group.GET("/:id", h.logs) } // list godoc @@ -62,6 +65,60 @@ func (h *handler) list(c *gin.Context) { c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } +// logs godoc +// @Summary Get payment logs for an order +// @Description Returns the full step-by-step audit trail (visa_master_payment_logs) recorded while processing an order's payout - evidence of what actually happened, independent of the summary rows returned by the list endpoint +// @Tags payment-history +// @Produce json +// @Security BearerAuth +// @Param id path int true "Order ID" +// @Success 200 {object} appresult.AppSuccess +// @Failure 400 {object} appresult.AppError +// @Failure 403 {object} appresult.AppError +// @Failure 404 {object} appresult.AppError +// @Failure 500 {object} appresult.AppError +// @Router /payment-history/{id} [get] +func (h *handler) logs(c *gin.Context) { + orderID, ok := shared.ParseID(c.Param("id")) + if !ok { + c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) + return + } + + if !h.guardOrder(c, orderID) { + return + } + + result, err := h.repository.Logs(c.Request.Context(), orderID) + if err != nil { + h.logger.Errorf("list payment logs failed (order %d): %v", orderID, err) + c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) + return + } + + c.JSON(http.StatusOK, appresult.SuccessWithData(result)) +} + +// guardOrder 404s if orderID doesn't exist, then enforces the same +// branch/own-scope rules as every other order-scoped endpoint (see +// visamasterpay.handler.guardOrder) before its logs are returned. +func (h *handler) guardOrder(c *gin.Context, orderID int64) bool { + if rbac.SkipRecordGuard(c) { + return true + } + userID, branchID, err := h.repository.OwnerInfo(c.Request.Context(), orderID) + if errors.Is(err, pgx.ErrNoRows) { + c.JSON(http.StatusNotFound, appresult.ErrNotFound) + return false + } + if err != nil { + h.logger.Errorf("payment history owner lookup failed (order %d): %v", orderID, err) + c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) + return false + } + return rbac.GuardOrder(c, userID, branchID) +} + func parseListFilter(c *gin.Context) (ListFilter, bool) { page, limit, ok := shared.ParsePageLimit(c) if !ok { diff --git a/internal/payment/paymenthistory/storage.go b/internal/payment/paymenthistory/storage.go index c2c6cab..082efcf 100644 --- a/internal/payment/paymenthistory/storage.go +++ b/internal/payment/paymenthistory/storage.go @@ -4,4 +4,9 @@ import "context" type Repository interface { List(ctx context.Context, filter ListFilter) (ListPaymentHistoryDTO, error) + // Logs returns the full visa_master_payment_logs audit trail for a + // single order, oldest first. + Logs(ctx context.Context, orderID int64) ([]PaymentLogDTO, error) + // OwnerInfo returns an order's owning user/branch, for rbac.GuardOrder. + OwnerInfo(ctx context.Context, orderID int64) (*int64, int64, error) }