This commit is contained in:
74
internal/idms/users.go
Normal file
74
internal/idms/users.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package idms
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"clintonambulance.com/calculate_negative_points/internal/config"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// FlexibleString handles JSON values that can be either strings or numbers
|
||||
type FlexibleString string
|
||||
|
||||
func (f *FlexibleString) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err == nil {
|
||||
*f = FlexibleString(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
var n json.Number
|
||||
if err := json.Unmarshal(data, &n); err == nil {
|
||||
*f = FlexibleString(n.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot unmarshal %s into FlexibleString", data)
|
||||
}
|
||||
|
||||
type IdmsUserAttributes struct {
|
||||
Cars bool `json:"cars"`
|
||||
EmployeeId FlexibleString `json:"employee_id"`
|
||||
}
|
||||
|
||||
type IdmsUser struct {
|
||||
Attributes IdmsUserAttributes `json:"attributes"`
|
||||
Groups []string `json:"groups"`
|
||||
Id string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func (u IdmsUser) FirstName() string {
|
||||
parts := strings.Fields(u.Name)
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
}
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
func (u IdmsUser) LastName() string {
|
||||
parts := strings.Fields(u.Name)
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
}
|
||||
return parts[len(parts)-1]
|
||||
}
|
||||
|
||||
func GetCarsMembers(config *config.ApplicationConfig) []IdmsUser {
|
||||
req := NewRequest[IdmsUser]("/api/v3/core/users/?is_active=true", Get)
|
||||
results, _ := req.Perform(config, map[string]string{}, map[string]string{})
|
||||
|
||||
filtered := lo.Filter(results, func(u IdmsUser, _ int) bool {
|
||||
return lo.Contains(u.Groups, config.Idms.CarsGroupId)
|
||||
})
|
||||
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
return filtered[i].LastName() < filtered[j].LastName()
|
||||
})
|
||||
|
||||
return filtered
|
||||
}
|
||||
Reference in New Issue
Block a user