ch something
This commit is contained in:
@@ -109,8 +109,8 @@ func (r *repository) List(ctx context.Context, filter visamaster.ListFilter) (vi
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
query := fmt.Sprintf(
|
||||
`SELECT o.id, o.type, o.created_at, o.region, o.branch_id, b.name, p.name,
|
||||
o.passport_name, o.passport_surname, o.phone, o.status, o.paid
|
||||
`SELECT o.id, o.unique_id, o.type, o.created_at, o.region, p.region, o.branch_id, b.name, p.name,
|
||||
o.passport_name, o.passport_surname, o.phone, o.status, o.paid, `+paidThisMonthSQL+`
|
||||
FROM visa_master_payment_orders o
|
||||
LEFT JOIN branches b ON b.id = o.branch_id
|
||||
LEFT JOIN provinces p ON p.id = b.province_id%s ORDER BY o.id DESC LIMIT $%d OFFSET $%d`,
|
||||
@@ -150,6 +150,18 @@ func buildListQuery(filter visamaster.ListFilter) (whereClause string, args []in
|
||||
)`, n))
|
||||
}
|
||||
|
||||
// Case-insensitive and trimmed, the same match visamasterpay uses to
|
||||
// pick Sber fees and credentials.
|
||||
if filter.Type != "" {
|
||||
args = append(args, strings.TrimSpace(filter.Type))
|
||||
conditions = append(conditions, fmt.Sprintf("LOWER(TRIM(o.type)) = LOWER($%d)", len(args)))
|
||||
}
|
||||
|
||||
if filter.ExcludeType != "" {
|
||||
args = append(args, strings.TrimSpace(filter.ExcludeType))
|
||||
conditions = append(conditions, fmt.Sprintf("LOWER(TRIM(COALESCE(o.type, ''))) <> LOWER($%d)", len(args)))
|
||||
}
|
||||
|
||||
if filter.Status != nil {
|
||||
args = append(args, *filter.Status)
|
||||
conditions = append(conditions, fmt.Sprintf("o.status = $%d", len(args)))
|
||||
@@ -296,18 +308,29 @@ func jsonOrDefault(raw []byte, fallback string) json.RawMessage {
|
||||
return json.RawMessage(raw)
|
||||
}
|
||||
|
||||
// paidThisMonthSQL: the order has a successful payment whose payment_period
|
||||
// falls in the current month in Ashgabat time (UTC+5, as visamasterpay).
|
||||
const paidThisMonthSQL = `EXISTS (
|
||||
SELECT 1 FROM visa_master_order_payments vp
|
||||
WHERE vp.order_id = o.id AND vp.gateway_status = 'success' AND vp.deleted_at IS NULL
|
||||
AND date_trunc('month', vp.payment_period) = date_trunc('month', (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') + INTERVAL '5 hours')
|
||||
)`
|
||||
|
||||
func scanOrderListItem(row genericrepo.Scanner) (visamaster.VisaMasterPaymentOrderListItemDTO, error) {
|
||||
var item visamaster.VisaMasterPaymentOrderListItemDTO
|
||||
var branchId int64
|
||||
var branchNameJSON, provinceNameJSON []byte
|
||||
var regionNameJSON, branchNameJSON, provinceNameJSON []byte
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.Type, &item.CreatedAt, &item.Region, &branchId, &branchNameJSON, &provinceNameJSON,
|
||||
&item.PassportName, &item.PassportSurname, &item.Phone, &item.Status, &item.Paid,
|
||||
&item.Id, &item.UniqueId, &item.Type, &item.CreatedAt, &item.Region, ®ionNameJSON, &branchId, &branchNameJSON, &provinceNameJSON,
|
||||
&item.PassportName, &item.PassportSurname, &item.Phone, &item.Status, &item.Paid, &item.PaidThisMonth,
|
||||
); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderListItemDTO{}, err
|
||||
}
|
||||
|
||||
if err := shared.UnmarshalLocalized(regionNameJSON, &item.RegionName); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderListItemDTO{}, err
|
||||
}
|
||||
var branchName shared.LocalizedText
|
||||
if err := shared.UnmarshalLocalized(branchNameJSON, &branchName); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderListItemDTO{}, err
|
||||
|
||||
@@ -107,24 +107,31 @@ type UpdateVisaMasterPaymentOrderDTO struct {
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
Name string
|
||||
Status *string
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
UserId *int64
|
||||
Region string
|
||||
Page int
|
||||
Limit int
|
||||
Name string
|
||||
// Type narrows to one order type; ExcludeType drops one (the Visa/Master
|
||||
// page hides sber, which has its own page).
|
||||
Type string
|
||||
ExcludeType string
|
||||
Status *string
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
UserId *int64
|
||||
Region string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
// VisaMasterPaymentOrderListItemDTO is the list row (visa/master and sber);
|
||||
// sender, receiver, documents and amounts come from GET /visa-master-payment-orders/:id.
|
||||
type VisaMasterPaymentOrderListItemDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Type *string `json:"type"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Region string `json:"region"`
|
||||
Branch *shared.NamedInfo `json:"branch,omitempty"`
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Type *string `json:"type"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Region string `json:"region"`
|
||||
// RegionName is the welaýat of the branch's province (Mary, Lebap, ...).
|
||||
RegionName shared.LocalizedText `json:"regionName"`
|
||||
Branch *shared.NamedInfo `json:"branch,omitempty"`
|
||||
// ProvinceName is the branch's province (etrap).
|
||||
ProvinceName shared.LocalizedText `json:"provinceName"`
|
||||
PassportName string `json:"passportName"`
|
||||
@@ -132,6 +139,9 @@ type VisaMasterPaymentOrderListItemDTO struct {
|
||||
Phone string `json:"phone"`
|
||||
Status *string `json:"status"`
|
||||
Paid bool `json:"paid"`
|
||||
// PaidThisMonth: a successful payment exists for the current month's
|
||||
// payment period (Ashgabat time).
|
||||
PaidThisMonth bool `json:"paidThisMonth"`
|
||||
}
|
||||
|
||||
type ListVisaMasterPaymentOrderDTO struct {
|
||||
|
||||
@@ -15,6 +15,10 @@ import (
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
// orderTypeSber is the type value of Sber orders, matched case-insensitively
|
||||
// like visamasterpay does when it picks Sber fees and credentials.
|
||||
const orderTypeSber = "sber"
|
||||
|
||||
const baseURL = "/visa-master-payment-orders"
|
||||
|
||||
type handler struct {
|
||||
@@ -29,6 +33,7 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := rbac.Group(router, baseURL, rbac.Require(perm.Cards))
|
||||
group.GET("", h.list)
|
||||
group.GET("/sber", h.listSber)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
@@ -37,11 +42,12 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
// list godoc
|
||||
// @Summary List visa/master payment orders
|
||||
// @Description Returns paginated visa/master payment orders with branch and user details
|
||||
// @Description Returns paginated visa/master payment orders; sber orders are excluded (see /visa-master-payment-orders/sber)
|
||||
// @Tags visa-master-payment-orders
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param name query string false "Filter by passport name or surname"
|
||||
// @Param type query string false "Filter by order type (e.g. student), case-insensitive"
|
||||
// @Param status query string false "Filter by status"
|
||||
// @Param branch_id query int false "Filter by branch ID"
|
||||
// @Param user_id query int false "Filter by user ID"
|
||||
@@ -58,7 +64,38 @@ func (h *handler) list(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
filter.ExcludeType = orderTypeSber
|
||||
h.respondList(c, filter)
|
||||
}
|
||||
|
||||
// listSber godoc
|
||||
// @Summary List sber payment orders
|
||||
// @Description Returns paginated payment orders of type sber
|
||||
// @Tags visa-master-payment-orders
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param name query string false "Filter by passport name or surname"
|
||||
// @Param status query string false "Filter by status"
|
||||
// @Param branch_id query int false "Filter by branch ID"
|
||||
// @Param user_id query int false "Filter by user ID"
|
||||
// @Param region query string false "Filter by region"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /visa-master-payment-orders/sber [get]
|
||||
func (h *handler) listSber(c *gin.Context) {
|
||||
filter, ok := parseListFilter(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
filter.Type = orderTypeSber
|
||||
h.respondList(c, filter)
|
||||
}
|
||||
|
||||
func (h *handler) respondList(c *gin.Context, filter ListFilter) {
|
||||
result, err := h.repository.List(c.Request.Context(), filter)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list visa master payment orders failed: %v", err)
|
||||
@@ -225,6 +262,10 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Name = raw
|
||||
}
|
||||
|
||||
if raw := shared.QueryValue(c, "type"); raw != "" {
|
||||
filter.Type = raw
|
||||
}
|
||||
|
||||
if raw := shared.QueryValue(c, "status"); raw != "" {
|
||||
filter.Status = &raw
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user