diff --git a/pachca.go b/pachca.go index 1982d86..00e744a 100644 --- a/pachca.go +++ b/pachca.go @@ -14,7 +14,6 @@ import ( "mime/multipart" "os" "regexp" - "slices" "strconv" "strings" "time" @@ -321,20 +320,29 @@ type ChatFilter struct { // UserRequest is a struct with information needed to create or modify a user type UserRequest struct { - Email string `json:"email"` - FirstName string `json:"first_name,omitempty"` - LastName string `json:"last_name,omitempty"` - Nickname string `json:"nickname,omitempty"` - Role UserRole `json:"role,omitempty"` - PhoneNumber string `json:"phone_number,omitempty"` - Title string `json:"title,omitempty"` - Department string `json:"department,omitempty"` - Properties Properties `json:"custom_properties,omitempty"` - Tags []string `json:"list_tags,omitempty"` - IsSuspended bool `json:"suspended,omitempty"` - SkipEmailNotify bool `json:"skip_email_notify,omitempty"` + Email string `json:"email"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Nickname string `json:"nickname,omitempty"` + Role UserRole `json:"role,omitempty"` + PhoneNumber string `json:"phone_number,omitempty"` + Title string `json:"title,omitempty"` + Department string `json:"department,omitempty"` + Properties PropertyRequests `json:"custom_properties,omitempty"` + Tags []string `json:"list_tags,omitempty"` + IsSuspended bool `json:"suspended,omitempty"` + SkipEmailNotify bool `json:"skip_email_notify,omitempty"` } +// PropertyRequest is a struct with property info +type PropertyRequest struct { + ID uint64 `json:"id"` + Value string `json:"value"` +} + +// PropertyRequests is a slice with properties requests +type PropertyRequests []*PropertyRequest + // ChatRequest is a struct with information needed to create or modify a chat type ChatRequest struct { Name string `json:"name"` @@ -444,6 +452,30 @@ func NewClient(token string) (*Client, error) { }, nil } +// NewPropertyRequest creates new custom property +func NewPropertyRequest(id uint64, value any) *PropertyRequest { + var v string + + switch t := value.(type) { + case time.Time: + v = formatDate(t.UTC()) + + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + v = fmt.Sprintf("%d", value) + + case float32: + v = fmt.Sprintf("%d", int64(t)) + + case float64: + v = fmt.Sprintf("%d", int64(t)) + + default: + v = fmt.Sprintf("%v", value) + } + + return &PropertyRequest{ID: id, Value: v} +} + // ValidateToken validates API access token func ValidateToken(token string) error { switch { @@ -1588,8 +1620,10 @@ func (p Properties) HasAny(name ...string) bool { // Find returns custom property with given name func (p Properties) Find(name string) *Property { + name = strings.ToLower(name) + for _, pp := range p { - if pp.Name == name { + if strings.ToLower(pp.Name) == name { return pp } } @@ -1599,9 +1633,11 @@ func (p Properties) Find(name string) *Property { // FindAny returns first found property with one of given names func (p Properties) FindAny(name ...string) *Property { - for _, pp := range p { - if slices.Contains(name, pp.Name) { - return pp + for _, n := range name { + p := p.Find(n) + + if p != nil { + return p } } @@ -1764,8 +1800,11 @@ func (u Users) InChat(chat *Chat) Users { // Find returns user with given nickname or email func (u Users) Find(nicknameOrEmail string) *User { + nicknameOrEmail = strings.ToLower(nicknameOrEmail) + for _, uu := range u { - if uu.Nickname == nicknameOrEmail || uu.Email == nicknameOrEmail { + if strings.ToLower(uu.Nickname) == nicknameOrEmail || + strings.ToLower(uu.Email) == nicknameOrEmail { return uu } } @@ -1877,8 +1916,10 @@ func (c Chats) Get(id uint64) *Chat { // Find returns chat with given name func (c Chats) Find(name string) *Chat { + name = strings.ToLower(name) + for _, cc := range c { - if cc.Name == name { + if strings.ToLower(cc.Name) == name { return cc } } @@ -1949,6 +1990,30 @@ func (t Tags) Get(id uint64) *Tag { return nil } +// Find returns tag with given name +func (t Tags) Find(name string) *Tag { + name = strings.ToLower(name) + + for _, tt := range t { + if strings.ToLower(tt.Name) == name { + return tt + } + } + + return nil +} + +// Names returns names of all tags +func (t Tags) Names() []string { + var result []string + + for _, tt := range t { + result = append(result, tt.Name) + } + + return result +} + // InChat only returns tags that are present in the given chat func (t Tags) InChat(chat *Chat) Tags { if chat == nil { diff --git a/pachca_test.go b/pachca_test.go index 20a6cd7..51bc462 100644 --- a/pachca_test.go +++ b/pachca_test.go @@ -163,6 +163,15 @@ func (s *PachcaSuite) TestNilClient(c *C) { c.Assert(err, Equals, ErrNilClient) } +func (s *PachcaSuite) TestNewPropertyRequest(c *C) { + c.Assert(NewPropertyRequest(1, "test").Value, Equals, "test") + c.Assert(NewPropertyRequest(1, 100).Value, Equals, "100") + c.Assert(NewPropertyRequest(1, float32(100.12)).Value, Equals, "100") + c.Assert(NewPropertyRequest(1, float64(100.12)).Value, Equals, "100") + c.Assert(NewPropertyRequest(1, time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC)).Value, Equals, "2020-01-01T12:00:00Z") + c.Assert(NewPropertyRequest(1, true).Value, Equals, "true") +} + func (s *PachcaSuite) TestErrors(c *C) { cc, err := NewClient("YQlf-6Vce7jM1RMZZUs_iWKYPt24PeR4c7k_RwzqjI5") c.Assert(cc, NotNil) @@ -346,7 +355,7 @@ func (s *PachcaSuite) TestPropertiesHelpers(c *C) { _, err = p.Find("test5").ToInt() c.Assert(err, IsNil) - _, err = p.Find("test2").ToInt() + _, err = p.Find("TEST2").ToInt() c.Assert(err, NotNil) var pp *Property @@ -399,7 +408,7 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) { c.Assert(uu.Find("test"), IsNil) c.Assert(uu.Find("j.doe"), NotNil) - c.Assert(uu.Find("test@example.com"), NotNil) + c.Assert(uu.Find("TEST@EXAMPLE.COM"), NotNil) c.Assert(uu.Get(100), IsNil) c.Assert(uu.Get(6).ID, Equals, uint64(6)) @@ -421,7 +430,7 @@ func (s *PachcaSuite) TestChatsHelpers(c *C) { c.Assert(cc.Get(100), IsNil) c.Assert(cc.Find("test"), IsNil) - c.Assert(cc.Find("test1"), NotNil) + c.Assert(cc.Find("TEST1"), NotNil) c.Assert(cc.Public()[0].ID, Equals, uint64(3)) c.Assert(cc.Channels()[0].ID, Equals, uint64(4)) @@ -440,10 +449,16 @@ func (s *PachcaSuite) TestTagsHelpers(c *C) { c.Assert(tt.Get(1), NotNil) c.Assert(tt.Get(10), IsNil) + c.Assert(tt.Find("test"), IsNil) + c.Assert(tt.Find("test1"), NotNil) + c.Assert(tt.Find("test1").ID, Equals, uint64(1)) + chat := &Chat{ID: 1, Name: "test1", GroupTags: []uint64{1, 2, 3, 100, 101, 102}} c.Assert(tt.InChat(nil), IsNil) c.Assert(tt.InChat(chat), HasLen, 3) + + c.Assert(tt.Names(), DeepEquals, []string{"Test1", "Test2", "Test3"}) } func (s *PachcaSuite) TestURLHelpers(c *C) {