Compare commits

...

3 Commits
main ... cor-84

Author SHA1 Message Date
Abhishek Pandey
1bd3d61f77 Fetch folder by display name 2023-05-02 18:43:32 -07:00
Abhishek Pandey
4af8678c8a Remove some dead code 2023-05-01 20:51:49 -07:00
Abhishek Pandey
c641e457fd Add initial commit 2023-05-01 20:25:47 -07:00
3 changed files with 52 additions and 4 deletions

View File

@ -55,6 +55,41 @@ func (c Contacts) CreateContactFolder(
return mdl, nil
}
// GetContactFolderByDisplayName fetches contact folder which has the displayName of
// folderName. This is only done at root level.
func (c Contacts) GetContactFolderByDisplayName(
ctx context.Context,
user, folderName string,
) (models.ContactFolderable, error) {
filter := fmt.Sprintf("displayName eq '%s'", folderName)
options := &users.ItemContactFoldersRequestBuilderGetRequestConfiguration{
QueryParameters: &users.ItemContactFoldersRequestBuilderGetQueryParameters{
Filter: &filter,
},
}
resp, err := c.Stable.Client().UsersById(user).ContactFolders().Get(ctx, options)
if err != nil {
return nil, graph.Wrap(ctx, err, "getting contact folder").
With("folder name", folderName)
}
// We only expect one folder with provided folderName. Return an error if
// multiple folders exist(unlikely) or if no folder was found
if len(resp.GetValue()) != 1 {
return nil, graph.Wrap(ctx, err, "unexpected number of folders").
With("folder count", len(resp.GetValue())).
With("folder name", folderName)
}
fold := resp.GetValue()[0]
if err := checkIDAndName(fold); err != nil {
return nil, err
}
return fold, nil
}
// DeleteContainer deletes the ContactFolder associated with the M365 ID if permissions are valid.
func (c Contacts) DeleteContainer(
ctx context.Context,

View File

@ -651,19 +651,27 @@ func establishContactsRestoreLocation(
ctx = clues.Add(ctx, "is_new_cache", isNewCache)
temp, err := ac.Contacts().CreateContactFolder(ctx, user, folders[0])
if err != nil {
fold, err := ac.Contacts().CreateContactFolder(ctx, user, folders[0])
// 409 handling: Fetch folder if it exists.
// This is rare, but it may happen if an earlier CreateContactFolder POST failed
// with 5xx, potentially leaving dirty state in graph.
if graph.IsErrFolderExists(err) {
fold, err = ac.Contacts().GetContactFolderByDisplayName(ctx, user, folders[0])
if err != nil {
return "", err
}
} else if err != nil {
return "", err
}
folderID := ptr.Val(temp.GetId())
folderID := ptr.Val(fold.GetId())
if isNewCache {
if err := cfc.Populate(ctx, errs, folderID, folders[0]); err != nil {
return "", clues.Wrap(err, "populating contact cache")
}
if err = cfc.AddToCache(ctx, temp); err != nil {
if err = cfc.AddToCache(ctx, fold); err != nil {
return "", clues.Wrap(err, "adding contact folder to cache")
}
}

View File

@ -41,6 +41,7 @@ const (
syncFolderNotFound errorCode = "ErrorSyncFolderNotFound"
syncStateInvalid errorCode = "SyncStateInvalid"
syncStateNotFound errorCode = "SyncStateNotFound"
folderExists errorCode = "ErrorFolderExists"
)
const (
@ -172,6 +173,10 @@ func IsMalwareResp(ctx context.Context, resp *http.Response) bool {
return false
}
func IsErrFolderExists(err error) bool {
return hasErrorCode(err, folderExists)
}
// ---------------------------------------------------------------------------
// error parsers
// ---------------------------------------------------------------------------