feat(api): add phone number registration endpoint and update related logic
This commit is contained in:
@@ -51,7 +51,10 @@ func (m *Manager) Connect(ctx context.Context, cfg config.WhatsAppConfig) error
|
||||
// Factory pattern based on type
|
||||
switch cfg.Type {
|
||||
case "business-api":
|
||||
client, err = businessapi.NewClient(cfg, m.eventBus, m.mediaConfig)
|
||||
onWABAResolved := func(accountID, wabaID string) {
|
||||
m.updateWABAInConfig(accountID, wabaID)
|
||||
}
|
||||
client, err = businessapi.NewClient(cfg, m.eventBus, m.mediaConfig, onWABAResolved)
|
||||
case "whatsmeow", "":
|
||||
// Create callback for phone number updates
|
||||
onPhoneUpdate := func(accountID, phoneNumber string) {
|
||||
@@ -187,6 +190,32 @@ func (m *Manager) GetClient(id string) (Client, bool) {
|
||||
return client, exists
|
||||
}
|
||||
|
||||
// updateWABAInConfig stores the resolved WABA ID back into the in-memory config and syncs to DB/file
|
||||
func (m *Manager) updateWABAInConfig(accountID, wabaID string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
for i := range m.config.WhatsApp {
|
||||
if m.config.WhatsApp[i].ID == accountID {
|
||||
if m.config.WhatsApp[i].BusinessAPI == nil {
|
||||
return
|
||||
}
|
||||
if m.config.WhatsApp[i].BusinessAPI.WABAId == wabaID {
|
||||
return // already up to date
|
||||
}
|
||||
m.config.WhatsApp[i].BusinessAPI.WABAId = wabaID
|
||||
logging.Info("Updated WABA ID in config", "account_id", accountID, "waba_id", wabaID)
|
||||
|
||||
if m.onConfigUpdate != nil {
|
||||
if err := m.onConfigUpdate(m.config); err != nil {
|
||||
logging.Error("Failed to save config after WABA ID update", "account_id", accountID, "error", err)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updatePhoneNumberInConfig updates the phone number for an account in config and saves it
|
||||
func (m *Manager) updatePhoneNumberInConfig(accountID, phoneNumber string) {
|
||||
m.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user