Files
online-backend/app.go
GuwandykA 11e1fa4972
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
visamaster: record order actions for the "Ammallar" table
Every create, update and delete of a visa/master or sber order is now
recorded in visa_master_order_actions: who did it, finished or failed
(with the exception), and for an update only the columns that changed -
original before, changes after. Marking an order paid when a payment is
finalized is recorded the same way, with no user when the GBÜS callback
did it.

Actions are written in the transaction that changes the order, so one is
on record exactly when its change is; failed ones are written after the
rollback with the values they attempted. The payment finalize records
after its commit instead, so recording never holds up a payment GBÜS has
already taken. Updates that change nothing and creates that fail (no
order to attach to) are not recorded.

GET /visa-master-payment-orders/{id}/actions lists them, newest first;
GET /visa-master-payment-orders/{id}/actions/{actionId} adds original,
changes and exception. Both are staff-only and order-scoped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 22:39:42 +05:00

143 lines
5.3 KiB
Go
Executable File

package main
import (
"context"
"encoding/json"
"net/http"
"strings"
"online-back/docs"
"online-back/internal/config"
handlermanager "online-back/internal/handlers/manager"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"
)
// @title Service APIs
// @version 1.0
// @description Online payment system admin API. Localized fields ({en, ru, tm} objects) come back as a plain string in the language of the `lang` query parameter: tm, ru or en (default tm), falling back to tm, ru, en when that translation is empty. lang=all returns the full {en, ru, tm} objects (for edit forms); any other value is 400.
// @host localhost:5005
// @BasePath /api/admin
// @schemes http
// @tag.name auth
// @tag.description Authentication
// @tag.name province
// @tag.description Provinces
// @tag.name branches
// @tag.description Branches
// @tag.name payment-card
// @tag.description Payment cards
// @tag.name reason-card
// @tag.description Reason cards
// @tag.name loan-type
// @tag.description Loan types
// @tag.name loan-document
// @tag.description Loan required documents
// @tag.name loans-repayment
// @tag.description Loan repayments
// @tag.name card-orders
// @tag.description Card orders
// @tag.name card-rekwizit-orders
// @tag.description Card rekwizit (card details) orders
// @tag.name activity-logs
// @tag.description User activity audit trail
// @tag.name visa-master-orders
// @tag.description Visa/Master and Sber student-transfer orders: list, view, create, edit, delete
// @tag.name visa-master-order-actions
// @tag.description Visa/Master we Sber sargytlarynyň amallary (Ammallar): kim, haçan, näme goşdy, üýtgetdi ýa-da pozdy. Ulanylyş tertibi: 1) GET /visa-master-payment-orders/{id}/actions - sargydyň amallary; 2) GET /visa-master-payment-orders/{id}/actions/{actionId} - bir amalyň öňki we täze bahalary
// @tag.name visa-master-payment
// @tag.description Paying a Visa/Master or Sber order through GBÜS (HALKBANK): quote, pay, status re-check, bank callback
// @tag.name payment-history
// @tag.description Visa/Master we Sber töleg synanyşyklarynyň taryhy. Ulanylyş tertibi: 1) GET /payment-history - ähli tölegler; 2) GET /payment-history/{id} - bir sargydyň tölegleri; 3) GET /payment-history/payments/{id} - bir tölegiň jikme-jikligi; 4) GET /payment-history/{id}/gateway - sargydyň tölegleri GBÜS jogaplary bilen; 5) GET /payment-history/{id}/logs - sargydyň ädimme-ädim töleg loglary
// @tag.name backups
// @tag.description Database SQL backups
// @tag.name currencies
// @tag.description Currency exchange rates
//
// Security definitions go last: swag reads every line after one as its
// attributes, so anything declared below it (tags, schemes) is dropped.
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
cfg := config.GetConfig()
logger := logging.GetLogger()
docs.SwaggerInfo.Title = "Service APIs"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Description = "Online payment system admin API. Localized fields ({en, ru, tm} objects) come back as a plain string in the language of the `lang` query parameter: tm, ru or en (default tm), falling back to tm, ru, en when that translation is empty. lang=all returns the full {en, ru, tm} objects (for edit forms); any other value is 400."
docs.SwaggerInfo.Host = "localhost:" + cfg.Listen.Port
docs.SwaggerInfo.BasePath = "/api/admin"
docs.SwaggerInfo.Schemes = []string{"http"}
pool := mustConnectPostgresql(cfg, logger)
router := handlermanager.New(cfg, pool, logger)
router.GET("/swagger/*any", swaggerHandler())
addr := cfg.Listen.BindIP + ":" + cfg.Listen.Port
logger.Infof("starting server on %s", addr)
if err := router.Run(addr); err != nil {
logger.Fatal(err)
}
}
// swaggerHandler serves the Swagger UI, but answers doc.json itself so the
// spec can describe the address the browser actually used. Behind a reverse
// proxy that strips a path prefix, the baked-in host and basePath would point
// "Try it out" at the app's own listen address instead of the public URL.
func swaggerHandler() gin.HandlerFunc {
ui := ginSwagger.WrapHandler(swaggerFiles.Handler)
return func(c *gin.Context) {
if strings.HasSuffix(c.Param("any"), "/doc.json") {
serveSwaggerSpec(c)
return
}
ui(c)
}
}
func serveSwaggerSpec(c *gin.Context) {
doc, err := swag.ReadDoc()
if err != nil {
c.Status(http.StatusInternalServerError)
return
}
var spec map[string]any
if err := json.Unmarshal([]byte(doc), &spec); err != nil {
c.Status(http.StatusInternalServerError)
return
}
scheme := "http"
if proto := c.GetHeader("X-Forwarded-Proto"); proto != "" {
scheme = proto
} else if c.Request.TLS != nil {
scheme = "https"
}
basePath, _ := spec["basePath"].(string)
prefix := strings.TrimSuffix(c.GetHeader("X-Forwarded-Prefix"), "/")
spec["schemes"] = []string{scheme}
spec["host"] = c.Request.Host
spec["basePath"] = prefix + basePath
c.JSON(http.StatusOK, spec)
}
func mustConnectPostgresql(cfg *config.Config, logger *logging.Logger) *pgxpool.Pool {
pool, err := postgresql.NewClient(context.Background(), 3, cfg.Storage)
if err != nil {
logger.Fatal(err)
}
return pool
}