public async Task <GroupMemberDetails> GetGroupMembershipUserAsync(Guid userId, Guid targetUserId, string slug, CancellationToken cancellationToken) { if (Guid.Empty == userId) { throw new ArgumentOutOfRangeException(nameof(userId)); } if (Guid.Empty == targetUserId) { throw new ArgumentOutOfRangeException(nameof(targetUserId)); } if (string.IsNullOrEmpty(slug)) { throw new ArgumentOutOfRangeException(nameof(slug)); } var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(userId, slug, EditUserRole, cancellationToken); if (!userCanPerformAction) { _logger.LogError($"Error: GetGroupMembershipUserAsync - User:{0} does not have access to group:{1}", userId, slug); throw new ForbiddenException($"Error: User does not have access"); } var groupId = await _groupCommand.GetGroupIdForSlugAsync(slug, cancellationToken); if (!groupId.HasValue) { _logger.LogError($"Error: GetGroupMembershipUserAsync - Group not found for slug:{0}", slug); throw new KeyNotFoundException("Error: Group not found for slug"); } var groupUserResult = await _groupCommand.GetGroupUserAsync(targetUserId, groupId.Value, cancellationToken); if (groupUserResult is null) { _logger.LogError($"Error: GetGroupMembershipUserAsync - User:{0} not found in group:{1}", targetUserId, slug); throw new KeyNotFoundException("Error: User not found in group"); } var userResult = await _userCommand.GetUserAsync(groupUserResult.MembershipUser, cancellationToken); if (userResult is null) { _logger.LogError($"Error: GetGroupMembershipUserAsync - User:{0} not found in group:{1}", groupUserResult.MembershipUser, slug); throw new KeyNotFoundException("Error: User not found in group"); } var roleResult = await _rolesCommand.GetRoleAsync(groupUserResult.MembershipRole, cancellationToken); if (roleResult is null) { _logger.LogError($"Error: GetGroupMembershipUserAsync - Role:{0} not found in group:{1}", groupUserResult.MembershipRole, slug); throw new KeyNotFoundException("Error: User not found in group"); } var membershipUser = new GroupMemberDetails { Id = userResult.Id, UserName = userResult.UserName, Slug = userResult.Slug, FirstName = userResult.FirstName, LastName = userResult.LastName, Initials = userResult.Initials, Email = userResult.Email, DateJoinedUtc = groupUserResult.ApprovedDateUTCAsString, LastLoginUtc = userResult.LastLoginDateUtc, RoleId = roleResult.Id, Role = roleResult.Name, RowVersion = groupUserResult.RowVersion, Image = userResult.Image }; return(membershipUser); }