diff --git a/src/internal/connector/exchange/delta_get.go b/src/internal/connector/exchange/delta_get.go index cebc0329b..a2793739e 100644 --- a/src/internal/connector/exchange/delta_get.go +++ b/src/internal/connector/exchange/delta_get.go @@ -81,7 +81,6 @@ func sendMessagesDeltaGet( return res.(msmaildelta.DeltaResponseable), nil } -//nolint:unused func sendContactsDeltaGet( ctx context.Context, m *mscontactdelta.DeltaRequestBuilder, diff --git a/src/internal/connector/exchange/query_options.go b/src/internal/connector/exchange/query_options.go index 42b7c499b..f20c258e2 100644 --- a/src/internal/connector/exchange/query_options.go +++ b/src/internal/connector/exchange/query_options.go @@ -9,7 +9,6 @@ import ( mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders" mscontactfolderitem "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item" mscontactfolderchild "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/childfolders" - mscontactfolderitemcontact "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/contacts" mscontacts "github.com/microsoftgraph/msgraph-sdk-go/users/item/contacts" msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/events" msfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders" @@ -300,19 +299,18 @@ func optionsForMailFoldersItem( } // optionsForContactFoldersItem is the same as optionsForContacts. -// TODO: Remove after Issue #828; requires updating msgraph to v0.34 func optionsForContactFoldersItem( moreOps []string, -) (*mscontactfolderitemcontact.ContactsRequestBuilderGetRequestConfiguration, error) { +) (*DeltaRequestBuilderGetRequestConfiguration, error) { selecting, err := buildOptions(moreOps, contacts) if err != nil { return nil, err } - requestParameters := &mscontactfolderitemcontact.ContactsRequestBuilderGetQueryParameters{ + requestParameters := &DeltaRequestBuilderGetQueryParameters{ Select: selecting, } - options := &mscontactfolderitemcontact.ContactsRequestBuilderGetRequestConfiguration{ + options := &DeltaRequestBuilderGetRequestConfiguration{ QueryParameters: requestParameters, } diff --git a/src/internal/connector/exchange/service_iterators.go b/src/internal/connector/exchange/service_iterators.go index 53e6001e4..b9314502d 100644 --- a/src/internal/connector/exchange/service_iterators.go +++ b/src/internal/connector/exchange/service_iterators.go @@ -8,6 +8,7 @@ import ( multierror "github.com/hashicorp/go-multierror" msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" "github.com/microsoftgraph/msgraph-sdk-go/models" + cdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/contacts/delta" mdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders/item/messages/delta" "github.com/pkg/errors" @@ -217,61 +218,54 @@ func FetchEventIDsFromCalendar( // FetchContactIDsFromDirectory function that returns a list of all the m365IDs of the contacts // of the targeted directory func FetchContactIDsFromDirectory(ctx context.Context, gs graph.Service, user, directoryID string) ([]string, error) { + var ( + errs *multierror.Error + ids []string + ) + options, err := optionsForContactFoldersItem([]string{"parentFolderId"}) if err != nil { - return nil, err + return nil, errors.Wrap(err, "getting query options") } - ids := []string{} - - response, err := gs.Client(). + builder := gs.Client(). UsersById(user). ContactFoldersById(directoryID). Contacts(). - Get(ctx, options) - if err != nil { - return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err)) - } + Delta() - pageIterator, err := msgraphgocore.NewPageIterator( - response, - gs.Adapter(), - models.CreateContactCollectionResponseFromDiscriminatorValue, - ) - if err != nil { - return nil, errors.Wrap(err, "iterator creation during FetchContactIDs") - } - - var errs *multierror.Error - - err = pageIterator.Iterate(ctx, func(pageItem any) bool { - entry, ok := pageItem.(graph.Idable) - if !ok { - errs = multierror.Append( - errs, - errors.New("casting pageItem to models.Contactable"), - ) - - return true + for { + // TODO(ashmrtn): Update to pass options once graph SDK dependency is updated. + resp, err := sendContactsDeltaGet(ctx, builder, options, gs.Adapter()) + if err != nil { + return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err)) } - if entry.GetId() == nil { - errs = multierror.Append(errs, errors.New("item with nil ID")) - return true + for _, item := range resp.GetValue() { + if item.GetId() == nil { + errs = multierror.Append( + errs, + errors.Errorf("contact with nil ID in folder %s", directoryID), + ) + + // TODO(ashmrtn): Handle fail-fast. + continue + } + + ids = append(ids, *item.GetId()) } - ids = append(ids, *entry.GetId()) + nextLinkIface := resp.GetAdditionalData()[nextLinkKey] + if nextLinkIface == nil { + break + } - return true - }) + nextLink := nextLinkIface.(*string) + if len(*nextLink) == 0 { + break + } - if err != nil { - return nil, - errors.Wrap( - err, - support.ConnectorStackErrorTrace(err)+ - " :fetching contactIDs from directory "+directoryID, - ) + builder = cdelta.NewDeltaRequestBuilder(*nextLink, gs.Adapter()) } return ids, errs.ErrorOrNil()