From b4d146e6e2d06a8693acf7fdd0cfa71f58084b3a Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Mon, 9 Jul 2018 20:48:02 -0600 Subject: [PATCH] Add Patch Add AssignTicketToTeam --- 3.0/connectwise/requests.go | 7 +++++++ 3.0/connectwise/schedule.go | 2 ++ 3.0/connectwise/service.go | 32 +++++++++++++++++++++++++++++++- 3.0/connectwise/system.go | 5 ++--- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/3.0/connectwise/requests.go b/3.0/connectwise/requests.go index 8dbbea4..2e81815 100644 --- a/3.0/connectwise/requests.go +++ b/3.0/connectwise/requests.go @@ -20,6 +20,13 @@ type Request struct { PageSize int } +//Patch is a struct which holds the required fields to make a PATCH request +type Patch struct { + Op string `json:"op"` + Path string `json:"path"` + Value string `json:"value"` +} + //NewRequest is a function which takes the mandatory fields to perform a request to the CW API and returns a pointer to a Request struct func (cw *Site) NewRequest(restAction, method string, body []byte) *Request { req := Request{CW: cw, RestAction: restAction, Method: method, Body: body} diff --git a/3.0/connectwise/schedule.go b/3.0/connectwise/schedule.go index 550d075..722ccae 100644 --- a/3.0/connectwise/schedule.go +++ b/3.0/connectwise/schedule.go @@ -6,6 +6,7 @@ import ( "time" ) +//Calendar is a struct to hold calendar data extracted from JSON type Calendar struct { ID int `json:"id"` Name string `json:"name"` @@ -32,6 +33,7 @@ type Calendar struct { } `json:"_info"` } +//ScheduleEntry is a struct to hold extracted JSON data type ScheduleEntry struct { ID int `json:"id"` ObjectID int `json:"objectId"` diff --git a/3.0/connectwise/service.go b/3.0/connectwise/service.go index ae36237..b2445c1 100644 --- a/3.0/connectwise/service.go +++ b/3.0/connectwise/service.go @@ -3,6 +3,7 @@ package connectwise import ( "encoding/json" "fmt" + "strconv" "time" ) @@ -486,7 +487,7 @@ func (cw *Site) GetBoardTeamByName(boardID int, teamName string) (*BoardTeam, er } if len(*boardTeam) == 0 { - return nil, fmt.Errorf("connectsise returned no results for %s/%s", boardID, teamName) + return nil, fmt.Errorf("connectwise returned no results for %d/%s", boardID, teamName) } return &(*boardTeam)[0], nil @@ -508,3 +509,32 @@ func (cw *Site) GetBoards() (*[]Board, error) { return board, nil } + +//AssignTicketToTeam will set the team/id of a ticket +func (cw *Site) AssignTicketToTeam(ticketID, teamID int) (*Ticket, error) { + patches := &[]Patch{} + patch := &Patch{ + Op: "replace", + Path: "team/id", + Value: strconv.Itoa(teamID)} + *patches = append(*patches, *patch) + + patchBody, err := json.Marshal(patches) + if err != nil { + return nil, fmt.Errorf("could not marhsal patch json to byte slice: %s", err) + } + + req := cw.NewRequest(fmt.Sprintf("/service/tickets/%d", ticketID), "PATCH", patchBody) + err = req.Do() + if err != nil { + return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err) + } + + ticket := &Ticket{} + err = json.Unmarshal(req.Body, ticket) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err) + } + + return ticket, nil +} diff --git a/3.0/connectwise/system.go b/3.0/connectwise/system.go index 61f1f9b..4800df1 100644 --- a/3.0/connectwise/system.go +++ b/3.0/connectwise/system.go @@ -21,7 +21,8 @@ type Callback struct { } `json:"_info"` } -//GetMembers returns a slice of Member structs containing all the members (users) of connectwise +//GetSystemMembers returns a slice of Member structs containing all the members (users) of connectwise +//TBD finish this, I don't have permissions with my API key to see the JSON data func (cw *Site) GetSystemMembers() error { req := cw.NewRequest("/system/members", "GET", nil) err := req.Do() @@ -29,8 +30,6 @@ func (cw *Site) GetSystemMembers() error { return fmt.Errorf("request failed for %s: %s", req.RestAction, err) } - fmt.Println("test") - fmt.Println(req.Body) /* callbacks := &[]Callback{} err = json.Unmarshal(req.Body, callbacks)