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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue