diff --git a/src/internal/connector/exchange/service_functions.go b/src/internal/connector/exchange/service_functions.go new file mode 100644 index 000000000..3157694fd --- /dev/null +++ b/src/internal/connector/exchange/service_functions.go @@ -0,0 +1,24 @@ +package exchange + +import ( + "github.com/microsoftgraph/msgraph-sdk-go/models" + + "github.com/alcionai/corso/internal/connector/graph" +) + +// CreateMailFolder makes a mail folder iff a folder of the same name does not exist +// Reference: https://docs.microsoft.com/en-us/graph/api/user-post-mailfolders?view=graph-rest-1.0&tabs=http +func CreateMailFolder(gs graph.Service, user, folder string) (models.MailFolderable, error) { + requestBody := models.NewMailFolder() + requestBody.SetDisplayName(&folder) + isHidden := false + requestBody.SetIsHidden(&isHidden) + + return gs.Client().UsersById(user).MailFolders().Post(requestBody) +} + +// DeleteMailFolder removes a mail folder with the corresponding M365 ID from the user's M365 Exchange account +// Reference: https://docs.microsoft.com/en-us/graph/api/mailfolder-delete?view=graph-rest-1.0&tabs=http +func DeleteMailFolder(gs graph.Service, user, folderID string) error { + return gs.Client().UsersById(user).MailFoldersById(folderID).Delete() +} diff --git a/src/internal/connector/graph_connector.go b/src/internal/connector/graph_connector.go index 4324ef2d6..03620bc0e 100644 --- a/src/internal/connector/graph_connector.go +++ b/src/internal/connector/graph_connector.go @@ -104,21 +104,6 @@ func (gs *graphService) EnableFailFast() { gs.failFast = true } -// createMailFolder will create a mail folder iff a folder of the same name does not exit -func createMailFolder(gc graphService, user, folder string) (models.MailFolderable, error) { - requestBody := models.NewMailFolder() - requestBody.SetDisplayName(&folder) - isHidden := false - requestBody.SetIsHidden(&isHidden) - - return gc.client.UsersById(user).MailFolders().Post(requestBody) -} - -// deleteMailFolder removes the mail folder from the user's M365 Exchange account -func deleteMailFolder(gc graphService, user, folderID string) error { - return gc.client.UsersById(user).MailFoldersById(folderID).Delete() -} - // setTenantUsers queries the M365 to identify the users in the // workspace. The users field is updated during this method // iff the return value is true diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 6b7a65c63..7b56b5682 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -4,12 +4,14 @@ import ( "bytes" "context" "testing" + "time" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/alcionai/corso/internal/common" + "github.com/alcionai/corso/internal/connector/exchange" "github.com/alcionai/corso/internal/connector/mockconnector" "github.com/alcionai/corso/internal/connector/support" "github.com/alcionai/corso/internal/data" @@ -93,13 +95,14 @@ func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_restoreMessages( assert.NoError(suite.T(), err) } -func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_createDeleteFolder() { +func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_CreateAndDeleteFolder() { user := "lidiah@8qzvrj.onmicrosoft.com" - folderName := uuid.NewString() // todo - replace with danny's fix #391 - aFolder, err := createMailFolder(suite.connector.graphService, user, folderName) + now := time.Now() + folderName := "TestFolder: " + common.FormatSimpleDateTime(now) + aFolder, err := exchange.CreateMailFolder(&suite.connector.graphService, user, folderName) assert.NoError(suite.T(), err, support.ConnectorStackErrorTrace(err)) if aFolder != nil { - err = deleteMailFolder(suite.connector.graphService, user, *aFolder.GetId()) + err = exchange.DeleteMailFolder(&suite.connector.graphService, user, *aFolder.GetId()) assert.NoError(suite.T(), err) } }