feat(channels): align whatsapp calling toggles
This commit is contained in:
@@ -749,6 +749,60 @@ func (h *InboxHandler) RegisterWebhook(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Webhook registered successfully"})
|
||||
}
|
||||
|
||||
// EnableWhatsAppCalling enables WhatsApp Calling for a Cloud API inbox.
|
||||
// POST /api/v1/accounts/:id/inboxes/:inbox_id/enable_whatsapp_calling
|
||||
// Reference: Enterprise::Api::V1::Accounts::InboxesController#enable_whatsapp_calling
|
||||
func (h *InboxHandler) EnableWhatsAppCalling(c *gin.Context) {
|
||||
h.handleWhatsAppCallingToggle(c, true)
|
||||
}
|
||||
|
||||
// DisableWhatsAppCalling disables WhatsApp Calling for a Cloud API inbox.
|
||||
// POST /api/v1/accounts/:id/inboxes/:inbox_id/disable_whatsapp_calling
|
||||
// Reference: Enterprise::Api::V1::Accounts::InboxesController#disable_whatsapp_calling
|
||||
func (h *InboxHandler) DisableWhatsAppCalling(c *gin.Context) {
|
||||
h.handleWhatsAppCallingToggle(c, false)
|
||||
}
|
||||
|
||||
func (h *InboxHandler) handleWhatsAppCallingToggle(c *gin.Context, enable bool) {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account id"})
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := parseUintParam(c, "inbox_id")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox id"})
|
||||
return
|
||||
}
|
||||
|
||||
if !h.svc.Ready() {
|
||||
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrInternal, "failed to update WhatsApp calling")
|
||||
return
|
||||
}
|
||||
|
||||
var svcErr error
|
||||
if enable {
|
||||
svcErr = h.svc.EnableWhatsAppCalling(c.Request.Context(), accountID, inboxID)
|
||||
} else {
|
||||
svcErr = h.svc.DisableWhatsAppCalling(c.Request.Context(), accountID, inboxID)
|
||||
}
|
||||
if svcErr != nil {
|
||||
if errors.Is(svcErr, service.ErrInboxWhatsAppCallingUnsupported) || errors.Is(svcErr, service.ErrInboxWhatsAppCallingFeatureRequired) {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": svcErr.Error()})
|
||||
return
|
||||
}
|
||||
if strings.Contains(strings.ToLower(svcErr.Error()), "not found") {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": svcErr.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// GetAgentBot retrieves the currently active agent bot for an inbox.
|
||||
// GET /api/v1/accounts/:id/inboxes/:inbox_id/agent_bot
|
||||
// Reference: Chatwoot InboxesController#agent_bot
|
||||
|
||||
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -32,6 +33,8 @@ func setupInboxMemberActionRouter(handler *InboxHandler) *gin.Engine {
|
||||
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/health", handler.Health)
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/sync_templates", handler.SyncTemplates)
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/register_webhook", handler.RegisterWebhook)
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/enable_whatsapp_calling", handler.EnableWhatsAppCalling)
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/disable_whatsapp_calling", handler.DisableWhatsAppCalling)
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -39,6 +42,28 @@ func newNilInboxHandler() *InboxHandler {
|
||||
return NewInboxHandler(&service.InboxService{})
|
||||
}
|
||||
|
||||
type fakeInboxHandlerWhatsAppService struct{}
|
||||
|
||||
func (f *fakeInboxHandlerWhatsAppService) FetchMessageTemplates(context.Context, *channelmodel.ChannelWhatsApp) ([]interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeInboxHandlerWhatsAppService) FetchHealthStatus(context.Context, *channelmodel.ChannelWhatsApp) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeInboxHandlerWhatsAppService) SetupWebhook(context.Context, *channelmodel.ChannelWhatsApp, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeInboxHandlerWhatsAppService) SetupWebhookFields(context.Context, *channelmodel.ChannelWhatsApp, string, []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeInboxHandlerWhatsAppService) UpdateCallingStatus(context.Context, *channelmodel.ChannelWhatsApp, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseJSONResponse extracts the "error" key from a JSON response body.
|
||||
func parseJSONError(body []byte) string {
|
||||
var resp map[string]interface{}
|
||||
@@ -193,3 +218,58 @@ func TestInboxRegisterWebhook_BadInboxID(t *testing.T) {
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// WhatsApp calling — param and response parity tests
|
||||
// ========================================
|
||||
|
||||
func TestInboxWhatsAppCalling_BadParams(t *testing.T) {
|
||||
handler := newNilInboxHandler()
|
||||
router := setupInboxMemberActionRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/inboxes/5/enable_whatsapp_calling", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid account id")
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("POST", "/api/v1/accounts/1/inboxes/xyz/disable_whatsapp_calling", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
||||
}
|
||||
|
||||
func TestInboxWhatsAppCalling_EnableDisableReturnEmptyOK(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:inbox_whatsapp_calling_handler?mode=memory&cache=shared"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
sqlDB, dbErr := db.DB()
|
||||
if dbErr == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
})
|
||||
require.NoError(t, db.AutoMigrate(&model.Account{}, &model.Inbox{}, &channelmodel.ChannelWhatsApp{}))
|
||||
account := &model.Account{Name: "Calling", Locale: "en", Active: true, FeatureFlags: `{"channel_voice":true}`}
|
||||
require.NoError(t, db.Create(account).Error)
|
||||
inbox := &model.Inbox{AccountID: account.ID, Name: "WhatsApp", ChannelType: "whatsapp", ChannelID: 1}
|
||||
require.NoError(t, db.Create(inbox).Error)
|
||||
channel := &channelmodel.ChannelWhatsApp{AccountID: account.ID, InboxID: inbox.ID, PhoneNumber: "+1555010000", PhoneNumberID: "phone-1", BusinessAccountID: "waba-1", AccessToken: "token", Provider: "whatsapp_cloud"}
|
||||
require.NoError(t, db.Create(channel).Error)
|
||||
|
||||
svc := service.NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, &fakeInboxHandlerWhatsAppService{}, whatsappchannel.NewRepository(db))
|
||||
handler := NewInboxHandler(svc)
|
||||
router := setupInboxMemberActionRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/inboxes/1/enable_whatsapp_calling", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
assert.Empty(t, w.Body.String())
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("POST", "/api/v1/accounts/1/inboxes/1/disable_whatsapp_calling", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
assert.Empty(t, w.Body.String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user