Contacts delta endpoint fetch (#1644)

## Description

Use delta endpoint to get contacts in each folder

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

part of:
* #1612 

merge after:
* #1620 

## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-12-01 21:30:14 -08:00 committed by GitHub
parent c04b3ac6cd
commit 3eae0f969d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 47 deletions

View File

@ -81,7 +81,6 @@ func sendMessagesDeltaGet(
return res.(msmaildelta.DeltaResponseable), nil return res.(msmaildelta.DeltaResponseable), nil
} }
//nolint:unused
func sendContactsDeltaGet( func sendContactsDeltaGet(
ctx context.Context, ctx context.Context,
m *mscontactdelta.DeltaRequestBuilder, m *mscontactdelta.DeltaRequestBuilder,

View File

@ -9,7 +9,6 @@ import (
mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders" mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders"
mscontactfolderitem "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item" mscontactfolderitem "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item"
mscontactfolderchild "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/childfolders" 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" mscontacts "github.com/microsoftgraph/msgraph-sdk-go/users/item/contacts"
msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/events" msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/events"
msfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders" msfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders"
@ -300,19 +299,18 @@ func optionsForMailFoldersItem(
} }
// optionsForContactFoldersItem is the same as optionsForContacts. // optionsForContactFoldersItem is the same as optionsForContacts.
// TODO: Remove after Issue #828; requires updating msgraph to v0.34
func optionsForContactFoldersItem( func optionsForContactFoldersItem(
moreOps []string, moreOps []string,
) (*mscontactfolderitemcontact.ContactsRequestBuilderGetRequestConfiguration, error) { ) (*DeltaRequestBuilderGetRequestConfiguration, error) {
selecting, err := buildOptions(moreOps, contacts) selecting, err := buildOptions(moreOps, contacts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
requestParameters := &mscontactfolderitemcontact.ContactsRequestBuilderGetQueryParameters{ requestParameters := &DeltaRequestBuilderGetQueryParameters{
Select: selecting, Select: selecting,
} }
options := &mscontactfolderitemcontact.ContactsRequestBuilderGetRequestConfiguration{ options := &DeltaRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters, QueryParameters: requestParameters,
} }

View File

@ -8,6 +8,7 @@ import (
multierror "github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models" "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" mdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders/item/messages/delta"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -217,61 +218,54 @@ func FetchEventIDsFromCalendar(
// FetchContactIDsFromDirectory function that returns a list of all the m365IDs of the contacts // FetchContactIDsFromDirectory function that returns a list of all the m365IDs of the contacts
// of the targeted directory // of the targeted directory
func FetchContactIDsFromDirectory(ctx context.Context, gs graph.Service, user, directoryID string) ([]string, error) { func FetchContactIDsFromDirectory(ctx context.Context, gs graph.Service, user, directoryID string) ([]string, error) {
var (
errs *multierror.Error
ids []string
)
options, err := optionsForContactFoldersItem([]string{"parentFolderId"}) options, err := optionsForContactFoldersItem([]string{"parentFolderId"})
if err != nil { if err != nil {
return nil, err return nil, errors.Wrap(err, "getting query options")
} }
ids := []string{} builder := gs.Client().
response, err := gs.Client().
UsersById(user). UsersById(user).
ContactFoldersById(directoryID). ContactFoldersById(directoryID).
Contacts(). Contacts().
Get(ctx, options) Delta()
for {
// TODO(ashmrtn): Update to pass options once graph SDK dependency is updated.
resp, err := sendContactsDeltaGet(ctx, builder, options, gs.Adapter())
if err != nil { if err != nil {
return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err)) return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err))
} }
pageIterator, err := msgraphgocore.NewPageIterator( for _, item := range resp.GetValue() {
response, if item.GetId() == nil {
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 = multierror.Append(
errs, errs,
errors.New("casting pageItem to models.Contactable"), errors.Errorf("contact with nil ID in folder %s", directoryID),
) )
return true // TODO(ashmrtn): Handle fail-fast.
continue
} }
if entry.GetId() == nil { ids = append(ids, *item.GetId())
errs = multierror.Append(errs, errors.New("item with nil ID"))
return true
} }
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 { builder = cdelta.NewDeltaRequestBuilder(*nextLink, gs.Adapter())
return nil,
errors.Wrap(
err,
support.ConnectorStackErrorTrace(err)+
" :fetching contactIDs from directory "+directoryID,
)
} }
return ids, errs.ErrorOrNil() return ids, errs.ErrorOrNil()