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:
parent
c04b3ac6cd
commit
3eae0f969d
@ -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,
|
||||||
|
|||||||
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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()
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
pageIterator, err := msgraphgocore.NewPageIterator(
|
for {
|
||||||
response,
|
// TODO(ashmrtn): Update to pass options once graph SDK dependency is updated.
|
||||||
gs.Adapter(),
|
resp, err := sendContactsDeltaGet(ctx, builder, options, gs.Adapter())
|
||||||
models.CreateContactCollectionResponseFromDiscriminatorValue,
|
if err != nil {
|
||||||
)
|
return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry.GetId() == nil {
|
for _, item := range resp.GetValue() {
|
||||||
errs = multierror.Append(errs, errors.New("item with nil ID"))
|
if item.GetId() == nil {
|
||||||
return true
|
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 {
|
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()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user