ch some
This commit is contained in:
1 parent
28509eb4ca
commit
d1a6fcaadc
102 files changed
+2089
-1957
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user