package paymenttexts import ( "errors" "net/http" "strings" "online-back/internal/appresult" "online-back/internal/handlers" "online-back/internal/identity/rbac" "online-back/internal/identity/rbac/perm" "online-back/internal/shared" "online-back/pkg/logging" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4" ) const baseURL = "/payment-texts" type handler struct { logger *logging.Logger repository Repository } func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler { return &handler{logger: logger, repository: repository} } // Catalog: clients read the texts too (the payout screens show them); // editing needs payment-settings. Codes are added by migration because the // client apps look them up by code; only a super admin may delete one, and // clients then get its built-in default (see defaults.go). func (h *handler) Register(router *gin.RouterGroup) { group := rbac.Group(router, baseURL, rbac.Catalog(perm.PaymentSettings)) group.GET("", h.list) group.GET("/:code", h.getByCode) group.PUT("/:code", h.update) group.DELETE("/:code", h.delete) } // list godoc // @Summary List payment texts // @Description Returns the admin-editable, localized texts (warnings, hints) shown to clients on visa/master/sber payout screens. Clients only get active ones, with empty languages filled with a default text. // @Tags payment-texts // @Produce json // @Security BearerAuth // @Param search query string false "Filter by code, name or text" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} ListPaymentTextDTO // @Failure 500 {object} appresult.AppError // @Router /payment-texts [get] func (h *handler) list(c *gin.Context) { filter := ListFilter{ Search: shared.QueryValue(c, "search", "q"), ActiveOnly: rbac.IsOwnScope(c), } result, err := h.repository.List(c.Request.Context(), filter) if err != nil { h.logger.Errorf("list payment texts failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } if filter.ActiveOnly { for i := range result.Items { result.Items[i] = withFallback(result.Items[i]) } } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } // getByCode godoc // @Summary Get a payment text by code // @Description Returns one payment text by its fixed code (e.g. payment_warning_text, sber_payment_warning). For clients, empty languages are filled with a default text, a deleted known code returns its default, and inactive texts are 404. // @Tags payment-texts // @Produce json // @Security BearerAuth // @Param code path string true "Text code" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} PaymentTextDTO // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-texts/{code} [get] func (h *handler) getByCode(c *gin.Context) { code := strings.TrimSpace(c.Param("code")) client := rbac.IsOwnScope(c) item, err := h.repository.GetByCode(c.Request.Context(), code) if errors.Is(err, pgx.ErrNoRows) { if def, ok := defaultItem(code); ok && client { c.JSON(http.StatusOK, appresult.SuccessWithData(def)) return } c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("get payment text failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } if client { if !item.IsActive { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } item = withFallback(item) } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // update godoc // @Summary Update a payment text // @Description Updates the name, localized text and active flag of an existing payment text. The code itself cannot be changed. // @Tags payment-texts // @Accept json // @Produce json // @Security BearerAuth // @Param code path string true "Text code" // @Param body body UpdatePaymentTextDTO true "Payment text payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} PaymentTextDTO // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-texts/{code} [put] func (h *handler) update(c *gin.Context) { var dto UpdatePaymentTextDTO if err := c.ShouldBindJSON(&dto); err != nil || strings.TrimSpace(dto.Name) == "" { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } dto.Name = strings.TrimSpace(dto.Name) if id, ok := rbac.CurrentUserID(c); ok { dto.UpdatedBy = &id } item, err := h.repository.Update(c.Request.Context(), strings.TrimSpace(c.Param("code")), dto) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update payment text failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // delete godoc // @Summary Delete a payment text // @Description Super admin only. Deletes a payment text by code; clients then get the built-in default text for known codes. // @Tags payment-texts // @Produce json // @Security BearerAuth // @Param code path string true "Text code" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 403 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-texts/{code} [delete] func (h *handler) delete(c *gin.Context) { if !rbac.IsSuper(c) { c.JSON(http.StatusForbidden, appresult.ErrForbidden) return } deleted, err := h.repository.Delete(c.Request.Context(), strings.TrimSpace(c.Param("code"))) if err != nil { h.logger.Errorf("delete payment text failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } if !deleted { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } c.JSON(http.StatusOK, appresult.SuccessWithData("payment text removed")) }