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 <cursoragent@cursor.com>
This commit is contained in:
parent
ff5cb64758
commit
162f822343
|
|
@ -123,7 +123,25 @@ type UserProfileResponse struct {
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
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 {
|
type UserFilter struct {
|
||||||
|
|
|
||||||
|
|
@ -1460,6 +1460,17 @@ func (h *Handler) GetUserProfile(c *fiber.Ctx) error {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve subscription status:"+err.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)
|
lastLogin, err := h.authSvc.GetLastLogin(c.Context(), user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != authentication.ErrRefreshTokenNotFound {
|
if err != authentication.ErrRefreshTokenNotFound {
|
||||||
|
|
@ -1504,7 +1515,8 @@ func (h *Handler) GetUserProfile(c *fiber.Ctx) error {
|
||||||
PreferredLanguage: user.PreferredLanguage,
|
PreferredLanguage: user.PreferredLanguage,
|
||||||
CreatedAt: user.CreatedAt,
|
CreatedAt: user.CreatedAt,
|
||||||
UpdatedAt: user.UpdatedAt,
|
UpdatedAt: user.UpdatedAt,
|
||||||
SubscriptionStatus: subscriptionStatus,
|
SubscriptionStatus: subscriptionStatus,
|
||||||
|
ActiveSubscriptions: profileSubscriptionsFromDomain(activeSubs),
|
||||||
}
|
}
|
||||||
return response.WriteJSON(c, fiber.StatusOK, "User profile retrieved successfully", res, nil)
|
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")
|
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 {
|
type SearchUserByNameOrPhoneReq struct {
|
||||||
SearchString string `json:"query"`
|
SearchString string `json:"query"`
|
||||||
Role *domain.Role `json:"role,omitempty"`
|
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())
|
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)
|
lastLogin, err := h.authSvc.GetLastLogin(c.Context(), user.ID)
|
||||||
if err != nil && err != authentication.ErrRefreshTokenNotFound {
|
if err != nil && err != authentication.ErrRefreshTokenNotFound {
|
||||||
h.mongoLoggerSvc.Error("Failed to get user last login",
|
h.mongoLoggerSvc.Error("Failed to get user last login",
|
||||||
|
|
@ -1869,7 +1915,8 @@ func (h *Handler) GetUserByID(c *fiber.Ctx) error {
|
||||||
PreferredLanguage: user.PreferredLanguage,
|
PreferredLanguage: user.PreferredLanguage,
|
||||||
CreatedAt: user.CreatedAt,
|
CreatedAt: user.CreatedAt,
|
||||||
UpdatedAt: user.UpdatedAt,
|
UpdatedAt: user.UpdatedAt,
|
||||||
SubscriptionStatus: subscriptionStatus,
|
SubscriptionStatus: subscriptionStatus,
|
||||||
|
ActiveSubscriptions: profileSubscriptionsFromDomain(activeSubs),
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.WriteJSON(c, fiber.StatusOK, "User retrieved successfully", res, nil)
|
return response.WriteJSON(c, fiber.StatusOK, "User retrieved successfully", res, nil)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user