From beebf131c87d19838b37adaf910c491251431c9c Mon Sep 17 00:00:00 2001 From: mlogclub Date: Tue, 21 Apr 2026 17:57:53 +0800 Subject: [PATCH] feat(wxwork): update toUsers field type to int64 and enhance comments for clarity --- config/config.example.yaml | 3 +- internal/pkg/config/config.go | 10 +-- internal/services/wxwork_notify_service.go | 2 +- .../services/wxwork_notify_service_test.go | 63 ++++++++++++++++++- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/config/config.example.yaml b/config/config.example.yaml index 15a7b62..2dd6e81 100644 --- a/config/config.example.yaml +++ b/config/config.example.yaml @@ -86,7 +86,8 @@ wxWork: notify: # 是否启用企业微信应用消息通知。 enabled: false - # 默认接收通知的成员ID列表;如业务目标用户已绑定企业微信身份,会优先发给目标用户。 + # 默认接收通知的系统用户ID列表;如业务目标用户已绑定企业微信身份,会优先发给目标用户。 + # 系统用户ID会通过企业微信身份绑定映射为企业微信成员ID后发送。 toUsers: [] # 是否发送保密消息。 safe: false diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 9e29364..049f04a 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -20,11 +20,11 @@ type Config struct { } type WxWorkNotifyConfig struct { - Enabled bool `yaml:"enabled"` - ToUsers []string `yaml:"toUsers"` - Safe bool `yaml:"safe"` - EnableDuplicateCheck bool `yaml:"enableDuplicateCheck"` - DuplicateCheckInterval int `yaml:"duplicateCheckInterval"` + Enabled bool `yaml:"enabled"` + ToUsers []int64 `yaml:"toUsers"` + Safe bool `yaml:"safe"` + EnableDuplicateCheck bool `yaml:"enableDuplicateCheck"` + DuplicateCheckInterval int `yaml:"duplicateCheckInterval"` } type ServerConfig struct { diff --git a/internal/services/wxwork_notify_service.go b/internal/services/wxwork_notify_service.go index 4419fbe..c994660 100644 --- a/internal/services/wxwork_notify_service.go +++ b/internal/services/wxwork_notify_service.go @@ -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 { diff --git a/internal/services/wxwork_notify_service_test.go b/internal/services/wxwork_notify_service_test.go index 88f0f68..5957900 100644 --- a/internal/services/wxwork_notify_service_test.go +++ b/internal/services/wxwork_notify_service_test.go @@ -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 +}