From 80a4c2b70a96fb9efe4709b5ca01e368fb6d0874 Mon Sep 17 00:00:00 2001 From: Hein Date: Fri, 20 Feb 2026 23:10:46 +0200 Subject: [PATCH] feat(accounts): sync updated WhatsApp account configs to database --- pkg/handlers/accounts.go | 15 +++++++++++++++ pkg/storage/repository.go | 12 ++++++++++++ pkg/whatshooked/whatshooked.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/pkg/handlers/accounts.go b/pkg/handlers/accounts.go index 4ea90bc..55d4a69 100644 --- a/pkg/handlers/accounts.go +++ b/pkg/handlers/accounts.go @@ -7,6 +7,7 @@ import ( "git.warky.dev/wdevs/whatshooked/pkg/config" "git.warky.dev/wdevs/whatshooked/pkg/logging" + "git.warky.dev/wdevs/whatshooked/pkg/storage" ) // Accounts returns the list of all configured WhatsApp accounts @@ -264,6 +265,20 @@ func (h *Handlers) UpdateAccount(w http.ResponseWriter, r *http.Request) { return } + // Sync updated config to database + if db := storage.GetDB(); db != nil { + accountRepo := storage.NewWhatsAppAccountRepository(db) + cfgJSON := "" + if updates.BusinessAPI != nil { + if b, err := json.Marshal(updates.BusinessAPI); err == nil { + cfgJSON = string(b) + } + } + if err := accountRepo.UpdateConfig(context.Background(), updates.ID, updates.PhoneNumber, cfgJSON, !updates.Disabled); err != nil { + logging.Warn("Failed to sync updated account config to database", "account_id", updates.ID, "error", err) + } + } + // If the account was enabled and settings changed, reconnect it if !updates.Disabled { // Disconnect old connection diff --git a/pkg/storage/repository.go b/pkg/storage/repository.go index 035e1b6..d12cf3a 100644 --- a/pkg/storage/repository.go +++ b/pkg/storage/repository.go @@ -200,6 +200,18 @@ func (r *WhatsAppAccountRepository) GetByPhoneNumber(ctx context.Context, phoneN return &account, nil } +// UpdateConfig updates the config JSON column and phone number for a WhatsApp account +func (r *WhatsAppAccountRepository) UpdateConfig(ctx context.Context, id string, phoneNumber string, cfgJSON string, active bool) error { + _, err := r.db.NewUpdate().Model((*models.ModelPublicWhatsappAccount)(nil)). + Set("config = ?", cfgJSON). + Set("phone_number = ?", phoneNumber). + Set("active = ?", active). + Set("updated_at = ?", time.Now()). + Where("id = ?", id). + Exec(ctx) + return err +} + // UpdateStatus updates the status of a WhatsApp account func (r *WhatsAppAccountRepository) UpdateStatus(ctx context.Context, id string, status string) error { query := r.db.NewUpdate().Model((*models.ModelPublicWhatsappAccount)(nil)). diff --git a/pkg/whatshooked/whatshooked.go b/pkg/whatshooked/whatshooked.go index 8acc51c..0577127 100644 --- a/pkg/whatshooked/whatshooked.go +++ b/pkg/whatshooked/whatshooked.go @@ -90,7 +90,24 @@ func newWithConfig(cfg *config.Config, configPath string) (*WhatsHooked, error) configPath, func(updatedCfg *config.Config) error { if configPath != "" { - return config.Save(configPath, updatedCfg) + if err := config.Save(configPath, updatedCfg); err != nil { + return err + } + } + // Sync account configs to database + if db := storage.GetDB(); db != nil { + accountRepo := storage.NewWhatsAppAccountRepository(db) + for _, waCfg := range updatedCfg.WhatsApp { + cfgJSON := "" + if waCfg.BusinessAPI != nil { + if b, err := json.Marshal(waCfg.BusinessAPI); err == nil { + cfgJSON = string(b) + } + } + if err := accountRepo.UpdateConfig(context.Background(), waCfg.ID, waCfg.PhoneNumber, cfgJSON, !waCfg.Disabled); err != nil { + logging.Warn("Failed to sync account config to database", "account_id", waCfg.ID, "error", err) + } + } } return nil }, @@ -182,6 +199,16 @@ func (wh *WhatsHooked) connectFromDatabase(ctx context.Context) error { Disabled: !account.Active, } + // For business-api accounts, parse the Config JSON column to populate BusinessAPI + if waCfg.Type == "business-api" && account.Config.String() != "" { + var businessAPICfg config.BusinessAPIConfig + if err := json.Unmarshal([]byte(account.Config.String()), &businessAPICfg); err == nil { + waCfg.BusinessAPI = &businessAPICfg + } else { + logging.Warn("Failed to parse business API config from database", "account_id", accountID, "error", err) + } + } + if err := wh.whatsappMgr.Connect(ctx, waCfg); err != nil { logging.Error("Failed to connect to WhatsApp", "account_id", waCfg.ID, "error", err) // Continue connecting to other accounts even if one fails