Skip to content

Commit

Permalink
sync: backend: refact user sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Nov 8, 2023
1 parent aaf538b commit 8d5182e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
39 changes: 39 additions & 0 deletions internal/pkg/feishu/User.go
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
package feishu

import (
"github.com/ncuhome/GeniusAuthoritarian/pkg/feishuApi"
)

type UserSync interface {
IsInvalid() bool
Departments(groupMap map[string]uint) []uint
}

func NewUser(data *feishuApi.User) *User {
return &User{
data: data,
}
}

type User struct {
data *feishuApi.User
departments []uint
}

func (u User) IsInvalid() bool {
return !u.data.Status.IsActivated || u.data.Status.IsFrozen || u.data.Status.IsResigned || u.data.Mobile == "" || // 账号未异常
u.data.EmployeeType != 1 // 仅允许正式员工状态账号
}

func (u User) Departments(groupMap map[string]uint) []uint {
var departments = make([]uint, len(u.data.DepartmentIds))
var validLength int
for _, groupOpenID := range u.data.DepartmentIds {
id, ok := groupMap[groupOpenID]
if !ok {
continue
}
departments[validLength] = id
validLength++
}
return departments[:validLength]
}
File renamed without changes.
26 changes: 7 additions & 19 deletions pkg/feishuApi/feishuApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ func (f Fs) doLoadUserListRequest(departmentID, pageToken string, pageSize uint)
}
var data ListUserResp
query := map[string]interface{}{
"department_id": departmentID,
"page_size": pageSize,
"page_size": pageSize,
}
if departmentID != "" {
query["department_id"] = departmentID
}
if pageToken != "" {
query["page_token"] = pageToken
Expand All @@ -144,6 +146,7 @@ func (f Fs) doLoadUserListRequest(departmentID, pageToken string, pageSize uint)
Query: query,
})
}

func (f Fs) doLoadAllUserListRequest(departmentID string) ([]User, error) {
const pageSize = 99
var r []User
Expand All @@ -162,21 +165,6 @@ func (f Fs) doLoadAllUserListRequest(departmentID string) ([]User, error) {
return r, nil
}

// LoadUserList map key 为飞书部门 OpenID
func (f Fs) LoadUserList() (map[string][]User, error) {
departments, err := f.LoadDepartmentList()
if err != nil {
return nil, err
}

result := make(map[string][]User, len(departments.Items))
for _, department := range departments.Items {
var list []User
list, err = f.doLoadAllUserListRequest(department.OpenDepartmentId)
if err != nil {
return nil, err
}
result[department.OpenDepartmentId] = list
}
return result, nil
func (f Fs) LoadUserList() ([]User, error) {
return f.doLoadAllUserListRequest("")
}

0 comments on commit 8d5182e

Please sign in to comment.