GetMailFolderID feature added (#455)

GetMaillFolderID add to `exchange` package. Test Suite Added.
This commit is contained in:
Danny 2022-08-01 15:12:02 -04:00 committed by GitHub
parent 890c8f2b7f
commit bcbf0b1fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}