diff --git a/v1/client.go b/v1/client.go index da677e0..429f2c8 100644 --- a/v1/client.go +++ b/v1/client.go @@ -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: diff --git a/v1/client_test.go b/v1/client_test.go index 3d190c9..ad18879 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -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 diff --git a/v1/types.go b/v1/types.go index c7f8947..1cd608f 100644 --- a/v1/types.go +++ b/v1/types.go @@ -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"`