ch some
This commit is contained in:
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cardbalance"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -30,23 +32,24 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) cardbalance
|
||||
var cardBalanceColumns = []string{
|
||||
"id", "passport_number", "card_last4", "expiry_month", "expiry_year", "expiry_at",
|
||||
"balance_amount", "balance_currency", "status", "checked_at",
|
||||
"created_by", "created_at", "updated_at",
|
||||
"branch_id", "created_by", "created_at", "updated_at",
|
||||
}
|
||||
|
||||
var cardBalanceColumnsSQL = strings.Join(cardBalanceColumns, ", ")
|
||||
|
||||
var cardBalanceWritableColumns = []string{
|
||||
"passport_number", "card_number", "expiry_month", "expiry_year",
|
||||
"balance_amount", "balance_currency", "status", "checked_at", "created_by",
|
||||
"balance_amount", "balance_currency", "status", "checked_at", "branch_id", "created_by",
|
||||
}
|
||||
|
||||
var cardBalanceWritableColumnsSQL = strings.Join(cardBalanceWritableColumns, ", ")
|
||||
|
||||
const cardBalanceFromClause = ` FROM card_balances cb
|
||||
LEFT JOIN users u ON u.id = cb.created_by`
|
||||
LEFT JOIN users u ON u.id = cb.created_by
|
||||
LEFT JOIN branches b ON b.id = cb.branch_id`
|
||||
|
||||
var cardBalanceSelectColumns = shared.PrefixedColumns("cb", cardBalanceColumns) + `,
|
||||
COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, ''), ` + shared.UserDisplaySQL
|
||||
COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, ''), ` + shared.UserDisplaySQL + `, b.name`
|
||||
|
||||
func resolveStatus(status *string) string {
|
||||
if status == nil || strings.TrimSpace(*status) == "" {
|
||||
@@ -66,7 +69,7 @@ func createArgs(dto cardbalance.CreateCardBalanceDTO) []interface{} {
|
||||
return []interface{}{
|
||||
dto.PassportNumber, dto.CardNumber, dto.ExpiryMonth, dto.ExpiryYear,
|
||||
dto.BalanceAmount, resolveCurrency(dto.BalanceCurrency), resolveStatus(dto.Status),
|
||||
shared.OptionalTime(dto.CheckedAt), shared.OptionalInt64(dto.CreatedBy),
|
||||
shared.OptionalTime(dto.CheckedAt), shared.OptionalInt64(dto.BranchId), shared.OptionalInt64(dto.CreatedBy),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +77,7 @@ func updateArgs(dto cardbalance.UpdateCardBalanceDTO) []interface{} {
|
||||
return []interface{}{
|
||||
dto.PassportNumber, dto.CardNumber, dto.ExpiryMonth, dto.ExpiryYear,
|
||||
dto.BalanceAmount, resolveCurrency(dto.BalanceCurrency), resolveStatus(dto.Status),
|
||||
shared.OptionalTime(dto.CheckedAt), shared.OptionalInt64(dto.CreatedBy),
|
||||
shared.OptionalTime(dto.CheckedAt), shared.OptionalInt64(dto.BranchId), shared.OptionalInt64(dto.CreatedBy),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +136,13 @@ func buildListQuery(filter cardbalance.ListFilter) (whereClause string, args []i
|
||||
conditions = append(conditions, fmt.Sprintf("cb.created_by = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(filter.BranchIds) > 0 {
|
||||
conditions, args = rbac.AppendBranchIDs(conditions, args, "cb.branch_id", filter.BranchIds)
|
||||
} else if filter.BranchId != nil {
|
||||
args = append(args, *filter.BranchId)
|
||||
conditions = append(conditions, fmt.Sprintf("cb.branch_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
@@ -156,7 +166,8 @@ func (r *repository) Create(ctx context.Context, dto cardbalance.CreateCardBalan
|
||||
)
|
||||
SELECT %s
|
||||
FROM inserted cb
|
||||
LEFT JOIN users u ON u.id = cb.created_by`,
|
||||
LEFT JOIN users u ON u.id = cb.created_by
|
||||
LEFT JOIN branches b ON b.id = cb.branch_id`,
|
||||
cardBalanceWritableColumnsSQL, strings.Join(placeholders, ", "),
|
||||
cardBalanceColumnsSQL, cardBalanceSelectColumns,
|
||||
)
|
||||
@@ -181,7 +192,8 @@ func (r *repository) Update(ctx context.Context, id int64, dto cardbalance.Updat
|
||||
)
|
||||
SELECT %s
|
||||
FROM updated cb
|
||||
LEFT JOIN users u ON u.id = cb.created_by`,
|
||||
LEFT JOIN users u ON u.id = cb.created_by
|
||||
LEFT JOIN branches b ON b.id = cb.branch_id`,
|
||||
strings.Join(assignments, ", "), len(args), cardBalanceColumnsSQL, cardBalanceSelectColumns,
|
||||
)
|
||||
|
||||
@@ -201,19 +213,30 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
|
||||
func scanCardBalanceWithJoins(row genericrepo.Scanner) (cardbalance.CardBalanceDTO, error) {
|
||||
var item cardbalance.CardBalanceDTO
|
||||
var branchID sql.NullInt64
|
||||
var branchNameJSON []byte
|
||||
var userUsername, userPhone, userEmail, userName string
|
||||
|
||||
dest := []interface{}{
|
||||
&item.Id, &item.PassportNumber, &item.CardLast4, &item.ExpiryMonth, &item.ExpiryYear, &item.ExpiryAt,
|
||||
&item.BalanceAmount, &item.BalanceCurrency, &item.Status, &item.CheckedAt,
|
||||
&item.CreatedById, &item.CreatedAt, &item.UpdatedAt,
|
||||
&userUsername, &userPhone, &userEmail, &userName,
|
||||
&branchID, &item.CreatedById, &item.CreatedAt, &item.UpdatedAt,
|
||||
&userUsername, &userPhone, &userEmail, &userName, &branchNameJSON,
|
||||
}
|
||||
|
||||
if err := row.Scan(dest...); err != nil {
|
||||
return cardbalance.CardBalanceDTO{}, err
|
||||
}
|
||||
|
||||
if branchID.Valid {
|
||||
item.BranchId = branchID.Int64
|
||||
var branchName shared.LocalizedText
|
||||
if err := shared.UnmarshalLocalized(branchNameJSON, &branchName); err != nil {
|
||||
return cardbalance.CardBalanceDTO{}, err
|
||||
}
|
||||
item.Branch = &shared.NamedInfo{Id: item.BranchId, Name: branchName}
|
||||
}
|
||||
|
||||
if item.CreatedById != nil {
|
||||
item.CreatedBy = &shared.UserInfo{
|
||||
Id: *item.CreatedById,
|
||||
|
||||
@@ -3,24 +3,26 @@ package cardbalance
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type CardBalanceDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
PassportNumber string `json:"passportNumber"`
|
||||
CardLast4 string `json:"cardLast4"`
|
||||
ExpiryMonth int16 `json:"expiryMonth"`
|
||||
ExpiryYear int16 `json:"expiryYear"`
|
||||
ExpiryAt time.Time `json:"expiryAt"`
|
||||
BalanceAmount *float64 `json:"balanceAmount"`
|
||||
BalanceCurrency string `json:"balanceCurrency"`
|
||||
Status string `json:"status"`
|
||||
CheckedAt *time.Time `json:"checkedAt"`
|
||||
CreatedById *int64 `json:"-"`
|
||||
CreatedBy *shared.UserInfo `json:"createdBy,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
Id int64 `json:"id"`
|
||||
PassportNumber string `json:"passportNumber"`
|
||||
CardLast4 string `json:"cardLast4"`
|
||||
ExpiryMonth int16 `json:"expiryMonth"`
|
||||
ExpiryYear int16 `json:"expiryYear"`
|
||||
ExpiryAt time.Time `json:"expiryAt"`
|
||||
BalanceAmount *float64 `json:"balanceAmount"`
|
||||
BalanceCurrency string `json:"balanceCurrency"`
|
||||
Status string `json:"status"`
|
||||
CheckedAt *time.Time `json:"checkedAt"`
|
||||
BranchId int64 `json:"-"`
|
||||
Branch *shared.NamedInfo `json:"branch,omitempty"`
|
||||
CreatedById *int64 `json:"-"`
|
||||
CreatedBy *shared.UserInfo `json:"createdBy,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type CreateCardBalanceDTO struct {
|
||||
@@ -32,6 +34,7 @@ type CreateCardBalanceDTO struct {
|
||||
BalanceCurrency *string `json:"balanceCurrency"`
|
||||
Status *string `json:"status"`
|
||||
CheckedAt *shared.FlexibleTime `json:"checkedAt" swaggertype:"string" format:"date-time"`
|
||||
BranchId *shared.FlexibleInt64 `json:"branchId" swaggertype:"integer"`
|
||||
CreatedBy *shared.FlexibleInt64 `json:"createdBy" swaggertype:"integer"`
|
||||
}
|
||||
|
||||
@@ -44,6 +47,7 @@ type UpdateCardBalanceDTO struct {
|
||||
BalanceCurrency *string `json:"balanceCurrency"`
|
||||
Status *string `json:"status"`
|
||||
CheckedAt *shared.FlexibleTime `json:"checkedAt" swaggertype:"string" format:"date-time"`
|
||||
BranchId *shared.FlexibleInt64 `json:"branchId" swaggertype:"integer"`
|
||||
CreatedBy *shared.FlexibleInt64 `json:"createdBy" swaggertype:"integer"`
|
||||
}
|
||||
|
||||
@@ -51,6 +55,8 @@ type ListFilter struct {
|
||||
Status *string
|
||||
PassportNumber string
|
||||
CreatedBy *int64
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
@@ -5,8 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -94,6 +95,9 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardOrder(c, item.CreatedById, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
@@ -148,6 +152,10 @@ func (h *handler) update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardCardBalanceByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateCardBalanceDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
@@ -187,6 +195,10 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !h.guardCardBalanceByID(c, id) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
@@ -199,6 +211,22 @@ func (h *handler) delete(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("card balance removed"))
|
||||
}
|
||||
|
||||
func (h *handler) guardCardBalanceByID(c *gin.Context, id int64) bool {
|
||||
if rbac.SkipRecordGuard(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardOrder(c, item.CreatedById, item.BranchId)
|
||||
}
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
page, limit, ok := shared.ParsePageLimit(c)
|
||||
if !ok {
|
||||
@@ -220,5 +248,12 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.CreatedBy = id
|
||||
}
|
||||
|
||||
if id, ok := shared.OptionalQueryID(c, "branch_id", "branchId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.BranchId = id
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cardorder"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
@@ -3,7 +3,7 @@ package cardorder
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type CardOrderDTO struct {
|
||||
@@ -46,67 +46,67 @@ type CardOrderDTO struct {
|
||||
}
|
||||
|
||||
type CreateCardOrderDTO struct {
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *shared.FlexibleInt64 `json:"userId" swaggertype:"integer"`
|
||||
Paid *bool `json:"paid"`
|
||||
Paid *bool `json:"paid"`
|
||||
}
|
||||
|
||||
type UpdateCardOrderDTO struct {
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *shared.FlexibleInt64 `json:"userId" swaggertype:"integer"`
|
||||
Paid bool `json:"paid"`
|
||||
Paid bool `json:"paid"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cardpinorders"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
@@ -3,7 +3,7 @@ package cardpinorders
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type CardPinOrderDTO struct {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cardrekwizit"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
@@ -3,7 +3,7 @@ package cardrekwizit
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type CardRekwizitOrderDTO struct {
|
||||
@@ -96,6 +96,6 @@ type ListFilter struct {
|
||||
}
|
||||
|
||||
type ListCardRekwizitOrderDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
Count int64 `json:"count"`
|
||||
CardRekwizitOrders []CardRekwizitOrderDTO `json:"cardRekwizitOrders"`
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/cards"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
@@ -3,7 +3,7 @@ package cards
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type CardDTO struct {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/paymentcard"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -49,7 +49,7 @@ func buildListQuery(filter paymentcard.ListFilter) (whereClause string, args []i
|
||||
var conditions []string
|
||||
|
||||
if filter.CardName != "" {
|
||||
args = append(args, likePattern(filter.CardName))
|
||||
args = append(args, shared.LikePattern(filter.CardName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
c.name->>'en' ILIKE $%[1]d OR c.name->>'ru' ILIKE $%[1]d OR c.name->>'tm' ILIKE $%[1]d
|
||||
@@ -68,13 +68,6 @@ func buildListQuery(filter paymentcard.ListFilter) (whereClause string, args []i
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
term = strings.ReplaceAll(term, `_`, `\_`)
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (paymentcard.PaymentCardDTO, error) {
|
||||
return r.generic.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package paymentcard
|
||||
|
||||
import "online-back/internal/cards/shared"
|
||||
import "online-back/internal/shared"
|
||||
|
||||
type PaymentCardDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"strconv"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/reasonscard"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -48,7 +48,7 @@ func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (r
|
||||
func buildListQuery(filter reasonscard.ListFilter) (whereClause string, args []interface{}) {
|
||||
var conditions []string
|
||||
if filter.ReasonName != "" {
|
||||
args = append(args, likePattern(filter.ReasonName))
|
||||
args = append(args, shared.LikePattern(filter.ReasonName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
r.name->>'en' ILIKE $%[1]d OR r.name->>'ru' ILIKE $%[1]d OR r.name->>'tm' ILIKE $%[1]d
|
||||
@@ -62,13 +62,6 @@ func buildListQuery(filter reasonscard.ListFilter) (whereClause string, args []i
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
term = strings.ReplaceAll(term, `_`, `\_`)
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonCardDTO, error) {
|
||||
return r.generic.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package reasonscard
|
||||
import (
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type ReasonCardDTO struct {
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FlexibleTime time.Time
|
||||
|
||||
func (t FlexibleTime) Time() time.Time { return time.Time(t) }
|
||||
|
||||
func (t FlexibleTime) IsZero() bool { return time.Time(t).IsZero() }
|
||||
|
||||
func (t FlexibleTime) MarshalJSON() ([]byte, error) {
|
||||
return time.Time(t).MarshalJSON()
|
||||
}
|
||||
|
||||
func (t *FlexibleTime) UnmarshalJSON(b []byte) error {
|
||||
*t = FlexibleTime{}
|
||||
raw := strings.TrimSpace(string(b))
|
||||
if raw == "" || raw == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var n json.Number
|
||||
if err := json.Unmarshal(b, &n); err == nil {
|
||||
s := n.String()
|
||||
if parsed, ok := parseFlexibleTime(s); ok {
|
||||
*t = parsed
|
||||
return nil
|
||||
}
|
||||
if unix, err := n.Int64(); err == nil && unix > 0 {
|
||||
*t = FlexibleTime(time.Unix(unix, 0).UTC())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return nil
|
||||
}
|
||||
if parsed, ok := parseFlexibleTime(s); ok {
|
||||
*t = parsed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseFlexibleTime(s string) (FlexibleTime, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
case "string", "undefined", "null", "none", "invalid", "nan", "-":
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
for _, layout := range []string{
|
||||
time.RFC3339Nano,
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04:05Z07:00",
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02",
|
||||
"02.01.2006",
|
||||
"02/01/2006",
|
||||
"02-01-2006",
|
||||
"2006/01/02",
|
||||
"20060102",
|
||||
} {
|
||||
if parsed, err := time.Parse(layout, s); err == nil {
|
||||
return FlexibleTime(parsed), true
|
||||
}
|
||||
}
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
|
||||
type FlexibleInt64 int64
|
||||
|
||||
func (n FlexibleInt64) Int64() int64 { return int64(n) }
|
||||
|
||||
func (n *FlexibleInt64) UnmarshalJSON(b []byte) error {
|
||||
if string(b) == "null" {
|
||||
*n = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var v int64
|
||||
if err := json.Unmarshal(b, &v); err == nil {
|
||||
*n = FlexibleInt64(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
*n = 0
|
||||
return nil
|
||||
}
|
||||
v, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*n = FlexibleInt64(v)
|
||||
return nil
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultPage = 1
|
||||
DefaultLimit = 10
|
||||
MaxLimit = 100
|
||||
)
|
||||
|
||||
func ParseID(raw string) (int64, bool) {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func QueryValue(c *gin.Context, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
||||
return raw
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func OptionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
|
||||
raw := QueryValue(c, keys...)
|
||||
if raw == "" {
|
||||
return nil, true
|
||||
}
|
||||
id, ok := ParseID(raw)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return &id, true
|
||||
}
|
||||
|
||||
func ParsePageLimit(c *gin.Context) (page, limit int, ok bool) {
|
||||
page = DefaultPage
|
||||
limit = DefaultLimit
|
||||
|
||||
if raw := QueryValue(c, "page"); raw != "" {
|
||||
p, err := strconv.Atoi(raw)
|
||||
if err != nil || p < 1 {
|
||||
return 0, 0, false
|
||||
}
|
||||
page = p
|
||||
}
|
||||
|
||||
if raw := QueryValue(c, "limit"); raw != "" {
|
||||
l, err := strconv.Atoi(raw)
|
||||
if err != nil || l < 1 {
|
||||
return 0, 0, false
|
||||
}
|
||||
if l > MaxLimit {
|
||||
l = MaxLimit
|
||||
}
|
||||
limit = l
|
||||
}
|
||||
|
||||
return page, limit, true
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const UserDisplaySQL = `COALESCE(NULLIF(TRIM(CONCAT_WS(' ', u.first_name, u.last_name)), ''), NULLIF(u.name, ''), u.username, '')`
|
||||
|
||||
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, ", ")
|
||||
}
|
||||
|
||||
func UnmarshalLocalized(raw []byte, dest *LocalizedText) error {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, dest)
|
||||
}
|
||||
|
||||
func OptionalTime(t *FlexibleTime) *time.Time {
|
||||
if t == nil || t.IsZero() {
|
||||
return nil
|
||||
}
|
||||
tt := t.Time()
|
||||
return &tt
|
||||
}
|
||||
|
||||
func OptionalInt64(n *FlexibleInt64) *int64 {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
v := n.Int64()
|
||||
return &v
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package shared
|
||||
|
||||
type LocalizedText struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type NamedInfo struct {
|
||||
Id int64 `json:"id"`
|
||||
Name LocalizedText `json:"name"`
|
||||
}
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/cards/visamaster"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
@@ -138,7 +138,7 @@ func buildListQuery(filter visamaster.ListFilter) (whereClause string, args []in
|
||||
var conditions []string
|
||||
|
||||
if filter.Name != "" {
|
||||
args = append(args, likePattern(filter.Name))
|
||||
args = append(args, shared.LikePattern(filter.Name))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
o.passport_name ILIKE $%[1]d OR o.passport_surname ILIKE $%[1]d
|
||||
@@ -175,13 +175,6 @@ func buildListQuery(filter visamaster.ListFilter) (whereClause string, args []in
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
term = strings.ReplaceAll(term, `_`, `\_`)
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (visamaster.VisaMasterPaymentOrderDTO, error) {
|
||||
query := `SELECT ` + orderSelectColumns + orderFromClause + ` WHERE o.id = $1`
|
||||
return scanOrder(r.client.QueryRow(ctx, query, id))
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/shared"
|
||||
)
|
||||
|
||||
type BranchInfo struct {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
Reference in New Issue
Block a user