feat(wxwork): update toUsers field type to int64 and enhance comments for clarity

This commit is contained in:
mlogclub
2026-04-21 17:57:53 +08:00
parent 22e1c2e395
commit beebf131c8
4 changed files with 69 additions and 9 deletions
+2 -1
View File
@@ -86,7 +86,8 @@ wxWork:
notify:
# 是否启用企业微信应用消息通知。
enabled: false
# 默认接收通知的成员ID列表;如业务目标用户已绑定企业微信身份,会优先发给目标用户。
# 默认接收通知的系统用户ID列表;如业务目标用户已绑定企业微信身份,会优先发给目标用户。
# 系统用户ID会通过企业微信身份绑定映射为企业微信成员ID后发送。
toUsers: []
# 是否发送保密消息。
safe: false
+1 -1
View File
@@ -21,7 +21,7 @@ type Config struct {
type WxWorkNotifyConfig struct {
Enabled bool `yaml:"enabled"`
ToUsers []string `yaml:"toUsers"`
ToUsers []int64 `yaml:"toUsers"`
Safe bool `yaml:"safe"`
EnableDuplicateCheck bool `yaml:"enableDuplicateCheck"`
DuplicateCheckInterval int `yaml:"duplicateCheckInterval"`
+1 -1
View File
@@ -107,7 +107,7 @@ func (s *wxWorkNotifyService) resolveToUsersByUserIDs(userIDs []int64) []string
func (s *wxWorkNotifyService) defaultToUsers() []string {
cfg := config.Current().WxWork.Notify
return arrs.Distinct(cfg.ToUsers)
return s.resolveToUsersByUserIDs(cfg.ToUsers)
}
func (s *wxWorkNotifyService) buildTextContent(title, body string) string {
@@ -2,8 +2,17 @@ package services
import (
"testing"
"time"
"cs-agent/internal/models"
"cs-agent/internal/pkg/config"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/repositories"
"github.com/glebarez/sqlite"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func TestWxWorkNotifyBuildTextContent(t *testing.T) {
@@ -15,18 +24,43 @@ func TestWxWorkNotifyBuildTextContent(t *testing.T) {
}
func TestWxWorkNotifyDefaultRecipients(t *testing.T) {
db := setupWxWorkNotifyTestDB(t)
config.SetCurrent(&config.Config{
WxWork: config.WxWorkConfig{
CorpID: "corp-1",
Notify: config.WxWorkNotifyConfig{
Enabled: true,
ToUsers: []string{"user_a", "user_a", "user_b"},
ToUsers: []int64{11, 11, 12},
},
},
})
now := time.Now()
for _, identity := range []*models.UserIdentity{
{
UserID: 11,
Provider: enums.ThirdProviderWxWork,
ProviderUserID: "wx_user_a",
ProviderCorpID: "corp-1",
Status: enums.StatusOk,
LastAuthAt: &now,
},
{
UserID: 12,
Provider: enums.ThirdProviderWxWork,
ProviderUserID: "wx_user_b",
ProviderCorpID: "corp-1",
Status: enums.StatusOk,
LastAuthAt: &now,
},
} {
if err := repositories.UserIdentityRepository.Create(db, identity); err != nil {
t.Fatalf("create user identity error = %v", err)
}
}
svc := newWxWorkNotifyService()
toUsers := svc.defaultToUsers()
if len(toUsers) != 2 || toUsers[0] != "user_a" || toUsers[1] != "user_b" {
if len(toUsers) != 2 || toUsers[0] != "wx_user_a" || toUsers[1] != "wx_user_b" {
t.Fatalf("unexpected users: %#v", toUsers)
}
}
@@ -43,3 +77,28 @@ func TestWxWorkNotifyNormalizeDuplicateCheckInterval(t *testing.T) {
t.Fatalf("expected interval 600, got %d", got)
}
}
func setupWxWorkNotifyTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "t_",
SingularTable: true,
},
})
if err != nil {
t.Fatalf("open sqlite error = %v", err)
}
t.Cleanup(func() {
sqlDB, err := db.DB()
if err == nil {
_ = sqlDB.Close()
}
})
if err := db.AutoMigrate(&models.UserIdentity{}); err != nil {
t.Fatalf("auto migrate error = %v", err)
}
sqls.SetDB(db)
return db
}