From 162f82234354cba540dffa1329562a84ab4510a6 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 14 Jul 2026 02:04:11 -0700 Subject: [PATCH] Include active subscriptions in user profile response. User profile and single-user endpoints now return active_subscriptions with plan category alongside subscription_status. Co-authored-by: Cursor --- internal/domain/user.go | 20 ++++++++++- internal/web_server/handlers/user.go | 51 ++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/internal/domain/user.go b/internal/domain/user.go index b56a0354..51a52bd0 100644 --- a/internal/domain/user.go +++ b/internal/domain/user.go @@ -123,7 +123,25 @@ type UserProfileResponse struct { CreatedAt time.Time `json:"created_at"` UpdatedAt *time.Time `json:"updated_at,omitempty"` - SubscriptionStatus string `json:"subscription_status"` + SubscriptionStatus string `json:"subscription_status"` + ActiveSubscriptions []UserProfileSubscription `json:"active_subscriptions"` +} + +// UserProfileSubscription is an active subscription summary embedded in user profile responses. +type UserProfileSubscription struct { + ID int64 `json:"id"` + PlanID int64 `json:"plan_id"` + PlanName *string `json:"plan_name,omitempty"` + Category *string `json:"category,omitempty"` + StartsAt string `json:"starts_at"` + ExpiresAt string `json:"expires_at"` + Status string `json:"status"` + AutoRenew bool `json:"auto_renew"` + DurationValue *int32 `json:"duration_value,omitempty"` + DurationUnit *string `json:"duration_unit,omitempty"` + Price *float64 `json:"price,omitempty"` + Currency *string `json:"currency,omitempty"` + CreatedAt string `json:"created_at"` } type UserFilter struct { diff --git a/internal/web_server/handlers/user.go b/internal/web_server/handlers/user.go index 0e89601a..5ce92e07 100644 --- a/internal/web_server/handlers/user.go +++ b/internal/web_server/handlers/user.go @@ -1460,6 +1460,17 @@ func (h *Handler) GetUserProfile(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve subscription status:"+err.Error()) } + activeSubs, err := h.subscriptionsSvc.GetActiveSubscriptions(c.Context(), user.ID) + if err != nil { + h.mongoLoggerSvc.Error("Failed to get active subscriptions for profile", + zap.Int64("userID", userID), + zap.Int("status_code", fiber.StatusInternalServerError), + zap.Error(err), + zap.Time("timestamp", time.Now()), + ) + return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve subscriptions:"+err.Error()) + } + lastLogin, err := h.authSvc.GetLastLogin(c.Context(), user.ID) if err != nil { if err != authentication.ErrRefreshTokenNotFound { @@ -1504,7 +1515,8 @@ func (h *Handler) GetUserProfile(c *fiber.Ctx) error { PreferredLanguage: user.PreferredLanguage, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt, - SubscriptionStatus: subscriptionStatus, + SubscriptionStatus: subscriptionStatus, + ActiveSubscriptions: profileSubscriptionsFromDomain(activeSubs), } return response.WriteJSON(c, fiber.StatusOK, "User profile retrieved successfully", res, nil) } @@ -1627,6 +1639,29 @@ func getMedium(email, phoneNumber string) (domain.OtpMedium, error) { return "", errors.New("both email and phone number are empty") } +func profileSubscriptionsFromDomain(subs []domain.UserSubscription) []domain.UserProfileSubscription { + out := make([]domain.UserProfileSubscription, 0, len(subs)) + for i := range subs { + s := &subs[i] + out = append(out, domain.UserProfileSubscription{ + ID: s.ID, + PlanID: s.PlanID, + PlanName: s.PlanName, + Category: s.PlanCategory, + StartsAt: s.StartsAt.Format("2006-01-02T15:04:05Z07:00"), + ExpiresAt: s.ExpiresAt.Format("2006-01-02T15:04:05Z07:00"), + Status: s.Status, + AutoRenew: s.AutoRenew, + DurationValue: s.DurationValue, + DurationUnit: s.DurationUnit, + Price: s.Price, + Currency: s.Currency, + CreatedAt: s.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), + }) + } + return out +} + type SearchUserByNameOrPhoneReq struct { SearchString string `json:"query"` Role *domain.Role `json:"role,omitempty"` @@ -1814,6 +1849,17 @@ func (h *Handler) GetUserByID(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve subscription status: "+err.Error()) } + activeSubs, err := h.subscriptionsSvc.GetActiveSubscriptions(c.Context(), user.ID) + if err != nil { + h.mongoLoggerSvc.Error("Failed to get active subscriptions for user by id", + zap.Int64("userID", userID), + zap.Int("status_code", fiber.StatusInternalServerError), + zap.Error(err), + zap.Time("timestamp", time.Now()), + ) + return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve subscriptions: "+err.Error()) + } + lastLogin, err := h.authSvc.GetLastLogin(c.Context(), user.ID) if err != nil && err != authentication.ErrRefreshTokenNotFound { h.mongoLoggerSvc.Error("Failed to get user last login", @@ -1869,7 +1915,8 @@ func (h *Handler) GetUserByID(c *fiber.Ctx) error { PreferredLanguage: user.PreferredLanguage, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt, - SubscriptionStatus: subscriptionStatus, + SubscriptionStatus: subscriptionStatus, + ActiveSubscriptions: profileSubscriptionsFromDomain(activeSubs), } return response.WriteJSON(c, fiber.StatusOK, "User retrieved successfully", res, nil)