remove YAGNI

This commit is contained in:
neha-Gupta1 2023-08-23 16:03:03 +05:30
parent 27990e6174
commit 63080485a8
2 changed files with 5 additions and 252 deletions

View File

@ -32,52 +32,6 @@ type Channels struct {
// containers
// ---------------------------------------------------------------------------
// CreateContainer makes an channels with the name in the team
func (c Channels) CreateChannel(
ctx context.Context,
teamID, containerName string,
) (graph.Container, error) {
body := models.NewChannel()
body.SetDisplayName(&containerName)
container, err := c.Stable.
Client().
Teams().
ByTeamId(teamID).
Channels().
Post(ctx, body, nil)
if err != nil {
return nil, graph.Wrap(ctx, err, "creating channel")
}
return ChannelsDisplayable{Channelable: container}, nil
}
// DeleteChannel removes a channel from user's M365 account
func (c Channels) DeleteChannel(
ctx context.Context,
teamID, containerID string,
) error {
// deletes require unique http clients
// https://github.com/alcionai/corso/issues/2707
srv, err := NewService(c.Credentials)
if err != nil {
return graph.Stack(ctx, err)
}
err = srv.Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Delete(ctx, nil)
if err != nil {
return graph.Stack(ctx, err)
}
return nil
}
func (c Channels) GetChannel(
ctx context.Context,
teamID, containerID string,
@ -160,26 +114,6 @@ func (c Channels) GetChannelByName(
return container, nil
}
func (c Channels) PatchChannel(
ctx context.Context,
teamID, containerID string,
body models.Channelable,
) error {
_, err := c.Stable.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Patch(ctx, body, nil)
if err != nil {
return graph.Wrap(ctx, err, "patching channel")
}
return nil
}
// ---------------------------------------------------------------------------
// message
// ---------------------------------------------------------------------------
@ -209,58 +143,7 @@ func (c Channels) GetMessage(
return nil, nil, graph.Stack(ctx, err)
}
return message, MessageInfo(message, size), nil
}
func (c Channels) PostMessage(
ctx context.Context,
teamID, containerID string,
body models.ChatMessageable,
) (models.ChatMessageable, error) {
itm, err := c.Stable.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Messages().
Post(ctx, body, nil)
if err != nil {
return nil, graph.Wrap(ctx, err, "creating mail message")
}
if itm == nil {
return nil, clues.New("nil response mail message creation").WithClues(ctx)
}
return itm, nil
}
func (c Channels) DeleteMessage(
ctx context.Context,
teamID, itemID, containerID string,
) error {
// deletes require unique http clients
// https://github.com/alcionai/corso/issues/2707
srv, err := NewService(c.Credentials)
if err != nil {
return graph.Stack(ctx, err)
}
err = srv.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Messages().
ByChatMessageId(itemID).
Delete(ctx, nil)
if err != nil {
return graph.Wrap(ctx, err, "deleting mail message")
}
return nil
return message, ChannelMessageInfo(message, size), nil
}
// ---------------------------------------------------------------------------
@ -289,66 +172,11 @@ func (c Channels) GetReplies(
return replies, nil
}
func (c Channels) PostReply(
ctx context.Context,
teamID, containerID, messageID string,
body models.ChatMessageable,
) (models.ChatMessageable, error) {
itm, err := c.Stable.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Messages().
ByChatMessageId(messageID).
Replies().
Post(ctx, body, nil)
if err != nil {
return nil, graph.Wrap(ctx, err, "creating reply message")
}
if itm == nil {
return nil, clues.New("nil response reply to message creation").WithClues(ctx)
}
return itm, nil
}
func (c Channels) DeleteReply(
ctx context.Context,
teamID, itemID, containerID, replyID string,
) error {
// deletes require unique http clients
// https://github.com/alcionai/corso/issues/2707
srv, err := NewService(c.Credentials)
if err != nil {
return graph.Stack(ctx, err)
}
err = srv.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Messages().
ByChatMessageId(itemID).
Replies().
ByChatMessageId1(replyID).
Delete(ctx, nil)
if err != nil {
return graph.Wrap(ctx, err, "deleting mail message")
}
return nil
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
func MessageInfo(msg models.ChatMessageable, size int64) *details.GroupsInfo {
func ChannelMessageInfo(msg models.ChatMessageable, size int64) *details.GroupsInfo {
var (
created = ptr.Val(msg.GetCreatedDateTime())
)

View File

@ -1,18 +1,12 @@
package api_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/tester"
"github.com/alcionai/corso/src/internal/tester/tconfig"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/services/m365/api"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
@ -48,96 +42,27 @@ func (suite *ChannelPagerIntgSuite) TestChannels_GetPage() {
assert.NotNil(t, a)
}
func (suite *ChannelPagerIntgSuite) TestChannels_CreateGetAndDelete() {
func (suite *ChannelPagerIntgSuite) TestChannels_Get() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
var (
yy, mm, dd = time.Now().Date()
hh = time.Now().Hour()
min = time.Now().Minute()
ss = time.Now().Second()
containerName = fmt.Sprintf("testChannel%d%d%d%d%d%d", yy, mm, dd, hh, min, ss)
containerName = "General"
teamID = tconfig.M365TeamsID(t)
credentials = suite.its.ac.Credentials
chanClient = suite.its.ac.Channels()
)
// GET channel - should be not found
_, err := suite.its.ac.Channels().GetChannelByName(ctx, teamID, containerName)
assert.Error(t, err, clues.ToCore(err))
// POST channel
channelPost, err := suite.its.ac.Channels().CreateChannel(ctx, teamID, containerName)
assert.NoError(t, err, clues.ToCore(err))
postChannelID := ptr.Val(channelPost.GetId())
// DELETE channel
defer func() {
_, err := chanClient.GetChannelByID(ctx, teamID, postChannelID)
if err != nil {
fmt.Println("could not find channel: ", err)
} else {
deleteChannel(ctx, credentials, teamID, postChannelID)
}
}()
// GET channel -should be found
channel, err := chanClient.GetChannelByName(ctx, teamID, containerName)
assert.NoError(t, err, clues.ToCore(err))
assert.Equal(t, ptr.Val(channel.GetDisplayName()), containerName)
// PATCH channel
patchBody := models.NewChannel()
patchName := fmt.Sprintf("othername%d%d%d%d%d%d", yy, mm, dd, hh, min, ss)
patchBody.SetDisplayName(ptr.To(patchName))
err = chanClient.PatchChannel(ctx, teamID, postChannelID, patchBody)
assert.NoError(t, err, clues.ToCore(err))
assert.Equal(t, ptr.Val(channel.GetDisplayName()), containerName)
// GET channel -should not be found with old name
_, err = chanClient.GetChannelByName(ctx, teamID, containerName)
assert.Error(t, err, clues.ToCore(err))
// GET channel -should be found with new name
channel, err = chanClient.GetChannelByName(ctx, teamID, patchName)
assert.NoError(t, err, clues.ToCore(err))
assert.Equal(t, ptr.Val(channel.GetDisplayName()), patchName)
assert.Equal(t, ptr.Val(channel.GetId()), postChannelID)
// GET channel -should not be found with old name
err = chanClient.DeleteChannel(ctx, teamID, postChannelID)
assert.NoError(t, err, clues.ToCore(err))
// GET channel -should not be found anymore
_, err = chanClient.GetChannel(ctx, teamID, postChannelID)
_, err = chanClient.GetChannel(ctx, teamID, ptr.Val(channel.GetId()))
assert.Error(t, err, clues.ToCore(err))
}
func deleteChannel(ctx context.Context, credentials account.M365Config, teamID, postChannelID string) {
srv, err := api.NewService(credentials)
if err != nil {
fmt.Println("Error found in getting creds")
}
if err != nil {
fmt.Println("Error found in getting creds")
}
err = srv.Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(postChannelID).
Delete(ctx, nil)
if err != nil {
fmt.Println("channel could not be delete in defer")
}
}
// func (suite *ChannelPagerIntgSuite) TestMessages_CreateGetAndDelete() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)