Enrich cards API responses with joined relations and share common helpers.
Nest user/branch/cardType/cardState on card, order, pin-order, and visa-master reads; accept flexible dates/IDs on pin create; widen pin region; ignore undefined list query params; extract internal/cards/shared to remove duplicated types and helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1fd0abbcb2
commit
0098422798
27 files changed
+942
-777
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/cards/visamaster"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
@@ -27,8 +28,6 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) visamaster.
|
||||
}
|
||||
}
|
||||
|
||||
const userDisplaySQL = `COALESCE(NULLIF(TRIM(CONCAT_WS(' ', u.first_name, u.last_name)), ''), NULLIF(u.name, ''), u.username, '')`
|
||||
|
||||
const orderFromClause = ` FROM visa_master_payment_orders o
|
||||
LEFT JOIN branches b ON b.id = o.branch_id
|
||||
LEFT JOIN users u ON u.id = o.user_id
|
||||
@@ -50,15 +49,7 @@ var orderColumnsSQL = strings.Join(orderColumns, ", ")
|
||||
|
||||
var orderWritableColumnsSQL = strings.Join(orderWritableColumns, ", ")
|
||||
|
||||
var orderSelectColumns = prefixedColumns("o", orderColumns) + `, p.region, b.name, COALESCE(b.unique_code, ''), COALESCE(b.region, ''), b.province_id, p.name, ` + userDisplaySQL + `, COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, '')`
|
||||
|
||||
func prefixedColumns(alias string, cols []string) string {
|
||||
parts := make([]string, len(cols))
|
||||
for i, col := range cols {
|
||||
parts[i] = alias + "." + col
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
var orderSelectColumns = shared.PrefixedColumns("o", orderColumns) + `, p.region, b.name, COALESCE(b.unique_code, ''), COALESCE(b.region, ''), b.province_id, p.name, ` + shared.UserDisplaySQL + `, COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, '')`
|
||||
|
||||
func resolveStatus(status *string) *string {
|
||||
if status == nil || strings.TrimSpace(*status) == "" {
|
||||
@@ -282,18 +273,18 @@ func scanOrder(row genericrepo.Scanner) (visamaster.VisaMasterPaymentOrderDTO, e
|
||||
|
||||
item.Branch.Id = item.BranchId
|
||||
item.Branch.Region = branchRegion
|
||||
if err := unmarshalLocalized(regionNameJSON, &item.RegionName); err != nil {
|
||||
if err := shared.UnmarshalLocalized(regionNameJSON, &item.RegionName); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderDTO{}, err
|
||||
}
|
||||
if err := unmarshalLocalized(branchNameJSON, &item.Branch.Name); err != nil {
|
||||
if err := shared.UnmarshalLocalized(branchNameJSON, &item.Branch.Name); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderDTO{}, err
|
||||
}
|
||||
if err := unmarshalLocalized(provinceNameJSON, &item.Branch.ProvinceName); err != nil {
|
||||
if err := shared.UnmarshalLocalized(provinceNameJSON, &item.Branch.ProvinceName); err != nil {
|
||||
return visamaster.VisaMasterPaymentOrderDTO{}, err
|
||||
}
|
||||
|
||||
if item.UserId != nil {
|
||||
item.User = &visamaster.UserInfo{
|
||||
item.User = &shared.UserInfo{
|
||||
Id: *item.UserId,
|
||||
Name: userName,
|
||||
Username: userUsername,
|
||||
@@ -311,10 +302,3 @@ func jsonOrDefault(raw []byte, fallback string) json.RawMessage {
|
||||
}
|
||||
return json.RawMessage(raw)
|
||||
}
|
||||
|
||||
func unmarshalLocalized(raw []byte, dest *visamaster.LocalizedText) error {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, dest)
|
||||
}
|
||||
@@ -3,62 +3,50 @@ package visamaster
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
)
|
||||
|
||||
type LocalizedText struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type BranchInfo struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name LocalizedText `json:"name"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
ProvinceName LocalizedText `json:"provinceName"`
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name shared.LocalizedText `json:"name"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
ProvinceName shared.LocalizedText `json:"provinceName"`
|
||||
}
|
||||
|
||||
type VisaMasterPaymentOrderDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Type *string `json:"type"`
|
||||
PassportName string `json:"passportName"`
|
||||
PassportSurname string `json:"passportSurname"`
|
||||
Phone string `json:"phone"`
|
||||
Email *string `json:"email"`
|
||||
Region string `json:"region"`
|
||||
RegionName LocalizedText `json:"regionName"`
|
||||
BranchId int64 `json:"branchId"`
|
||||
Branch BranchInfo `json:"branch"`
|
||||
UserId *int64 `json:"userId"`
|
||||
User *UserInfo `json:"user"`
|
||||
Address string `json:"address"`
|
||||
SenderDatas json.RawMessage `json:"senderDatas" swaggertype:"object"`
|
||||
PaymentReciever json.RawMessage `json:"paymentReciever" swaggertype:"array,object"`
|
||||
Documents json.RawMessage `json:"documents" swaggertype:"object"`
|
||||
Status *string `json:"status"`
|
||||
Notes *string `json:"notes"`
|
||||
SenderFullName string `json:"senderFullName"`
|
||||
SenderPassportSerie string `json:"senderPassportSerie"`
|
||||
SenderPassportNumber string `json:"senderPassportNumber"`
|
||||
SenderDepositAccount string `json:"senderDepositAccount"`
|
||||
RecieverFullName *string `json:"recieverFullName"`
|
||||
RecieverPassportSerie *string `json:"recieverPassportSerie"`
|
||||
RecieverPassportNumber *string `json:"recieverPassportNumber"`
|
||||
RecieverDepositAccount *string `json:"recieverDepositAccount"`
|
||||
Paid bool `json:"paid"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Type *string `json:"type"`
|
||||
PassportName string `json:"passportName"`
|
||||
PassportSurname string `json:"passportSurname"`
|
||||
Phone string `json:"phone"`
|
||||
Email *string `json:"email"`
|
||||
Region string `json:"region"`
|
||||
RegionName shared.LocalizedText `json:"regionName,omitempty"`
|
||||
BranchId int64 `json:"-"`
|
||||
Branch BranchInfo `json:"branch"`
|
||||
UserId *int64 `json:"-"`
|
||||
User *shared.UserInfo `json:"user,omitempty"`
|
||||
Address string `json:"address"`
|
||||
SenderDatas json.RawMessage `json:"senderDatas" swaggertype:"object"`
|
||||
PaymentReciever json.RawMessage `json:"paymentReciever" swaggertype:"array,object"`
|
||||
Documents json.RawMessage `json:"documents" swaggertype:"object"`
|
||||
Status *string `json:"status"`
|
||||
Notes *string `json:"notes"`
|
||||
SenderFullName string `json:"senderFullName"`
|
||||
SenderPassportSerie string `json:"senderPassportSerie"`
|
||||
SenderPassportNumber string `json:"senderPassportNumber"`
|
||||
SenderDepositAccount string `json:"senderDepositAccount"`
|
||||
RecieverFullName *string `json:"recieverFullName"`
|
||||
RecieverPassportSerie *string `json:"recieverPassportSerie"`
|
||||
RecieverPassportNumber *string `json:"recieverPassportNumber"`
|
||||
RecieverDepositAccount *string `json:"recieverDepositAccount"`
|
||||
Paid bool `json:"paid"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type CreateVisaMasterPaymentOrderDTO struct {
|
||||
|
||||
@@ -3,10 +3,9 @@ package visamaster
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
@@ -82,7 +81,7 @@ func (h *handler) list(c *gin.Context) {
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /visa-master-payment-orders/{id} [get]
|
||||
func (h *handler) getByID(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
id, ok := shared.ParseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
@@ -149,7 +148,7 @@ func (h *handler) create(c *gin.Context) {
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /visa-master-payment-orders/{id} [put]
|
||||
func (h *handler) update(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
id, ok := shared.ParseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
@@ -192,7 +191,7 @@ func (h *handler) update(c *gin.Context) {
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /visa-master-payment-orders/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
id, ok := shared.ParseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
@@ -214,68 +213,35 @@ func (h *handler) delete(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("visa master payment order removed"))
|
||||
}
|
||||
|
||||
func parseID(raw string) (int64, bool) {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
const (
|
||||
defaultPage = 1
|
||||
defaultLimit = 10
|
||||
maxLimit = 100
|
||||
)
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter := ListFilter{
|
||||
Page: defaultPage,
|
||||
Limit: defaultLimit,
|
||||
page, limit, ok := shared.ParsePageLimit(c)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter := ListFilter{Page: page, Limit: limit}
|
||||
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Page = page
|
||||
}
|
||||
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxLimit {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("name")); raw != "" {
|
||||
if raw := shared.QueryValue(c, "name"); raw != "" {
|
||||
filter.Name = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("status")); raw != "" {
|
||||
if raw := shared.QueryValue(c, "status"); raw != "" {
|
||||
filter.Status = &raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("region")); raw != "" {
|
||||
if raw := shared.QueryValue(c, "region"); raw != "" {
|
||||
filter.Region = raw
|
||||
}
|
||||
|
||||
if raw := c.Query("branch_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.BranchId = &id
|
||||
if id, ok := shared.OptionalQueryID(c, "branch_id", "branchId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.BranchId = id
|
||||
}
|
||||
|
||||
if raw := c.Query("user_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.UserId = &id
|
||||
if id, ok := shared.OptionalQueryID(c, "user_id", "userId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.UserId = id
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
|
||||
Reference in new issue
Block a user