Add unassigned dialog method

This commit is contained in:
Alex Lushpai 2020-07-15 15:49:22 +03:00 committed by GitHub
commit 23b605add8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
}
// 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
//
// Example:

View File

@ -249,6 +249,63 @@ func TestMgClient_DialogAssign(t *testing.T) {
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) {
c := client()
i := 1

View File

@ -315,6 +315,10 @@ type (
IsReAssign bool `json:"is_reassign"`
}
DialogUnassignResponse struct {
PreviousResponsible Responsible `json:"previous_responsible,omitempty"`
}
MessagesResponseItem struct {
Message
ChannelID uint64 `json:"channel_id,omitempty"`