Abhishek Pandey c1c4218994
Add common code to check if services are enabled (#4127)
<!-- PR description-->

Currently corso CLI & SDK have different checks for services enabled. These checks have diverged which can lead to bugs. For e.g. SDK users use [code](3e43028a88/src/pkg/services/m365/users.go (L92)) to check if OD is enabled, while corso uses [this code](https://github.com/alcionai/corso/blob/main/src/pkg/services/m365/api/users.go#L174). These funcs have different checks. This PR introduces common helpers to consolidate these checks in one place.


- Note: I decided against absorbing these helpers into api/users.go, because separation of concerns -  getters shouldn't arbitrate on return vals. 
- Note that this code is not yet wired up to SDK/CLI. It'll be added in a following [PR](https://github.com/alcionai/corso/pull/4096). 


**Changes**
1. `Is*ServiceEnabled` helpers in internal/m365/common.go. 

2. Add `GetMailboxInfo` to common code. This is currently a duplicate of code in `GetInfo`. That function will be removed once we port SDK users to `GetMailboxInfo`. 

3. Unit tests for common code. Integration tests will be added in a later PR
- Note:`TestIsOneDriveServiceEnabled` is copied from `TestCheckUserHasDrives`. The older test will be removed in a later PR.


---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* https://github.com/alcionai/corso/issues/3844

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
2023-09-04 18:59:03 +00:00

101 lines
2.5 KiB
Go

package exchange
import (
"context"
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/pkg/logger"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)
type getMailInboxer interface {
GetMailInbox(ctx context.Context, userID string) (models.MailFolderable, error)
}
func IsServiceEnabled(
ctx context.Context,
gmi getMailInboxer,
resource string,
) (bool, error) {
_, err := gmi.GetMailInbox(ctx, resource)
if err != nil {
if err := api.EvaluateMailboxError(err); err != nil {
logger.CtxErr(ctx, err).Error("getting user's mail folder")
return false, clues.Stack(err)
}
logger.Ctx(ctx).Info("resource owner does not have a mailbox enabled")
return false, nil
}
return true, nil
}
type getMailboxer interface {
GetMailInbox(ctx context.Context, userID string) (models.MailFolderable, error)
GetMailboxSettings(ctx context.Context, userID string) (models.Userable, error)
GetFirstInboxMessage(ctx context.Context, userID, inboxID string) error
}
func GetMailboxInfo(
ctx context.Context,
gmb getMailboxer,
userID string,
) (api.MailboxInfo, error) {
mi := api.MailboxInfo{
ErrGetMailBoxSetting: []error{},
}
// First check whether the user is able to access their inbox.
inbox, err := gmb.GetMailInbox(ctx, userID)
if err != nil {
if err := api.EvaluateMailboxError(graph.Stack(ctx, err)); err != nil {
logger.CtxErr(ctx, err).Error("getting user's mail folder")
return mi, err
}
logger.Ctx(ctx).Info("resource owner does not have a mailbox enabled")
mi.ErrGetMailBoxSetting = append(
mi.ErrGetMailBoxSetting,
api.ErrMailBoxSettingsNotFound)
return mi, nil
}
mboxSettings, err := gmb.GetMailboxSettings(ctx, userID)
if err != nil {
logger.CtxErr(ctx, err).Info("err getting user's mailbox settings")
if !graph.IsErrAccessDenied(err) {
return mi, graph.Wrap(ctx, err, "getting user's mailbox settings")
}
logger.CtxErr(ctx, err).Info("mailbox settings access denied")
mi.ErrGetMailBoxSetting = append(
mi.ErrGetMailBoxSetting,
api.ErrMailBoxSettingsAccessDenied,
)
} else {
mi = api.ParseMailboxSettings(mboxSettings, mi)
}
err = gmb.GetFirstInboxMessage(ctx, userID, ptr.Val(inbox.GetId()))
if err != nil {
if !graph.IsErrQuotaExceeded(err) {
return mi, clues.Stack(err)
}
mi.QuotaExceeded = graph.IsErrQuotaExceeded(err)
}
return mi, nil
}