Update Circle model to include webhook URL field

This commit is contained in:
Mo Tarbin 2025-02-11 22:02:15 -05:00
parent b1036730c4
commit 3fb83b0610
4 changed files with 59 additions and 9 deletions

View file

@ -14,7 +14,7 @@ type Circle struct {
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` // Updated at
InviteCode string `json:"invite_code" gorm:"column:invite_code"` // Invite code
Disabled bool `json:"disabled" gorm:"column:disabled"` // Disabled
WebhookURL *string `json:"-" gorm:"column:webhook_url"` // Webhook URL
WebhookURL *string `json:"webhook_url" gorm:"column:webhook_url"` // Webhook URL
}
type CircleDetail struct {

View file

@ -167,3 +167,7 @@ func (r *CircleRepository) RedeemPoints(c context.Context, circleID int, userID
}
return nil
}
func (r *CircleRepository) SetWebhookURL(c context.Context, circleID int, webhookURL *string) error {
return r.db.WithContext(c).Model(&cModel.Circle{}).Where("id = ?", circleID).Update("webhook_url", webhookURL).Error
}

View file

@ -24,14 +24,14 @@ const (
type EventType string
const (
EventTypeUnknown EventType = ""
EventTypeTaskCreated EventType = "task.created"
EventTypeTaskReminder EventType = "task.reminder"
EventTypeTaskUpdated EventType = "task.updated"
EventTypeTaskCompleted EventType = "task.completed"
EventTypeTaskReassigned EventType = "task.reassigned"
EventTypeTaskSkipped EventType = "task.skipped"
EventTypeThingChanged EventType = "thing.changed"
EventTypeUnknown EventType = ""
// EventTypeTaskCreated EventType = "task.created"
EventTypeTaskReminder EventType = "task.reminder"
// EventTypeTaskUpdated EventType = "task.updated"
EventTypeTaskCompleted EventType = "task.completed"
// EventTypeTaskReassigned EventType = "task.reassigned"
EventTypeTaskSkipped EventType = "task.skipped"
EventTypeThingChanged EventType = "thing.changed"
)
type Event struct {

View file

@ -686,6 +686,51 @@ func (h *Handler) updateUserPasswordLoggedInOnly(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
func (h *Handler) setWebhook(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 {
URL *string `json:"url"`
}
var req Request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
// get circle admins
admins, err := h.circleRepo.GetCircleAdmins(c, currentUser.CircleID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get circle details"})
return
}
// confirm that the user is an admin:
isAdmin := false
for _, admin := range admins {
if admin.ID == currentUser.ID {
isAdmin = true
break
}
}
if !isAdmin {
c.JSON(http.StatusForbidden, gin.H{"error": "You are not an admin"})
return
}
err = h.circleRepo.SetWebhookURL(c, currentUser.CircleID, req.URL)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to set webhook URL"})
return
}
c.JSON(http.StatusOK, gin.H{})
}
func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware, limiter *limiter.Limiter) {
userRoutes := router.Group("api/v1/users")
@ -697,6 +742,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("/webhook", h.setWebhook)
userRoutes.PUT("/targets", h.UpdateNotificationTarget)
userRoutes.PUT("change_password", h.updateUserPasswordLoggedInOnly)