Server qr fixes.
This commit is contained in:
@@ -38,10 +38,63 @@ func (h *Handlers) AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||
h.config.WhatsApp = append(h.config.WhatsApp, account)
|
||||
if h.configPath != "" {
|
||||
if err := config.Save(h.configPath, h.config); err != nil {
|
||||
logging.Error("Failed to save config", "error", err)
|
||||
logging.Error("Failed to save config after adding account", "account_id", account.ID, "error", err)
|
||||
} else {
|
||||
logging.Info("Config saved after adding account", "account_id", account.ID)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, map[string]string{"status": "ok", "account_id": account.ID})
|
||||
}
|
||||
|
||||
// RemoveAccount removes a WhatsApp account from the system
|
||||
func (h *Handlers) RemoveAccount(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Disconnect the account
|
||||
if err := h.whatsappMgr.Disconnect(req.ID); err != nil {
|
||||
logging.Warn("Failed to disconnect account during removal", "account_id", req.ID, "error", err)
|
||||
// Continue with removal even if disconnect fails
|
||||
}
|
||||
|
||||
// Remove from config
|
||||
found := false
|
||||
newAccounts := make([]config.WhatsAppConfig, 0)
|
||||
for _, acc := range h.config.WhatsApp {
|
||||
if acc.ID != req.ID {
|
||||
newAccounts = append(newAccounts, acc)
|
||||
} else {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
http.Error(w, "Account not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
h.config.WhatsApp = newAccounts
|
||||
|
||||
// Save config
|
||||
if h.configPath != "" {
|
||||
if err := config.Save(h.configPath, h.config); err != nil {
|
||||
logging.Error("Failed to save config after removing account", "account_id", req.ID, "error", err)
|
||||
} else {
|
||||
logging.Info("Config saved after removing account", "account_id", req.ID)
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]string{"status": "ok"})
|
||||
}
|
||||
|
||||
@@ -34,12 +34,14 @@ func (h *Handlers) AddHook(w http.ResponseWriter, r *http.Request) {
|
||||
h.config.Hooks = h.hookMgr.ListHooks()
|
||||
if h.configPath != "" {
|
||||
if err := config.Save(h.configPath, h.config); err != nil {
|
||||
logging.Error("Failed to save config", "error", err)
|
||||
logging.Error("Failed to save config after adding hook", "hook_id", hook.ID, "error", err)
|
||||
} else {
|
||||
logging.Info("Config saved after adding hook", "hook_id", hook.ID)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, map[string]string{"status": "ok"})
|
||||
writeJSON(w, map[string]string{"status": "ok", "hook_id": hook.ID})
|
||||
}
|
||||
|
||||
// RemoveHook removes a hook from the system
|
||||
@@ -66,7 +68,9 @@ func (h *Handlers) RemoveHook(w http.ResponseWriter, r *http.Request) {
|
||||
h.config.Hooks = h.hookMgr.ListHooks()
|
||||
if h.configPath != "" {
|
||||
if err := config.Save(h.configPath, h.config); err != nil {
|
||||
logging.Error("Failed to save config", "error", err)
|
||||
logging.Error("Failed to save config after removing hook", "hook_id", req.ID, "error", err)
|
||||
} else {
|
||||
logging.Info("Config saved after removing hook", "hook_id", req.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user