mirror of
https://github.com/retailcrm/mg-bot-api-client-go.git
synced 2024-11-21 20:36:05 +03:00
Add unassigned dialog method
This commit is contained in:
commit
23b605add8
36
v1/client.go
36
v1/client.go
@ -339,6 +339,42 @@ func (c *MgClient) DialogAssign(request DialogAssignRequest) (DialogAssignRespon
|
|||||||
return resp, status, err
|
return resp, status, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialogUnassign allows to remove responsible from the dialogue
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// var client = v1.New("https://demo.url", "09jIJ")
|
||||||
|
//
|
||||||
|
// data, status, err := client.DialogUnassign(1)
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Printf("%v", err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if status >= http.StatusBadRequest {
|
||||||
|
// fmt.Printf("%v", err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fmt.Printf("%v\n", data.Responsible)
|
||||||
|
func (c *MgClient) DialogUnassign(dialogID uint64) (DialogUnassignResponse, int, error) {
|
||||||
|
var resp DialogUnassignResponse
|
||||||
|
|
||||||
|
data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/unassign", dialogID), nil)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if status != http.StatusOK {
|
||||||
|
return resp, status, c.Error(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &resp); err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
// DialogClose close selected dialog
|
// DialogClose close selected dialog
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
|
@ -249,6 +249,63 @@ func TestMgClient_DialogAssign(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, status)
|
assert.Equal(t, http.StatusBadRequest, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMgClient_DialogUnassign(t *testing.T) {
|
||||||
|
c := client()
|
||||||
|
defer gock.Off()
|
||||||
|
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
gock.New(mgURL).
|
||||||
|
Patch("/api/bot/v1/dialogs/777/unassign").
|
||||||
|
Reply(200).
|
||||||
|
BodyString(`{"previous_responsible": {"id": 111, "type": "bot", "assigned_at": "2020-07-14T14:11:44.000000Z"}}`)
|
||||||
|
|
||||||
|
resp, status, err := c.DialogUnassign(777)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, status)
|
||||||
|
|
||||||
|
assert.Equal(t, int64(111), resp.PreviousResponsible.ID)
|
||||||
|
assert.Equal(t, "bot", resp.PreviousResponsible.Type)
|
||||||
|
assert.Equal(t, "2020-07-14T14:11:44.000000Z", resp.PreviousResponsible.AssignAt)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("dialog not latest in chat", func(t *testing.T) {
|
||||||
|
gock.New(mgURL).
|
||||||
|
Patch("/api/bot/v1/dialogs/666/unassign").
|
||||||
|
Reply(400).
|
||||||
|
BodyString(`{"errors": ["dialog is not the latest in the chat"]}`)
|
||||||
|
|
||||||
|
_, status, err := c.DialogUnassign(666)
|
||||||
|
|
||||||
|
assert.Error(t, err, "dialog is not the latest in the chat")
|
||||||
|
assert.Equal(t, http.StatusBadRequest, status)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("dialog is not assigned", func(t *testing.T) {
|
||||||
|
gock.New(mgURL).
|
||||||
|
Patch("/api/bot/v1/dialogs/555/unassign").
|
||||||
|
Reply(400).
|
||||||
|
BodyString(`{"errors": ["dialog is not assigned"]}`)
|
||||||
|
|
||||||
|
_, status, err := c.DialogUnassign(555)
|
||||||
|
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, http.StatusBadRequest, status)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("dialog not found", func(t *testing.T) {
|
||||||
|
gock.New(mgURL).
|
||||||
|
Patch("/api/bot/v1/dialogs/444/unassign").
|
||||||
|
Reply(404).
|
||||||
|
BodyString(`{"errors": ["dialog #444 not found"]}`)
|
||||||
|
|
||||||
|
_, status, err := c.DialogUnassign(444)
|
||||||
|
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, http.StatusNotFound, status)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestMgClient_DialogClose(t *testing.T) {
|
func TestMgClient_DialogClose(t *testing.T) {
|
||||||
c := client()
|
c := client()
|
||||||
i := 1
|
i := 1
|
||||||
|
@ -315,6 +315,10 @@ type (
|
|||||||
IsReAssign bool `json:"is_reassign"`
|
IsReAssign bool `json:"is_reassign"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DialogUnassignResponse struct {
|
||||||
|
PreviousResponsible Responsible `json:"previous_responsible,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
MessagesResponseItem struct {
|
MessagesResponseItem struct {
|
||||||
Message
|
Message
|
||||||
ChannelID uint64 `json:"channel_id,omitempty"`
|
ChannelID uint64 `json:"channel_id,omitempty"`
|
||||||
|
Loading…
Reference in New Issue
Block a user