add subscriptions to currentUser JSON for frontend

This commit is contained in:
Alex
2024-09-29 21:14:03 +02:00
parent 33561692b6
commit 41738753f0

View File

@@ -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"}) c.JSON(http.StatusForbidden, gin.H{"error": "You are not authorized to update this user"})
return 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 // TODO: If it's not an admin, prevent changes to critical fields
// if userRole != constants.Roles.Admin { // if userRole != constants.Roles.Admin {
// existingUser, err := uc.Service.GetUserByID(jwtUserID) // existingUser, err := uc.Service.GetUserByID(jwtUserID)
@@ -111,7 +123,17 @@ func (uc *UserController) CurrentUserHandler(c *gin.Context) {
return 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) { func (uc *UserController) LogoutHandler(c *gin.Context) {