|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/go-chi/chi/v5" |
| 7 | + "github.com/waitless/waitless/internal/database" |
| 8 | + middleware "github.com/waitless/waitless/internal/middleware" |
| 9 | + "github.com/waitless/waitless/internal/models" |
| 10 | + "github.com/waitless/waitless/internal/services" |
| 11 | +) |
| 12 | + |
| 13 | +// GetTelegramConfig returns the Telegram notification config for a project |
| 14 | +func GetTelegramConfig(w http.ResponseWriter, r *http.Request) { |
| 15 | + projectID := chi.URLParam(r, "id") |
| 16 | + user := middleware.GetUser(r) |
| 17 | + |
| 18 | + var project models.Project |
| 19 | + if err := database.DB.Where("id = ? AND user_id = ?", projectID, user.ID).First(&project).Error; err != nil { |
| 20 | + jsonError(w, "project not found", http.StatusNotFound) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + var cfg models.TelegramConfig |
| 25 | + database.DB.Where("project_id = ?", projectID).First(&cfg) |
| 26 | + jsonResponse(w, cfg, http.StatusOK) |
| 27 | +} |
| 28 | + |
| 29 | +// SaveTelegramConfig creates or updates Telegram config |
| 30 | +func SaveTelegramConfig(w http.ResponseWriter, r *http.Request) { |
| 31 | + projectID := chi.URLParam(r, "id") |
| 32 | + user := middleware.GetUser(r) |
| 33 | + |
| 34 | + var project models.Project |
| 35 | + if err := database.DB.Where("id = ? AND user_id = ?", projectID, user.ID).First(&project).Error; err != nil { |
| 36 | + jsonError(w, "project not found", http.StatusNotFound) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + var input struct { |
| 41 | + BotToken string `json:"bot_token"` |
| 42 | + ChatID string `json:"chat_id"` |
| 43 | + Enabled bool `json:"enabled"` |
| 44 | + NotifySignup bool `json:"notify_signup"` |
| 45 | + NotifyCoupon bool `json:"notify_coupon"` |
| 46 | + NotifyUnsubscribe bool `json:"notify_unsubscribe"` |
| 47 | + CampaignFilter string `json:"campaign_filter"` |
| 48 | + } |
| 49 | + if err := decodeJSON(r, &input); err != nil { |
| 50 | + jsonError(w, "invalid request", http.StatusBadRequest) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + var cfg models.TelegramConfig |
| 55 | + database.DB.Where("project_id = ?", projectID).First(&cfg) |
| 56 | + |
| 57 | + cfg.ProjectID = projectID |
| 58 | + cfg.BotToken = input.BotToken |
| 59 | + cfg.ChatID = input.ChatID |
| 60 | + cfg.Enabled = input.Enabled |
| 61 | + cfg.NotifySignup = input.NotifySignup |
| 62 | + cfg.NotifyCoupon = input.NotifyCoupon |
| 63 | + cfg.NotifyUnsubscribe = input.NotifyUnsubscribe |
| 64 | + cfg.CampaignFilter = input.CampaignFilter |
| 65 | + |
| 66 | + if cfg.ID == "" { |
| 67 | + database.DB.Create(&cfg) |
| 68 | + } else { |
| 69 | + database.DB.Save(&cfg) |
| 70 | + } |
| 71 | + |
| 72 | + jsonResponse(w, cfg, http.StatusOK) |
| 73 | +} |
| 74 | + |
| 75 | +// TestTelegramConfig sends a test message |
| 76 | +func TestTelegramConfig(w http.ResponseWriter, r *http.Request) { |
| 77 | + projectID := chi.URLParam(r, "id") |
| 78 | + user := middleware.GetUser(r) |
| 79 | + |
| 80 | + var project models.Project |
| 81 | + if err := database.DB.Where("id = ? AND user_id = ?", projectID, user.ID).First(&project).Error; err != nil { |
| 82 | + jsonError(w, "project not found", http.StatusNotFound) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + var cfg models.TelegramConfig |
| 87 | + if err := database.DB.Where("project_id = ?", projectID).First(&cfg).Error; err != nil { |
| 88 | + jsonError(w, "telegram not configured", http.StatusBadRequest) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + err := services.SendTelegramNotification(cfg.BotToken, cfg.ChatID, |
| 93 | + "✅ <b>Waitless Test</b>\n\nTelegram notifications are working for: "+project.Name) |
| 94 | + if err != nil { |
| 95 | + jsonError(w, "failed to send: "+err.Error(), http.StatusBadRequest) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + jsonResponse(w, map[string]string{"message": "test sent"}, http.StatusOK) |
| 100 | +} |
0 commit comments