feat(inboxes): queue template sync jobs

This commit is contained in:
2026-06-05 22:40:35 +08:00
parent af9e1eb711
commit 9db133697e
6 changed files with 168 additions and 43 deletions
+6 -2
View File
@@ -656,13 +656,17 @@ func (h *InboxHandler) SyncTemplates(c *gin.Context) {
return
}
templates, svcErr := h.svc.SyncTemplates(c.Request.Context(), accountID, inboxID)
svcErr := h.svc.SyncTemplates(c.Request.Context(), accountID, inboxID)
if svcErr != nil {
if errors.Is(svcErr, service.ErrInboxTemplateSyncWhatsAppOnly) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": service.InboxTemplateSyncWhatsAppOnlyMessage})
return
}
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, gin.H{"templates": templates})
c.JSON(http.StatusOK, gin.H{"message": service.InboxTemplateSyncInitiatedMessage})
}
// RegisterWebhook registers a webhook URL with the channel provider for an inbox.
@@ -1,6 +1,7 @@
package v1
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -8,8 +9,17 @@ import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
whatsappchannel "github.com/gochat/gochat/internal/channel/whatsapp"
"github.com/gochat/gochat/internal/model"
channelmodel "github.com/gochat/gochat/internal/model/channel"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/internal/worker"
)
// setupInboxMemberActionRouter creates a test router with inbox member-action routes.
@@ -123,6 +133,39 @@ func TestInboxSyncTemplates_BadInboxID(t *testing.T) {
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
}
func TestInboxSyncTemplates_ChatwootQueuedResponse(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:inbox_sync_templates_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{}, &model.BackgroundJob{}))
account := &model.Account{Name: "Sync Templates", Locale: "en", Active: 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)
wp := worker.NewWorkerPool(db)
svc := service.NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, whatsappchannel.NewRepository(db))
svc.SetWorkerPool(wp)
handler := NewInboxHandler(svc)
router := setupInboxMemberActionRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/inboxes/1/sync_templates", bytes.NewReader(nil))
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
var body map[string]any
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
assert.Equal(t, service.InboxTemplateSyncInitiatedMessage, body["message"])
assert.NotContains(t, body, "templates")
}
// ========================================
// RegisterWebhook — param validation tests
// ========================================