feat(accounts): sync updated WhatsApp account configs to database
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)).
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user