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
}
//nolint:unused
func sendContactsDeltaGet(
ctx context.Context,
m *mscontactdelta.DeltaRequestBuilder,

View File

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

View File

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