Skip to content

Commit bc1529d

Browse files
committed
fix(carts): surface real 7-day views/clicks on the dashboard
Analytics were written (cart_views_daily, click_daily) but never read — MarshalCart hardcoded ViewsLast7d/ClicksLast7d to 0, so the dashboard always showed zero. Add CartViews7d/CartClicks7d read queries + a CartStats7d service helper, and populate the counts in the dashboard list + single-cart responses.
1 parent 047fd2c commit bc1529d

7 files changed

Lines changed: 65 additions & 8 deletions

File tree

internal/carts/handlers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func listCarts(svc *Service) http.HandlerFunc {
6464
owner, _ := svc.GetUser(r.Context(), uid)
6565
for _, c := range list {
6666
items, _ := svc.ListCartItems(r.Context(), c.ID)
67-
out = append(out, MarshalCart(c, owner, items))
67+
v, cl := svc.CartStats7d(r.Context(), c.ID)
68+
out = append(out, MarshalCart(c, owner, items, int(v), int(cl)))
6869
}
6970
writeJSON(w, http.StatusOK, out)
7071
}
@@ -86,7 +87,7 @@ func createCart(svc *Service) http.HandlerFunc {
8687
return
8788
}
8889
owner, _ := svc.GetUser(r.Context(), uid)
89-
writeJSON(w, http.StatusCreated, MarshalCart(cart, owner, nil))
90+
writeJSON(w, http.StatusCreated, MarshalCart(cart, owner, nil, 0, 0))
9091
}
9192
}
9293

@@ -112,7 +113,8 @@ func getCart(svc *Service) http.HandlerFunc {
112113
return
113114
}
114115
owner, _ := svc.GetUser(r.Context(), uid)
115-
writeJSON(w, http.StatusOK, MarshalCart(cart, owner, items))
116+
v, cl := svc.CartStats7d(r.Context(), cart.ID)
117+
writeJSON(w, http.StatusOK, MarshalCart(cart, owner, items, int(v), int(cl)))
116118
}
117119
}
118120

@@ -147,7 +149,7 @@ func updateCart(svc *Service) http.HandlerFunc {
147149
return
148150
}
149151
owner, _ := svc.GetUser(r.Context(), uid)
150-
writeJSON(w, http.StatusOK, MarshalCart(cart, owner, nil))
152+
writeJSON(w, http.StatusOK, MarshalCart(cart, owner, nil, 0, 0))
151153
}
152154
}
153155

internal/carts/marshal.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func MarshalUser(u sqlcgen.User) UserJSON {
5757
}
5858

5959
// MarshalCart converts a sqlc Cart, its owner, and item rows into CartJSON.
60-
func MarshalCart(c sqlcgen.Cart, owner sqlcgen.User, items []sqlcgen.ListCartItemsRow) CartJSON {
60+
// viewsLast7d/clicksLast7d are the analytics counts (0 where not needed, e.g.
61+
// the public page that doesn't display them).
62+
func MarshalCart(c sqlcgen.Cart, owner sqlcgen.User, items []sqlcgen.ListCartItemsRow, viewsLast7d, clicksLast7d int) CartJSON {
6163
out := CartJSON{
6264
ID: intStr(c.ID),
6365
Slug: c.Slug,
@@ -68,8 +70,8 @@ func MarshalCart(c sqlcgen.Cart, owner sqlcgen.User, items []sqlcgen.ListCartIte
6870
Bio: pgTextStr(c.Description),
6971
CoverImageURL: pgTextStr(c.CoverImageUrl),
7072
AccentHex: "#B5532A",
71-
ViewsLast7d: 0,
72-
ClicksLast7d: 0,
73+
ViewsLast7d: viewsLast7d,
74+
ClicksLast7d: clicksLast7d,
7375
CreatedAt: c.CreatedAt.Time.Format(time.RFC3339),
7476
UpdatedAt: c.UpdatedAt.Time.Format(time.RFC3339),
7577
}

internal/carts/service.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func (s *Service) ListMyCarts(ctx context.Context, userID int64) ([]sqlcgen.Cart
6060
return s.q.ListCartsByUser(ctx, userID)
6161
}
6262

63+
// CartStats7d returns the cart's view and click counts over the last 7 days.
64+
// Best-effort: any read error yields 0 rather than failing the request.
65+
func (s *Service) CartStats7d(ctx context.Context, cartID int64) (views, clicks int64) {
66+
views, _ = s.q.CartViews7d(ctx, cartID)
67+
clicks, _ = s.q.CartClicks7d(ctx, pgtype.Int8{Int64: cartID, Valid: true})
68+
return views, clicks
69+
}
70+
6371
// ListMyCoverImages returns the distinct cover image URLs the user has used
6472
// across their carts, most-recent first — their "personal" cover library.
6573
func (s *Service) ListMyCoverImages(ctx context.Context, userID int64) ([]string, error) {

internal/db/queries.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,16 @@ UPDATE extension_tokens SET last_used_at = now() WHERE id = $1;
135135

136136
-- name: InsertFeedback :exec
137137
INSERT INTO feedback (message, email, name, page) VALUES ($1, $2, $3, $4);
138+
139+
-- ─── ANALYTICS (reads) ──────────────────────────────────────────────────────
140+
141+
-- name: CartViews7d :one
142+
SELECT COALESCE(SUM(views), 0)::bigint AS views
143+
FROM cart_views_daily
144+
WHERE cart_id = $1 AND day >= current_date - 6;
145+
146+
-- name: CartClicks7d :one
147+
SELECT COALESCE(SUM(cd.clicks), 0)::bigint AS clicks
148+
FROM click_daily cd
149+
JOIN links l ON cd.link_id = l.id
150+
WHERE l.cart_id = $1 AND cd.day >= current_date - 6;

internal/db/sqlc/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/sqlc/queries.sql.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/publicapi/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ func getPublicCart(svc *carts.Service) http.HandlerFunc {
3030
return
3131
}
3232
w.Header().Set("Content-Type", "application/json")
33-
_ = json.NewEncoder(w).Encode(carts.MarshalCart(cart, user, items))
33+
_ = json.NewEncoder(w).Encode(carts.MarshalCart(cart, user, items, 0, 0))
3434
}
3535
}

0 commit comments

Comments
 (0)