@@ -60,12 +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.
63+ // CartStats7d returns the cart's view, click, and reach counts over the last 7 days.
6464// 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 ) {
65+ func (s * Service ) CartStats7d (ctx context.Context , cartID int64 ) (views , clicks , reach int64 ) {
6666 views , _ = s .q .CartViews7d (ctx , cartID )
67- clicks , _ = s .q .CartClicks7d (ctx , pgtype.Int8 {Int64 : cartID , Valid : true })
68- return views , clicks
67+ cid := pgtype.Int8 {Int64 : cartID , Valid : true }
68+ clicks , _ = s .q .CartClicks7d (ctx , cid )
69+ reach , _ = s .q .CartReach7d (ctx , cid )
70+ return views , clicks , reach
6971}
7072
7173// ListMyCoverImages returns the distinct cover image URLs the user has used
@@ -101,8 +103,9 @@ func (s *Service) GetCart(ctx context.Context, userID, cartID int64) (sqlcgen.Ca
101103}
102104
103105// GetPublicCart resolves a public cart by slug, returning the cart, its items,
104- // and the owning user. It also bumps the view counter in the background.
105- func (s * Service ) GetPublicCart (ctx context.Context , slug string ) (sqlcgen.Cart , []sqlcgen.ListCartItemsRow , sqlcgen.User , error ) {
106+ // and the owning user. It also bumps the view counter in the background,
107+ // unless the viewer is the cart owner (to avoid counting self-views).
108+ func (s * Service ) GetPublicCart (ctx context.Context , slug string , viewerUserID int64 ) (sqlcgen.Cart , []sqlcgen.ListCartItemsRow , sqlcgen.User , error ) {
106109 cart , err := s .q .GetCartBySlug (ctx , slug )
107110 if err != nil {
108111 return sqlcgen.Cart {}, nil , sqlcgen.User {}, err
@@ -115,10 +118,13 @@ func (s *Service) GetPublicCart(ctx context.Context, slug string) (sqlcgen.Cart,
115118 if err != nil {
116119 return sqlcgen.Cart {}, nil , sqlcgen.User {}, err
117120 }
118- // Fire-and-forget view bump (best effort).
119- go func () {
120- _ = s .q .BumpCartViewsDaily (context .Background (), cart .ID )
121- }()
121+ // Fire-and-forget view bump (best effort) — but never count the owner's
122+ // own views of their own cart.
123+ if viewerUserID != cart .UserID {
124+ go func () {
125+ _ = s .q .BumpCartViewsDaily (context .Background (), cart .ID )
126+ }()
127+ }
122128 return cart , items , user , nil
123129}
124130
0 commit comments