fix(backend): use pq.Array for PostgreSQL array syntax in dashboard queries
This commit is contained in:
parent
940e6561f4
commit
2a602ab09e
1 changed files with 9 additions and 8 deletions
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/gofrs/uuid/v5"
|
"github.com/gofrs/uuid/v5"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/lib/pq"
|
||||||
|
|
||||||
"github.com/saveinmed/backend-go/internal/domain"
|
"github.com/saveinmed/backend-go/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
@ -984,7 +985,7 @@ func (r *Repository) SellerDashboard(ctx context.Context, sellerID uuid.UUID) (*
|
||||||
}
|
}
|
||||||
|
|
||||||
baseStatuses := []string{string(domain.OrderStatusPaid), string(domain.OrderStatusInvoiced), string(domain.OrderStatusDelivered)}
|
baseStatuses := []string{string(domain.OrderStatusPaid), string(domain.OrderStatusInvoiced), string(domain.OrderStatusDelivered)}
|
||||||
if err := r.db.GetContext(ctx, &sales, `SELECT COALESCE(SUM(total_cents), 0) AS total, COUNT(*) AS orders FROM orders WHERE seller_id = $1 AND status = ANY($2)`, sellerID, baseStatuses); err != nil {
|
if err := r.db.GetContext(ctx, &sales, `SELECT COALESCE(SUM(total_cents), 0) AS total, COUNT(*) AS orders FROM orders WHERE seller_id = $1 AND status = ANY($2::text[])`, sellerID, pq.Array(baseStatuses)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -993,11 +994,11 @@ func (r *Repository) SellerDashboard(ctx context.Context, sellerID uuid.UUID) (*
|
||||||
FROM order_items oi
|
FROM order_items oi
|
||||||
JOIN orders o ON o.id = oi.order_id
|
JOIN orders o ON o.id = oi.order_id
|
||||||
JOIN products p ON p.id = oi.product_id
|
JOIN products p ON p.id = oi.product_id
|
||||||
WHERE o.seller_id = $1 AND o.status = ANY($2)
|
WHERE o.seller_id = $1 AND o.status = ANY($2::text[])
|
||||||
GROUP BY oi.product_id, p.name
|
GROUP BY oi.product_id, p.name
|
||||||
ORDER BY total_quantity DESC
|
ORDER BY total_quantity DESC
|
||||||
LIMIT 5`
|
LIMIT 5`
|
||||||
if err := r.db.SelectContext(ctx, &topProducts, topQuery, sellerID, baseStatuses); err != nil {
|
if err := r.db.SelectContext(ctx, &topProducts, topQuery, sellerID, pq.Array(baseStatuses)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1022,7 +1023,7 @@ LIMIT 5`
|
||||||
|
|
||||||
func (r *Repository) AdminDashboard(ctx context.Context, since time.Time) (*domain.AdminDashboard, error) {
|
func (r *Repository) AdminDashboard(ctx context.Context, since time.Time) (*domain.AdminDashboard, error) {
|
||||||
var gmv *int64
|
var gmv *int64
|
||||||
if err := r.db.GetContext(ctx, &gmv, `SELECT COALESCE(SUM(total_cents), 0) FROM orders WHERE status = ANY($1)`, []string{string(domain.OrderStatusPaid), string(domain.OrderStatusInvoiced), string(domain.OrderStatusDelivered)}); err != nil {
|
if err := r.db.GetContext(ctx, &gmv, `SELECT COALESCE(SUM(total_cents), 0) FROM orders WHERE status = ANY($1::text[])`, pq.Array([]string{string(domain.OrderStatusPaid), string(domain.OrderStatusInvoiced), string(domain.OrderStatusDelivered)})); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1094,7 +1095,7 @@ SET active = EXCLUDED.active,
|
||||||
func (r *Repository) ListReviews(ctx context.Context, filter domain.ReviewFilter) ([]domain.Review, int64, error) {
|
func (r *Repository) ListReviews(ctx context.Context, filter domain.ReviewFilter) ([]domain.Review, int64, error) {
|
||||||
baseQuery := `FROM reviews`
|
baseQuery := `FROM reviews`
|
||||||
var args []any
|
var args []any
|
||||||
|
|
||||||
// Add where clauses if needed in future
|
// Add where clauses if needed in future
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
|
|
@ -1106,7 +1107,7 @@ func (r *Repository) ListReviews(ctx context.Context, filter domain.ReviewFilter
|
||||||
filter.Limit = 20
|
filter.Limit = 20
|
||||||
}
|
}
|
||||||
args = append(args, filter.Limit, filter.Offset)
|
args = append(args, filter.Limit, filter.Offset)
|
||||||
|
|
||||||
listQuery := fmt.Sprintf("SELECT id, order_id, buyer_id, seller_id, rating, comment, created_at %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d", baseQuery, len(args)-1, len(args))
|
listQuery := fmt.Sprintf("SELECT id, order_id, buyer_id, seller_id, rating, comment, created_at %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d", baseQuery, len(args)-1, len(args))
|
||||||
|
|
||||||
var reviews []domain.Review
|
var reviews []domain.Review
|
||||||
|
|
@ -1119,7 +1120,7 @@ func (r *Repository) ListReviews(ctx context.Context, filter domain.ReviewFilter
|
||||||
func (r *Repository) ListShipments(ctx context.Context, filter domain.ShipmentFilter) ([]domain.Shipment, int64, error) {
|
func (r *Repository) ListShipments(ctx context.Context, filter domain.ShipmentFilter) ([]domain.Shipment, int64, error) {
|
||||||
baseQuery := `FROM shipments`
|
baseQuery := `FROM shipments`
|
||||||
var args []any
|
var args []any
|
||||||
|
|
||||||
// Add where clauses if needed in future
|
// Add where clauses if needed in future
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
|
|
@ -1131,7 +1132,7 @@ func (r *Repository) ListShipments(ctx context.Context, filter domain.ShipmentFi
|
||||||
filter.Limit = 20
|
filter.Limit = 20
|
||||||
}
|
}
|
||||||
args = append(args, filter.Limit, filter.Offset)
|
args = append(args, filter.Limit, filter.Offset)
|
||||||
|
|
||||||
listQuery := fmt.Sprintf("SELECT id, order_id, carrier, tracking_code, external_tracking, status, created_at, updated_at %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d", baseQuery, len(args)-1, len(args))
|
listQuery := fmt.Sprintf("SELECT id, order_id, carrier, tracking_code, external_tracking, status, created_at, updated_at %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d", baseQuery, len(args)-1, len(args))
|
||||||
|
|
||||||
var shipments []domain.Shipment
|
var shipments []domain.Shipment
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue