diff --git a/src/internal/connector/exchange/service_functions.go b/src/internal/connector/exchange/service_functions.go index 3157694fd..8ab740bc7 100644 --- a/src/internal/connector/exchange/service_functions.go +++ b/src/internal/connector/exchange/service_functions.go @@ -1,9 +1,13 @@ package exchange import ( + msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" + "github.com/microsoftgraph/msgraph-sdk-go/models" + "github.com/pkg/errors" "github.com/alcionai/corso/internal/connector/graph" + "github.com/alcionai/corso/internal/connector/support" ) // CreateMailFolder makes a mail folder iff a folder of the same name does not exist @@ -22,3 +26,44 @@ func CreateMailFolder(gs graph.Service, user, folder string) (models.MailFoldera func DeleteMailFolder(gs graph.Service, user, folderID string) error { return gs.Client().UsersById(user).MailFoldersById(folderID).Delete() } + +// GetMailFolderID query function to retrieve the M365 ID based on the folder's displayName. +// @param folderName the target folder's display name. Case sensitive +// @returns a *string if the folder exists, nil otherwise +func GetMailFolderID(service graph.Service, folderName, user string) (*string, error) { + var errs error + var folderId *string + options, err := optionsForMailFolders([]string{"displayName"}) + if err != nil { + return nil, err + } + response, err := service.Client().UsersById(user).MailFolders().GetWithRequestConfigurationAndResponseHandler(options, nil) + if err != nil { + return nil, err + } + if response == nil { + return nil, errors.New("mail folder query to m365 back store returned nil") + } + pageIterator, err := msgraphgocore.NewPageIterator(response, service.Adapter(), models.CreateMailFolderCollectionResponseFromDiscriminatorValue) + if err != nil { + return nil, err + } + callbackFunc := func(folderItem any) bool { + folder, ok := folderItem.(models.MailFolderable) + if !ok { + errs = support.WrapAndAppend(service.Adapter().GetBaseUrl(), errors.New("HasFolder() iteration failure"), errs) + return true + } + if *folder.GetDisplayName() == folderName { + folderId = folder.GetId() + return false + } + return true + } + iterateError := pageIterator.Iterate(callbackFunc) + if iterateError != nil { + errs = support.WrapAndAppend(service.Adapter().GetBaseUrl(), iterateError, errs) + } + return folderId, errs + +} diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 7b56b5682..dcce4dfd9 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -95,6 +95,12 @@ func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_restoreMessages( assert.NoError(suite.T(), err) } +///------------------------------------------------------------ +// Exchange Functions +//------------------------------------------------------- + +// TestGraphConnector_CreateAndDeleteFolder ensures msgraph application has the ability +// to create and remove folders within the tenant func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_CreateAndDeleteFolder() { user := "lidiah@8qzvrj.onmicrosoft.com" now := time.Now() @@ -106,3 +112,13 @@ func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_CreateAndDeleteF assert.NoError(suite.T(), err) } } + +// TestGraphConnector_GetMailFolderID verifies the ability to retrieve folder ID of folders +// at the top level of the file tree +func (suite *GraphConnectorIntegrationSuite) TestGraphConnector_GetMailFolderID() { + user := "lidiah@8qzvrj.onmicrosoft.com" + folderName := "Inbox" + folderID, err := exchange.GetMailFolderID(&suite.connector.graphService, folderName, user) + assert.NoError(suite.T(), err) + assert.NotNil(suite.T(), folderID) +}