133 lines
4.5 KiB
Go
133 lines
4.5 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/identity/rbac"
|
|
"online-back/internal/payment/paymenthistory"
|
|
"online-back/internal/shared"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) paymenthistory.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
const historyFromClause = ` FROM visa_master_order_payments p
|
|
JOIN visa_master_payment_orders o ON o.id = p.order_id
|
|
LEFT JOIN branches b ON b.id = o.branch_id
|
|
LEFT JOIN users u ON u.id = p.created_by`
|
|
|
|
const historySelectColumns = `p.id, p.order_id, COALESCE(o.unique_id, ''), COALESCE(o.type, ''),
|
|
o.branch_id, b.name, COALESCE(o.passport_name, ''), COALESCE(o.passport_surname, ''), p.user_id,
|
|
p.amount_tmt, p.bank_fee_tmt, p.gbus_fee_tmt, p.tveb_fee_usd, p.tveb_fee_tmt, p.total_amount_tmt,
|
|
p.exchange_rate_usd, p.exchange_rate_rub, p.payment_period,
|
|
p.gateway_status, p.gateway_reference, p.created_by, ` + shared.UserDisplaySQL + `,
|
|
p.created_at, p.updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter paymenthistory.ListFilter) (paymenthistory.ListPaymentHistoryDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
var count int64
|
|
countQuery := `SELECT COUNT(*)` + historyFromClause + whereClause
|
|
if err := r.client.QueryRow(ctx, countQuery, args...).Scan(&count); err != nil {
|
|
return paymenthistory.ListPaymentHistoryDTO{}, err
|
|
}
|
|
|
|
offset := (filter.Page - 1) * filter.Limit
|
|
limitPlaceholder := len(args) + 1
|
|
offsetPlaceholder := len(args) + 2
|
|
query := fmt.Sprintf(
|
|
`SELECT %s%s%s ORDER BY p.id DESC LIMIT $%d OFFSET $%d`,
|
|
historySelectColumns, historyFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
|
)
|
|
|
|
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
|
|
if err != nil {
|
|
return paymenthistory.ListPaymentHistoryDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []paymenthistory.PaymentHistoryItemDTO
|
|
for rows.Next() {
|
|
item, err := scanHistoryItem(rows)
|
|
if err != nil {
|
|
return paymenthistory.ListPaymentHistoryDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return paymenthistory.ListPaymentHistoryDTO{}, err
|
|
}
|
|
|
|
return paymenthistory.ListPaymentHistoryDTO{Count: count, Items: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter paymenthistory.ListFilter) (whereClause string, args []interface{}) {
|
|
conditions := []string{"p.deleted_at IS NULL", "o.deleted_at IS NULL"}
|
|
|
|
if filter.OrderId != nil {
|
|
args = append(args, *filter.OrderId)
|
|
conditions = append(conditions, fmt.Sprintf("p.order_id = $%d", len(args)))
|
|
}
|
|
|
|
if len(filter.BranchIds) > 0 {
|
|
conditions, args = rbac.AppendBranchIDs(conditions, args, "o.branch_id", filter.BranchIds)
|
|
} else if filter.BranchId != nil {
|
|
args = append(args, *filter.BranchId)
|
|
conditions = append(conditions, fmt.Sprintf("o.branch_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.UserId != nil {
|
|
args = append(args, *filter.UserId)
|
|
conditions = append(conditions, fmt.Sprintf("p.user_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Status != nil {
|
|
args = append(args, *filter.Status)
|
|
conditions = append(conditions, fmt.Sprintf("p.gateway_status = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Search != "" {
|
|
args = append(args, shared.LikePattern(filter.Search))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
o.passport_name ILIKE $%[1]d OR o.passport_surname ILIKE $%[1]d OR o.unique_id ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
return " WHERE " + strings.Join(conditions, " AND "), args
|
|
}
|
|
|
|
func scanHistoryItem(row pgx.Row) (paymenthistory.PaymentHistoryItemDTO, error) {
|
|
var item paymenthistory.PaymentHistoryItemDTO
|
|
var gatewayStatus string
|
|
var branchNameJSON []byte
|
|
if err := row.Scan(
|
|
&item.Id, &item.OrderId, &item.OrderUniqueId, &item.Type,
|
|
&item.BranchId, &branchNameJSON, &item.PassportName, &item.PassportSurname, &item.UserId,
|
|
&item.AmountTmt, &item.BankFeeTmt, &item.GbusFeeTmt, &item.TvebFeeUsd, &item.TvebFeeTmt, &item.TotalAmountTmt,
|
|
&item.ExchangeRateUsd, &item.ExchangeRateRub, &item.PaymentPeriod,
|
|
&gatewayStatus, &item.GatewayReference, &item.CreatedById, &item.CreatedByName,
|
|
&item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return paymenthistory.PaymentHistoryItemDTO{}, err
|
|
}
|
|
if err := shared.UnmarshalLocalized(branchNameJSON, &item.BranchName); err != nil {
|
|
return paymenthistory.PaymentHistoryItemDTO{}, err
|
|
}
|
|
item.GatewayStatus = gatewayStatus
|
|
item.Paid = gatewayStatus == "success"
|
|
return item, nil
|
|
}
|