From 5b87fa629ebb244350dd053ad1813eecf71af133 Mon Sep 17 00:00:00 2001 From: jrmccannon Date: Wed, 19 Mar 2025 12:15:16 -0500 Subject: [PATCH] Cleaned up return. --- .../src/Scim/Users/PostUserCommand.cs | 40 +++++++++---------- .../InviteOrganizationUsersCommand.cs | 3 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/bitwarden_license/src/Scim/Users/PostUserCommand.cs b/bitwarden_license/src/Scim/Users/PostUserCommand.cs index d05188ebc4..574221690c 100644 --- a/bitwarden_license/src/Scim/Users/PostUserCommand.cs +++ b/bitwarden_license/src/Scim/Users/PostUserCommand.cs @@ -70,23 +70,27 @@ public class PostUserCommand( if (featureService.IsEnabled(FeatureFlagKeys.ScimInviteUserOptimization)) { - return await InviteScimOrganizationUserAsync(model, organization, scimProvider, hasStandaloneSecretsManager); + return await InviteScimOrganizationUserAsync(model, organization, scimProvider, + hasStandaloneSecretsManager); } - var invitedOrgUser = await organizationService.InviteUserAsync(organizationId, invitingUserId: null, EventSystemUser.SCIM, + var invitedOrgUser = await organizationService.InviteUserAsync(organizationId, invitingUserId: null, + EventSystemUser.SCIM, invite, externalId); var orgUser = await organizationUserRepository.GetDetailsByIdAsync(invitedOrgUser.Id); return orgUser; } - private async Task InviteScimOrganizationUserAsync(ScimUserRequestModel model, Organization organization, ScimProviderType scimProvider, bool hasStandaloneSecretsManager) + private async Task InviteScimOrganizationUserAsync(ScimUserRequestModel model, + Organization organization, ScimProviderType scimProvider, bool hasStandaloneSecretsManager) { var plan = await pricingClient.GetPlan(organization.PlanType); if (plan == null) { - logger.LogError("Plan {planType} not found for organization {organizationId}", organization.PlanType, organization.Id); + logger.LogError("Plan {planType} not found for organization {organizationId}", organization.PlanType, + organization.Id); return null; } @@ -98,24 +102,20 @@ public class PostUserCommand( var result = await inviteOrganizationUsersCommand.InviteScimOrganizationUserAsync(request); - if (result is Failure failure) + var invitedUserId = result switch { - if (failure.ErrorMessages.Count != 0) - { - throw new BadRequestException(failure.ErrorMessage); - } + Success success => success.Value.InvitedUser.OrganizationId, + Failure { ErrorMessage: InviteOrganizationUsersCommand.NoUsersToInvite } => (Guid?)null, + Failure failure when failure.ErrorMessages.Count != 0 => throw new BadRequestException(failure.ErrorMessage), + _ => throw new InvalidOperationException() + }; - return null; - } - - if (result is not Success successfulResponse) - { - return null; - } - - var invitedUser = await organizationUserRepository.GetDetailsByIdAsync(successfulResponse.Value.InvitedUser.Id); - - return invitedUser; + var organizationUser = invitedUserId.HasValue + ? await organizationUserRepository.GetDetailsByIdAsync(invitedUserId.Value) + : null; + return organizationUser; } + + } diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/InviteOrganizationUsersCommand.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/InviteOrganizationUsersCommand.cs index fbd5f5eecf..94392cdb34 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/InviteOrganizationUsersCommand.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/InviteOrganizationUsersCommand.cs @@ -37,6 +37,7 @@ public class InviteOrganizationUsersCommand(IEventService eventService, public const string IssueNotifyingOwnersOfSeatLimitReached = "Error encountered notifying organization owners of seat limit reached."; public const string FailedToInviteUsers = "Failed to invite user(s)."; public const string NoUsersToInvite = "No users to invite."; + public const string InvalidResultType = "Invalid result type."; public async Task> InviteScimOrganizationUserAsync(OrganizationUserSingleEmailInvite request) { @@ -62,7 +63,7 @@ public class InviteOrganizationUsersCommand(IEventService eventService, }); default: - return new Failure("WAHT"); + return new Failure(InvalidResultType); } }