mirror of
https://github.com/wahyd4/zendesk.git
synced 2026-08-09 05:06:48 +10:00
Add function to load users from json
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"github.com/wahyd4/zendesk/model"
|
||||
)
|
||||
|
||||
// APP represents the application
|
||||
type APP struct {
|
||||
organisations map[string]*model.Organisation
|
||||
users map[string]*model.User
|
||||
tickets map[string]*model.Ticket
|
||||
}
|
||||
|
||||
// FindOrganisation find a organisation by organisation ID
|
||||
func (app *APP) FindOrganisation(id string) *model.Organisation {
|
||||
return app.organisations[id]
|
||||
}
|
||||
@@ -22,6 +22,16 @@ func (app *APP) LoadOrganisationsFromJSON(jsonContent []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *APP) LoadUsersFromJSON(jsonContent []byte) error {
|
||||
var users []*view.User
|
||||
err := json.Unmarshal(jsonContent, &users)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load users from json %s", err.Error())
|
||||
}
|
||||
app.users = app.convertUserViews(users)
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertOrganisationViews(views []*view.Organisation) map[string]*model.Organisation {
|
||||
models := make(map[string]*model.Organisation)
|
||||
for _, view := range views {
|
||||
@@ -30,6 +40,38 @@ func convertOrganisationViews(views []*view.Organisation) map[string]*model.Orga
|
||||
return models
|
||||
}
|
||||
|
||||
func (app *APP) convertUserViews(views []*view.User) map[string]*model.User {
|
||||
models := make(map[string]*model.User)
|
||||
for _, view := range views {
|
||||
models[fmt.Sprintf("%d", view.ID)] = app.convertUserView(view)
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
func (app *APP) convertUserView(view *view.User) *model.User {
|
||||
return &model.User{
|
||||
ID: view.ID,
|
||||
URL: view.URL,
|
||||
ExternalID: view.ExternalID,
|
||||
Name: view.Name,
|
||||
Alias: view.Alias,
|
||||
CreatedAt: view.CreatedAt,
|
||||
Active: view.Active,
|
||||
Verified: view.Verified,
|
||||
Shared: view.Shared,
|
||||
Locale: view.Locale,
|
||||
Timezone: view.Timezone,
|
||||
LastLoginAt: view.LastLoginAt,
|
||||
Email: view.Email,
|
||||
Phone: view.Phone,
|
||||
Signature: view.Signature,
|
||||
Organization: app.FindOrganisation(fmt.Sprintf("%d", view.OrganizationID)),
|
||||
Tags: view.Tags,
|
||||
Suspended: view.Suspended,
|
||||
Role: view.Role,
|
||||
}
|
||||
}
|
||||
|
||||
func convertOrganisationView(view *view.Organisation) *model.Organisation {
|
||||
return &model.Organisation{
|
||||
ID: view.ID,
|
||||
|
||||
@@ -75,6 +75,20 @@ func TestAPP_LoadOrganisationsFromJSON(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cannot load organisations from json due to json is not valid",
|
||||
fields: fields{
|
||||
app: &APP{},
|
||||
},
|
||||
args: args{
|
||||
jsonContent: []byte(`[{
|
||||
"_id": 101,
|
||||
"url" "http://initech.zendesk.com/api/v2/organizations/101.json"
|
||||
]}]`),
|
||||
},
|
||||
wantErr: true,
|
||||
wantApp: &APP{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -90,3 +104,123 @@ func TestAPP_LoadOrganisationsFromJSON(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPP_LoadUsersFromJSON(t *testing.T) {
|
||||
organisation := &model.Organisation{
|
||||
ID: 119,
|
||||
}
|
||||
|
||||
type fields struct {
|
||||
app *APP
|
||||
}
|
||||
type args struct {
|
||||
jsonContent []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
wantApp *APP
|
||||
}{
|
||||
{
|
||||
name: "can load users from json",
|
||||
fields: fields{
|
||||
app: &APP{
|
||||
organisations: map[string]*model.Organisation{
|
||||
"119": organisation,
|
||||
},
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
jsonContent: []byte(`[{
|
||||
"_id": 1,
|
||||
"url": "http://initech.zendesk.com/api/v2/users/1.json",
|
||||
"external_id": "74341f74-9c79-49d5-9611-87ef9b6eb75f",
|
||||
"name": "Francisca Rasmussen",
|
||||
"alias": "Miss Coffey",
|
||||
"created_at": "2016-04-15T05:19:46 -10:00",
|
||||
"active": true,
|
||||
"verified": true,
|
||||
"shared": false,
|
||||
"locale": "en-AU",
|
||||
"timezone": "Sri Lanka",
|
||||
"last_login_at": "2013-08-04T01:03:27 -10:00",
|
||||
"email": "coffeyrasmussen@flotonic.com",
|
||||
"phone": "8335-422-718",
|
||||
"signature": "Don't Worry Be Happy!",
|
||||
"organization_id": 119,
|
||||
"tags": [
|
||||
"Springville",
|
||||
"Sutton",
|
||||
"Hartsville/Hartley",
|
||||
"Diaperville"
|
||||
],
|
||||
"suspended": true,
|
||||
"role": "admin"
|
||||
}]`),
|
||||
},
|
||||
wantErr: false,
|
||||
wantApp: &APP{
|
||||
organisations: map[string]*model.Organisation{
|
||||
"119": organisation,
|
||||
},
|
||||
users: map[string]*model.User{
|
||||
"1": &model.User{
|
||||
ID: 1,
|
||||
URL: "http://initech.zendesk.com/api/v2/users/1.json",
|
||||
ExternalID: "74341f74-9c79-49d5-9611-87ef9b6eb75f",
|
||||
Name: "Francisca Rasmussen",
|
||||
Alias: "Miss Coffey",
|
||||
CreatedAt: "2016-04-15T05:19:46 -10:00",
|
||||
Active: true,
|
||||
Verified: true,
|
||||
Shared: false,
|
||||
Locale: "en-AU",
|
||||
Timezone: "Sri Lanka",
|
||||
LastLoginAt: "2013-08-04T01:03:27 -10:00",
|
||||
Email: "coffeyrasmussen@flotonic.com",
|
||||
Phone: "8335-422-718",
|
||||
Signature: "Don't Worry Be Happy!",
|
||||
Organization: organisation,
|
||||
Tags: []string{
|
||||
"Springville",
|
||||
"Sutton",
|
||||
"Hartsville/Hartley",
|
||||
"Diaperville",
|
||||
},
|
||||
Suspended: true,
|
||||
Role: "admin",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cannot load users from json due to json is not valid",
|
||||
fields: fields{
|
||||
app: &APP{},
|
||||
},
|
||||
args: args{
|
||||
jsonContent: []byte(`[{
|
||||
"_id": 101,
|
||||
"url" "http://initech.zendesk.com/api/v2/organizations/101.json"
|
||||
]}]`),
|
||||
},
|
||||
wantErr: true,
|
||||
wantApp: &APP{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := tt.fields.app
|
||||
if err := app.LoadUsersFromJSON(tt.args.jsonContent); (err != nil) != tt.wantErr {
|
||||
t.Errorf("APP.LoadUsersFromJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(app, tt.wantApp) {
|
||||
t.Errorf("LoadUsersFromJSON() got = %v, want %v", app, tt.wantApp)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user