From 194f682df29ed763e436b0f18fc9afea3f88363c Mon Sep 17 00:00:00 2001 From: pablomendezroyo Date: Wed, 11 Dec 2024 16:12:04 +0100 Subject: [PATCH] send telegram config update notification --- cmd/main.go | 2 +- internal/adapters/api/api_adapter.go | 17 +++++++++-------- internal/adapters/notifier/notifier_adapter.go | 9 --------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index aee7d64..56e67f1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -54,7 +54,7 @@ func main() { logger.Fatal("Failed to initialize relaysAllowedAdapter: %v", err) } - apiAdapter := api.NewAPIAdapter(ctx, storageAdapter, relaysUsedAdapter, relaysAllowedAdapter, networkConfig.CORS) + apiAdapter := api.NewAPIAdapter(ctx, storageAdapter, notifierAdapter, relaysUsedAdapter, relaysAllowedAdapter, networkConfig.CORS) proxyApiAdapter := proxyapi.NewProxyAPIAdapter(networkConfig.CORS, networkConfig.LidoKeysApiUrl) // Initialize API services diff --git a/internal/adapters/api/api_adapter.go b/internal/adapters/api/api_adapter.go index d62783e..a199687 100644 --- a/internal/adapters/api/api_adapter.go +++ b/internal/adapters/api/api_adapter.go @@ -34,10 +34,11 @@ func (h *APIHandler) GetRouter() http.Handler { } // NewAPIAdapter initializes the APIHandler and sets up routes with CORS enabled -func NewAPIAdapter(ctx context.Context, storagePort ports.StoragePort, relaysUsedPort ports.RelaysUsedPort, relaysAllowedPort ports.RelaysAllowedPort, allowedOrigins []string) *APIHandler { +func NewAPIAdapter(ctx context.Context, storagePort ports.StoragePort, notifierPort ports.NotifierPort, relaysUsedPort ports.RelaysUsedPort, relaysAllowedPort ports.RelaysAllowedPort, allowedOrigins []string) *APIHandler { h := &APIHandler{ ctx: ctx, StoragePort: storagePort, + NotifierPort: notifierPort, RelaysUsedPort: relaysUsedPort, RelaysAllowedPort: relaysAllowedPort, Router: mux.NewRouter(), @@ -169,19 +170,19 @@ func (h *APIHandler) UpdateTelegramConfig(w http.ResponseWriter, r *http.Request return } - // send test notification to verify the chat id exists - if err := h.NotifierPort.SendNotification("🙋 Test notification"); err != nil { - logger.ErrorWithPrefix("API", "Failed to send test notification: %v", err) - writeErrorResponse(w, "Failed to send test notification", http.StatusInternalServerError) - return - } - if err := h.StoragePort.SaveTelegramConfig(domain.TelegramConfig(req)); err != nil { logger.ErrorWithPrefix("API", "Failed to update Telegram configuration: %v", err) writeErrorResponse(w, "Failed to update Telegram configuration", http.StatusInternalServerError) return } + // send update notification to verify the chat id exists + if err := h.NotifierPort.SendNotification("🔑 Updated telegram configuration successfully"); err != nil { + logger.ErrorWithPrefix("API", "Failed to send test notification: %v", err) + writeErrorResponse(w, "Failed to send test notification", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) } diff --git a/internal/adapters/notifier/notifier_adapter.go b/internal/adapters/notifier/notifier_adapter.go index ccd1a1c..2c3978f 100644 --- a/internal/adapters/notifier/notifier_adapter.go +++ b/internal/adapters/notifier/notifier_adapter.go @@ -51,15 +51,6 @@ func NewNotifierAdapter(ctx context.Context, storageAdapter ports.StoragePort) ( if err == nil { adapter.Bot = updatedBot logger.InfoWithPrefix(servicePrefix, "Telegram bot configuration updated successfully.") - - // Send a test notification to confirm the new configuration works - testMessage := "🔑 Updated telegram configuration successfully" - testErr := adapter.SendNotification(testMessage) - if testErr != nil { - logger.ErrorWithPrefix(servicePrefix, "Failed to send test notification after configuration update: %v", testErr) - } else { - logger.InfoWithPrefix(servicePrefix, "Test notification sent successfully.") - } } else { logger.ErrorWithPrefix(servicePrefix, "Failed to update Telegram bot: %v", err) adapter.Bot = nil // Disable notifications on failure