Add Event Producer

Update User to carry webhook from circle if assigned
Refactor notification handling and update models for webhook support
This commit is contained in:
Mo Tarbin 2025-02-09 20:15:28 -05:00
parent 44cb5501dd
commit 04d1894aea
17 changed files with 351 additions and 101 deletions

View file

@ -518,7 +518,7 @@ func (h *Handler) UpdateUserDetails(c *gin.Context) {
user.Image = *req.Image
}
if err := h.userRepo.UpdateUser(c, user); err != nil {
if err := h.userRepo.UpdateUser(c, &user.User); err != nil {
c.JSON(500, gin.H{
"error": "Error updating user",
})
@ -608,8 +608,8 @@ func (h *Handler) UpdateNotificationTarget(c *gin.Context) {
}
type Request struct {
Type nModel.NotificationType `json:"type"`
Target string `json:"target"`
Type nModel.NotificationPlatform `json:"type"`
Target string `json:"target"`
}
var req Request
@ -617,7 +617,7 @@ func (h *Handler) UpdateNotificationTarget(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if req.Type == nModel.NotificationTypeNone {
if req.Type == nModel.NotificationPlatformNone {
err := h.userRepo.DeleteNotificationTarget(c, currentUser.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete notification target"})

View file

@ -25,6 +25,10 @@ type User struct {
Expiration *string `json:"expiration" gorm:"column:expiration;<-:false"` // read only column
UserNotificationTargets UserNotificationTarget `json:"notification_target" gorm:"foreignKey:UserID;references:ID"`
}
type UserDetails struct {
User
WebhookURL *string `json:"webhookURL" gorm:"column:webhook_url;<-:false"` // read only column
}
type UserPasswordReset struct {
ID int `gorm:"column:id"`
@ -43,10 +47,10 @@ type APIToken struct {
}
type UserNotificationTarget struct {
UserID int `json:"userId" gorm:"column:user_id;index;primaryKey"` // Index on userID
Type nModel.NotificationType `json:"type" gorm:"column:type"` // Type
TargetID string `json:"target_id" gorm:"column:target_id"` // Target ID
CreatedAt time.Time `json:"-" gorm:"column:created_at"`
UserID int `json:"userId" gorm:"column:user_id;index;primaryKey"` // Index on userID
Type nModel.NotificationPlatform `json:"type" gorm:"column:type"` // Type
TargetID string `json:"target_id" gorm:"column:target_id"` // Target ID
CreatedAt time.Time `json:"-" gorm:"column:created_at"`
}
type AuthProviderType int

View file

@ -13,7 +13,7 @@ import (
)
type IUserRepository interface {
GetUserByUsername(username string) (*uModel.User, error)
GetUserByUsername(username string) (*uModel.UserDetails, error)
GetUser(id int) (*uModel.User, error)
GetAllUsers() ([]*uModel.User, error)
CreateUser(user *uModel.User) error
@ -52,14 +52,14 @@ func (r *UserRepository) CreateUser(c context.Context, user *uModel.User) (*uMod
}
return user, nil
}
func (r *UserRepository) GetUserByUsername(c context.Context, username string) (*uModel.User, error) {
var user *uModel.User
func (r *UserRepository) GetUserByUsername(c context.Context, username string) (*uModel.UserDetails, error) {
var user *uModel.UserDetails
if r.isDonetickDotCom {
if err := r.db.WithContext(c).Preload("UserNotificationTargets").Table("users u").Select("u.*, ss.status as subscription, ss.expired_at as expiration").Joins("left join stripe_customers sc on sc.user_id = u.id ").Joins("left join stripe_subscriptions ss on sc.customer_id = ss.customer_id").Where("username = ?", username).First(&user).Error; err != nil {
if err := r.db.WithContext(c).Preload("UserNotificationTargets").Table("users u").Select("u.*, ss.status as subscription, ss.expired_at as expiration, c.webhook_url as webhook_url").Joins("left join stripe_customers sc on sc.user_id = u.id ").Joins("left join stripe_subscriptions ss on sc.customer_id = ss.customer_id").Joins("left join circles c on c.id = u.circle_id").Where("username = ?", username).First(&user).Error; err != nil {
return nil, err
}
} else {
if err := r.db.WithContext(c).Preload("UserNotificationTargets").Table("users u").Select("u.*, 'active' as subscription, '2999-12-31' as expiration").Where("username = ?", username).First(&user).Error; err != nil {
if err := r.db.WithContext(c).Preload("UserNotificationTargets").Table("users u").Select("u.*, 'active' as subscription, '2999-12-31' as expiration, c.webhook_url as webhook_url").Joins("left join circles c on c.id = u.circle_id").Where("username = ?", username).First(&user).Error; err != nil {
return nil, err
}
}
@ -160,7 +160,7 @@ func (r *UserRepository) DeleteAPIToken(c context.Context, userID int, tokenID s
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 nModel.NotificationType) error {
func (r *UserRepository) UpdateNotificationTarget(c context.Context, userID int, targetID string, targetType nModel.NotificationPlatform) error {
return r.db.WithContext(c).Save(&uModel.UserNotificationTarget{
UserID: userID,
TargetID: targetID,
@ -173,7 +173,7 @@ func (r *UserRepository) DeleteNotificationTarget(c context.Context, userID int)
return r.db.WithContext(c).Where("user_id = ?", userID).Delete(&uModel.UserNotificationTarget{}).Error
}
func (r *UserRepository) UpdateNotificationTargetForAllNotifications(c context.Context, userID int, targetID string, targetType nModel.NotificationType) error {
func (r *UserRepository) UpdateNotificationTargetForAllNotifications(c context.Context, userID int, targetID string, targetType nModel.NotificationPlatform) error {
return r.db.WithContext(c).Model(&nModel.Notification{}).Where("user_id = ?", userID).Update("target_id", targetID).Update("type", targetType).Error
}
func (r *UserRepository) UpdatePasswordByUserId(c context.Context, userID int, password string) error {