feat(help-center): queue article translations

This commit is contained in:
2026-06-06 05:15:48 +08:00
parent 76604353f8
commit 5a3da22a21
7 changed files with 604 additions and 10 deletions
+23 -1
View File
@@ -386,7 +386,25 @@ func (h *ArticleHandler) BulkDelete(c *gin.Context) {
}
func (h *ArticleHandler) BulkTranslate(c *gin.Context) {
c.Status(http.StatusNotImplemented)
portal, ok := h.resolvePortalForRequest(c)
if !ok {
return
}
var req service.BulkTranslateRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
if err := h.svc.BulkTranslate(c.Request.Context(), portal.AccountID, portal, currentUserID(c), req); err != nil {
var conflict *service.ArticleTranslationConflictError
if errors.As(err, &conflict) {
c.JSON(http.StatusConflict, gin.H{"duplicate_articles": conflict.Duplicates})
return
}
renderArticleBulkError(c, err)
return
}
c.Status(http.StatusOK)
}
// BulkActions performs a bulk operation (publish/archive/delete) on multiple articles.
@@ -595,6 +613,10 @@ func renderArticleBulkError(c *gin.Context, err error) {
message = "Invalid status"
case errors.Is(err, service.ErrArticleBulkCategoryNotFound):
message = "Category not found"
case errors.Is(err, service.ErrArticleBulkCaptainNotAvailable):
message = "Captain is not available"
case errors.Is(err, service.ErrArticleBulkLocaleNotAvailable):
message = "Locale is not available"
default:
message = err.Error()
}
@@ -12,6 +12,7 @@ import (
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/internal/worker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/driver/sqlite"
@@ -38,11 +39,13 @@ func (s *ArticleHandlerTestSuite) SetupSuite() {
&model.Account{}, &model.User{}, &model.Portal{},
&model.Category{}, &model.Folder{}, &model.Article{},
&model.PortalMember{},
&model.BackgroundJob{},
))
s.db = db
repo := repository.NewArticleRepo(db)
svc := service.NewArticleService(repo)
svc.SetWorkerPool(worker.NewWorkerPool(db))
portalRepo := repository.NewPortalRepo(db)
s.handler = NewArticleHandler(svc, service.NewPortalService(portalRepo))
@@ -387,6 +390,71 @@ func (s *ArticleHandlerTestSuite) TestBulkActions_FrontendRoutes() {
assert.Equal(s.T(), http.StatusOK, w.Code)
}
func (s *ArticleHandlerTestSuite) TestBulkTranslate_FrontendRouteQueuesAndReturnsConflict() {
account := &model.Account{Name: "translate-account", FeatureFlags: `{"captain_tasks":true}`}
s.Require().NoError(s.db.Create(account).Error)
portal := &model.Portal{AccountID: account.ID, Name: "translate-portal", Slug: "translate-portal", Locale: "en", PortalConfiguration: json.RawMessage(`{"allowed_locales":["en","fr"]}`)}
s.Require().NoError(s.db.Create(portal).Error)
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "French", Slug: "french", Locale: "fr"}
s.Require().NoError(s.db.Create(category).Error)
article := &model.Article{AccountID: account.ID, PortalID: portal.ID, Title: "Returns", Slug: "returns-translate", Status: "published", Locale: "en"}
s.Require().NoError(s.db.Create(article).Error)
duplicate := &model.Article{AccountID: account.ID, PortalID: portal.ID, Title: "Retours", Slug: "retours-translate", Status: "draft", Locale: "fr", AssociatedArticleID: &article.ID}
s.Require().NoError(s.db.Create(duplicate).Error)
r := gin.New()
r.POST("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/translate", s.handler.BulkTranslate)
body := fmt.Sprintf(`{"ids":[%d],"locale":"fr","category_id":%d}`, article.ID, category.ID)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/translate-portal/articles/bulk_actions/translate", account.ID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusConflict, w.Code)
var conflict map[string][]map[string]interface{}
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &conflict))
s.Require().Len(conflict["duplicate_articles"], 1)
assert.Equal(s.T(), float64(duplicate.ID), conflict["duplicate_articles"][0]["id"])
body = fmt.Sprintf(`{"ids":[%d],"locale":"fr","category_id":%d,"force":true}`, article.ID, category.ID)
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/translate-portal/articles/bulk_actions/translate", account.ID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
assert.Empty(s.T(), w.Body.String())
var jobs int64
s.Require().NoError(s.db.Model(&model.BackgroundJob{}).Where("job_type = ? AND queue = ?", service.TaskTypeCaptainArticleTranslate, "low").Count(&jobs).Error)
assert.GreaterOrEqual(s.T(), jobs, int64(1))
}
func (s *ArticleHandlerTestSuite) TestBulkTranslate_ValidatesCaptainLocaleAndCategory() {
account := &model.Account{Name: "translate-invalid-account", FeatureFlags: `{"captain_tasks":true}`}
s.Require().NoError(s.db.Create(account).Error)
portal := &model.Portal{AccountID: account.ID, Name: "translate-invalid", Slug: "translate-invalid", Locale: "en", PortalConfiguration: json.RawMessage(`{"allowed_locales":["en","fr"]}`)}
s.Require().NoError(s.db.Create(portal).Error)
article := &model.Article{AccountID: account.ID, PortalID: portal.ID, Title: "Billing", Slug: "billing-translate-invalid", Status: "published", Locale: "en"}
s.Require().NoError(s.db.Create(article).Error)
r := gin.New()
r.POST("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/translate", s.handler.BulkTranslate)
w := httptest.NewRecorder()
body := fmt.Sprintf(`{"ids":[%d],"locale":"es"}`, article.ID)
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/translate-invalid/articles/bulk_actions/translate", account.ID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusUnprocessableEntity, w.Code)
w = httptest.NewRecorder()
body = fmt.Sprintf(`{"ids":[%d],"locale":"fr","category_id":999999}`, article.ID)
req, _ = http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/translate-invalid/articles/bulk_actions/translate", account.ID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusUnprocessableEntity, w.Code)
}
func (s *ArticleHandlerTestSuite) TestBulkUpdateStatus_InvalidStatusReturnsChatwootError() {
article := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "bulk-invalid", Slug: "bulk-invalid", Status: "draft"}
s.Require().NoError(s.db.Create(article).Error)