add unassigned dialog method

This commit is contained in:
Alexander Kulinich 2020-07-14 14:37:46 +03:00
parent 2b05530ce7
commit 0fd66ebf6e
3 changed files with 97 additions and 0 deletions

View File

@ -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:

View File

@ -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

View File

@ -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"`