forked from grokify/go-ringcentral-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergedusers.go
204 lines (185 loc) · 5.8 KB
/
mergedusers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mergedusers
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"strings"
rc "github.com/grokify/go-ringcentral/client"
"github.com/grokify/gotilla/mime/multipartutil"
"github.com/nyaruka/phonenumbers"
)
type MergedUserSet struct {
UserIdOrder []string
MergedUserMap map[string]MergedUser
}
func NewMergedUserSet() MergedUserSet {
return MergedUserSet{
UserIdOrder: []string{},
MergedUserMap: map[string]MergedUser{}}
}
type MergedUser struct {
MainNumber string
GlipPersonInfo rc.GlipPersonInfo
ExtensionInfo rc.GetExtensionInfoResponse
}
func (mu *MergedUser) TrimNumbers() {
mu.MainNumber = strings.TrimSpace(mu.MainNumber)
mu.ExtensionInfo.Contact.BusinessPhone = strings.TrimSpace(mu.ExtensionInfo.Contact.BusinessPhone)
mu.ExtensionInfo.ExtensionNumber = strings.TrimSpace(mu.ExtensionInfo.ExtensionNumber)
}
func (mu *MergedUser) PhoneBusinessOrMainNumberExt() string {
mu.TrimNumbers()
if len(mu.ExtensionInfo.Contact.BusinessPhone) > 0 {
return mu.ExtensionInfo.Contact.BusinessPhone
}
if len(mu.MainNumber) > 0 &&
len(mu.ExtensionInfo.ExtensionNumber) > 0 {
return fmt.Sprintf("%s;ext=%s", mu.MainNumber, mu.ExtensionInfo.ExtensionNumber)
} else if len(mu.MainNumber) > 0 {
return mu.MainNumber
} else if len(mu.ExtensionInfo.ExtensionNumber) > 0 {
return fmt.Sprintf("ext=%s", mu.ExtensionInfo.ExtensionNumber)
}
return ""
}
func (mu *MergedUser) DisplayNumber() (string, error) {
mu.TrimNumbers()
if len(mu.ExtensionInfo.Contact.BusinessPhone) > 0 {
num, err := phonenumbers.Parse(mu.ExtensionInfo.Contact.BusinessPhone, "")
if err != nil {
return "", err
}
return phonenumbers.Format(num, phonenumbers.NATIONAL), nil
} else if len(mu.MainNumber) > 0 {
num, err := phonenumbers.Parse(mu.MainNumber, "")
if err != nil {
return "", err
}
main := phonenumbers.Format(num, phonenumbers.NATIONAL)
if len(mu.ExtensionInfo.ExtensionNumber) > 0 {
main = fmt.Sprintf("%s x%s", main, mu.ExtensionInfo.ExtensionNumber)
}
return main, nil
} else if len(mu.ExtensionInfo.ExtensionNumber) > 0 {
return "x" + mu.ExtensionInfo.ExtensionNumber, nil
}
return "", errors.New("No phone number")
}
type MergedUserThin struct {
Id string
FirstName string
LastName string
Email string
Avatar string
CompanyId string
JobTitle string
MainNumber string
ExtensionNumber string
BusinessPhone string
DisplayNumber string
}
func (mu *MergedUser) ToMergedUserThin() MergedUserThin {
per := mu.GlipPersonInfo
ext := mu.ExtensionInfo
thin := MergedUserThin{
Id: per.Id,
FirstName: per.FirstName,
LastName: per.LastName,
Email: per.Email,
Avatar: per.Avatar,
CompanyId: per.CompanyId,
JobTitle: per.JobTitle,
MainNumber: mu.MainNumber,
ExtensionNumber: ext.ExtensionNumber,
BusinessPhone: ext.Contact.BusinessPhone}
if disp, err := mu.DisplayNumber(); err == nil {
thin.DisplayNumber = disp
}
return thin
}
func NewMergedUserFromMimePart(part *multipart.Part) (MergedUser, error) {
mu := MergedUser{}
if slurp, err := ioutil.ReadAll(part); err != nil {
return mu, err
} else if err = json.Unmarshal(slurp, &mu.GlipPersonInfo); err != nil {
return mu, err
}
return mu, nil
}
func NewGlipPersonInfoFromMimePart(part *multipart.Part) (rc.GlipPersonInfo, error) {
var person rc.GlipPersonInfo
if slurp, err := ioutil.ReadAll(part); err != nil {
return person, err
} else {
return person, json.Unmarshal(slurp, &person)
}
}
func NewExtensionInfoFromMimePart(part *multipart.Part) (rc.GetExtensionInfoResponse, error) {
var ext rc.GetExtensionInfoResponse
if slurp, err := ioutil.ReadAll(part); err != nil {
return ext, err
} else {
return ext, json.Unmarshal(slurp, &ext)
}
}
func AddBatchGlipPersonInfosBodyBoundary(mergedUserSet MergedUserSet, body []byte, boundary string) (MergedUserSet, error) {
mr := multipartutil.NewReaderBodyBytes([]byte(body), boundary)
return AddBatchGlipPersonInfosMultipartReader(mergedUserSet, mr)
}
func AddBatchGlipPersonInfosMultipartReader(mergedUserSet MergedUserSet, mr *multipart.Reader) (MergedUserSet, error) {
for {
part, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
return mergedUserSet, err
}
if person, err := NewGlipPersonInfoFromMimePart(part); err != nil {
return mergedUserSet, err
} else if len(person.Id) > 0 {
if mu, ok := mergedUserSet.MergedUserMap[person.Id]; ok {
mu.GlipPersonInfo = person
mergedUserSet.MergedUserMap[person.Id] = mu
} else {
mergedUserSet.MergedUserMap[person.Id] = MergedUser{GlipPersonInfo: person}
mergedUserSet.UserIdOrder = append(mergedUserSet.UserIdOrder, person.Id)
}
}
}
return mergedUserSet, nil
}
func AddBatchExtensionInfosHttpResponse(mergedUserSet MergedUserSet, resp *http.Response) (MergedUserSet, error) {
mr, err := multipartutil.NewMultipartReaderForHttpResponse(resp)
if err != nil {
return mergedUserSet, err
}
return AddBatchExtensionInfosMultipartReader(mergedUserSet, mr)
}
func AddBatchExtensionInfosMultipartReader(mergedUserSet MergedUserSet, mr *multipart.Reader) (MergedUserSet, error) {
for {
part, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
return mergedUserSet, err
}
if ext, err := NewExtensionInfoFromMimePart(part); err != nil {
return mergedUserSet, err
} else if ext.Id > 0 {
extIdString := strconv.Itoa(int(ext.Id))
if mu, ok := mergedUserSet.MergedUserMap[extIdString]; ok {
mu.ExtensionInfo = ext
mergedUserSet.MergedUserMap[extIdString] = mu
} else {
mergedUserSet.MergedUserMap[extIdString] = MergedUser{ExtensionInfo: ext}
mergedUserSet.UserIdOrder = append(mergedUserSet.UserIdOrder, extIdString)
}
}
}
return mergedUserSet, nil
}