feat(accounts): serialize enabled features

This commit is contained in:
2026-06-07 06:12:04 +08:00
parent 46eb7aafbd
commit ee9477ae58
3 changed files with 115 additions and 10 deletions
+66 -5
View File
@@ -461,13 +461,74 @@ func serializeAccountSettings(account *model.Account) map[string]any {
return settings
}
func parseAccountFeatures(raw string) map[string]any {
features := map[string]any{}
var chatwootDefaultEnabledAccountFeatures = []string{
"inbound_emails",
"channel_email",
"channel_facebook",
"help_center",
"agent_bots",
"macros",
"agent_management",
"team_management",
"inbox_management",
"labels",
"custom_attributes",
"automations",
"canned_responses",
"integrations",
"voice_recorder",
"channel_website",
"campaigns",
"reports",
"crm",
"auto_resolve_conversations",
"chatwoot_v4",
"contact_chatwoot_support_team",
"channel_instagram",
"channel_tiktok",
"assignment_v2",
"captain_tasks",
}
func parseAccountFeatures(raw string) map[string]bool {
features := defaultEnabledAccountFeatures()
if raw == "" {
return features
return onlyEnabledAccountFeatures(features)
}
if err := json.Unmarshal([]byte(raw), &features); err != nil {
return features
objectFlags := map[string]bool{}
if err := json.Unmarshal([]byte(raw), &objectFlags); err == nil {
for key, enabled := range objectFlags {
features[key] = enabled
}
return onlyEnabledAccountFeatures(features)
}
arrayFlags := []string{}
if err := json.Unmarshal([]byte(raw), &arrayFlags); err == nil {
for _, key := range arrayFlags {
features[key] = true
}
return onlyEnabledAccountFeatures(features)
}
return onlyEnabledAccountFeatures(features)
}
func defaultEnabledAccountFeatures() map[string]bool {
features := make(map[string]bool, len(chatwootDefaultEnabledAccountFeatures))
for _, key := range chatwootDefaultEnabledAccountFeatures {
features[key] = true
}
return features
}
func onlyEnabledAccountFeatures(features map[string]bool) map[string]bool {
enabled := map[string]bool{}
for key, value := range features {
if value {
enabled[key] = true
}
}
return enabled
}
@@ -199,6 +199,46 @@ func (s *AccountHandlerTestSuite) TestGet_Success() {
assert.Contains(s.T(), resp, "cache_keys")
}
func (s *AccountHandlerTestSuite) TestGet_FeaturesUseChatwootEnabledFeatures() {
acc := &model.Account{
Name: "Feature Account",
Active: true,
Status: "active",
FeatureFlags: `{"crm_v2":true,"sla":false,"crm":false}`,
}
s.Require().NoError(s.db.Create(acc).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/"+strconv.FormatUint(uint64(acc.ID), 10), nil)
s.router.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
resp := s.unmarshalResponse(w)
features := resp["features"].(map[string]interface{})
assert.Equal(s.T(), true, features["crm_v2"])
assert.Equal(s.T(), true, features["channel_email"])
assert.Equal(s.T(), true, features["assignment_v2"])
assert.NotContains(s.T(), features, "sla")
assert.NotContains(s.T(), features, "crm")
}
func (s *AccountHandlerTestSuite) TestGet_DefaultFeaturesMatchChatwootDefaults() {
acc := s.seedAccount("Default Feature Account")
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/"+strconv.FormatUint(uint64(acc.ID), 10), nil)
s.router.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
resp := s.unmarshalResponse(w)
features := resp["features"].(map[string]interface{})
assert.Equal(s.T(), true, features["crm"])
assert.Equal(s.T(), true, features["macros"])
assert.Equal(s.T(), true, features["captain_tasks"])
assert.NotContains(s.T(), features, "crm_v2")
assert.NotContains(s.T(), features, "sla")
}
func (s *AccountHandlerTestSuite) TestGet_NotFound() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/99999", nil)