Update Circle model to include webhook URL field
This commit is contained in:
parent
b1036730c4
commit
3fb83b0610
4 changed files with 59 additions and 9 deletions
|
@ -14,7 +14,7 @@ type Circle struct {
|
||||||
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` // Updated at
|
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` // Updated at
|
||||||
InviteCode string `json:"invite_code" gorm:"column:invite_code"` // Invite code
|
InviteCode string `json:"invite_code" gorm:"column:invite_code"` // Invite code
|
||||||
Disabled bool `json:"disabled" gorm:"column:disabled"` // Disabled
|
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 {
|
type CircleDetail struct {
|
||||||
|
|
|
@ -167,3 +167,7 @@ func (r *CircleRepository) RedeemPoints(c context.Context, circleID int, userID
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -25,11 +25,11 @@ type EventType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EventTypeUnknown EventType = ""
|
EventTypeUnknown EventType = ""
|
||||||
EventTypeTaskCreated EventType = "task.created"
|
// EventTypeTaskCreated EventType = "task.created"
|
||||||
EventTypeTaskReminder EventType = "task.reminder"
|
EventTypeTaskReminder EventType = "task.reminder"
|
||||||
EventTypeTaskUpdated EventType = "task.updated"
|
// EventTypeTaskUpdated EventType = "task.updated"
|
||||||
EventTypeTaskCompleted EventType = "task.completed"
|
EventTypeTaskCompleted EventType = "task.completed"
|
||||||
EventTypeTaskReassigned EventType = "task.reassigned"
|
// EventTypeTaskReassigned EventType = "task.reassigned"
|
||||||
EventTypeTaskSkipped EventType = "task.skipped"
|
EventTypeTaskSkipped EventType = "task.skipped"
|
||||||
EventTypeThingChanged EventType = "thing.changed"
|
EventTypeThingChanged EventType = "thing.changed"
|
||||||
)
|
)
|
||||||
|
|
|
@ -686,6 +686,51 @@ func (h *Handler) updateUserPasswordLoggedInOnly(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
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) {
|
func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware, limiter *limiter.Limiter) {
|
||||||
|
|
||||||
userRoutes := router.Group("api/v1/users")
|
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.POST("/tokens", h.CreateLongLivedToken)
|
||||||
userRoutes.GET("/tokens", h.GetAllUserToken)
|
userRoutes.GET("/tokens", h.GetAllUserToken)
|
||||||
userRoutes.DELETE("/tokens/:id", h.DeleteUserToken)
|
userRoutes.DELETE("/tokens/:id", h.DeleteUserToken)
|
||||||
|
userRoutes.PUT("/webhook", h.setWebhook)
|
||||||
userRoutes.PUT("/targets", h.UpdateNotificationTarget)
|
userRoutes.PUT("/targets", h.UpdateNotificationTarget)
|
||||||
userRoutes.PUT("change_password", h.updateUserPasswordLoggedInOnly)
|
userRoutes.PUT("change_password", h.updateUserPasswordLoggedInOnly)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue