diff options
author | Mo Tarbin <mhed.t91@gmail.com> | 2024-08-10 00:31:54 -0400 |
---|---|---|
committer | Mo Tarbin <mhed.t91@gmail.com> | 2024-08-10 00:31:54 -0400 |
commit | bddb80b1349c517ceaeacb92f5f8be9f482f400a (patch) | |
tree | cbfd2d9432c9ae9f6b84fc2dcba352e03c09abff /internal/user | |
parent | 8609a289b61de939366b31a8a7a67c2327e9f5d1 (diff) | |
parent | 4f22460313f21494442fbea5b1fcda49fb897df0 (diff) | |
download | donetick-bddb80b1349c517ceaeacb92f5f8be9f482f400a.tar.gz donetick-bddb80b1349c517ceaeacb92f5f8be9f482f400a.tar.bz2 donetick-bddb80b1349c517ceaeacb92f5f8be9f482f400a.zip |
Merge branch 'dev'
Diffstat (limited to 'internal/user')
-rw-r--r-- | internal/user/handler.go | 27 | ||||
-rw-r--r-- | internal/user/model/model.go | 24 | ||||
-rw-r--r-- | internal/user/repo/repository.go | 4 |
3 files changed, 52 insertions, 3 deletions
diff --git a/internal/user/handler.go b/internal/user/handler.go index 15e881f..ff885f3 100644 --- a/internal/user/handler.go +++ b/internal/user/handler.go @@ -486,6 +486,32 @@ func (h *Handler) DeleteUserToken(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) } +func (h *Handler) UpdateNotificationTarget(c *gin.Context) { + currentUser, ok := auth.CurrentUser(c) + if !ok { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get current user"}) + return + } + + type Request struct { + Type uModel.UserNotificationType `json:"type" binding:"required"` + Token string `json:"token" binding:"required"` + } + + var req Request + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) + return + } + + err := h.userRepo.UpdateNotificationTarget(c, currentUser.ID, req.Token, req.Type) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update notification target"}) + return + } + + c.JSON(http.StatusOK, gin.H{}) +} func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware, limiter *limiter.Limiter) { userRoutes := router.Group("users") @@ -497,6 +523,7 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware, limiter userRoutes.POST("/tokens", h.CreateLongLivedToken) userRoutes.GET("/tokens", h.GetAllUserToken) userRoutes.DELETE("/tokens/:id", h.DeleteUserToken) + userRoutes.PUT("/targets", h.UpdateNotificationTarget) } authRoutes := router.Group("auth") diff --git a/internal/user/model/model.go b/internal/user/model/model.go index 4874ac1..4cfb38b 100644 --- a/internal/user/model/model.go +++ b/internal/user/model/model.go @@ -16,9 +16,10 @@ type User struct { UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` // Updated at Disabled bool `json:"disabled" gorm:"column:disabled"` // Disabled // Email string `json:"email" gorm:"column:email"` // Email - CustomerID *string `gorm:"column:customer_id;<-:false"` // read one column - Subscription *string `json:"subscription" gorm:"column:subscription;<-:false"` // read one column - Expiration *string `json:"expiration" gorm:"column:expiration;<-:false"` // read one column + CustomerID *string `gorm:"column:customer_id;<-:false"` // read only column + Subscription *string `json:"subscription" gorm:"column:subscription;<-:false"` // read only column + Expiration *string `json:"expiration" gorm:"column:expiration;<-:false"` // read only column + UserNotificationTargets []UserNotificationTarget `json:"-" gorm:"foreignKey:UserID;references:ID"` } type UserPasswordReset struct { @@ -36,3 +37,20 @@ type APIToken struct { Token string `json:"token" gorm:"column:token;index"` // Index on token CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"` } + +type UserNotificationTarget struct { + ID int `json:"id" gorm:"primary_key"` // Unique identifier + UserID int `json:"userId" gorm:"column:user_id;index"` // Index on userID + Type UserNotificationType `json:"type" gorm:"column:type"` // Type + TargetID string `json:"targetId" gorm:"column:target_id"` // Target ID + CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"` +} + +type UserNotificationType int8 + +const ( + _ UserNotificationType = iota + Android + IOS + Telegram +) diff --git a/internal/user/repo/repository.go b/internal/user/repo/repository.go index 76ddd54..7dde6fb 100644 --- a/internal/user/repo/repository.go +++ b/internal/user/repo/repository.go @@ -158,3 +158,7 @@ func (r *UserRepository) GetAllUserTokens(c context.Context, userID int) ([]*uMo func (r *UserRepository) DeleteAPIToken(c context.Context, userID int, tokenID string) error { return r.db.WithContext(c).Where("id = ? AND user_id = ?", tokenID, userID).Delete(&uModel.APIToken{}).Error } + +func (r *UserRepository) UpdateNotificationTarget(c context.Context, userID int, targetID string, targetType uModel.UserNotificationType) error { + return r.db.WithContext(c).Model(&uModel.UserNotificationTarget{}).Where("user_id = ? AND type = ?", userID, targetType).Update("target_id", targetID).Error +} |