From 41738753f01f135f04d366f72d9089de9415bce4 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Sun, 29 Sep 2024 21:14:03 +0200 Subject: [PATCH] add subscriptions to currentUser JSON for frontend --- internal/controllers/user_controller.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/controllers/user_controller.go b/internal/controllers/user_controller.go index c890498..76216d9 100644 --- a/internal/controllers/user_controller.go +++ b/internal/controllers/user_controller.go @@ -61,6 +61,18 @@ func (uc *UserController) UpdateHandler(c *gin.Context) { c.JSON(http.StatusForbidden, gin.H{"error": "You are not authorized to update this user"}) return } + if user.Membership.SubscriptionModel.Name == "" { + logger.Error.Printf("No subscription model provided: %v", user.Email) + c.JSON(http.StatusNotAcceptable, gin.H{"error": "No subscription model provided"}) + return + } + selectedModel, err := uc.MembershipService.GetModelByName(&user.Membership.SubscriptionModel.Name) + if err != nil { + logger.Error.Printf("%v:No subscription model found: %#v", user.Email, err) + c.JSON(http.StatusNotFound, gin.H{"error": "Not a valid subscription model"}) + return + } + user.Membership.SubscriptionModel = *selectedModel // TODO: If it's not an admin, prevent changes to critical fields // if userRole != constants.Roles.Admin { // existingUser, err := uc.Service.GetUserByID(jwtUserID) @@ -111,7 +123,17 @@ func (uc *UserController) CurrentUserHandler(c *gin.Context) { return } - c.JSON(http.StatusOK, user.Safe()) + subscriptions, err := uc.MembershipService.GetSubscriptions(nil) + if err != nil { + logger.Error.Printf("Error retrieving subscriptions: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving subscriptions."}) + return + } + + c.JSON(http.StatusOK, gin.H{ + "user": user.Safe(), + "subscriptions": subscriptions, + }) } func (uc *UserController) LogoutHandler(c *gin.Context) {