Example #1
0
        public async Task RemoveAsync(int userIdToRemove)
        {
            int requestedByUserId = GetClaimId(ClaimType.UserId);

            if (HasPermission(Permission.DeleteParticipants))
            {
                var user = await _userRepository.GetByIdAsync(userIdToRemove);

                if (!user.CanBeDeleted)
                {
                    throw new GraException($"{user.FullName} cannot be deleted.");
                }
                var familyCount = await _userRepository.GetHouseholdCountAsync(userIdToRemove);

                if (familyCount > 0)
                {
                    var group = await _groupInfoRepository.GetByUserIdAsync(user.Id);

                    string callIt = "family";
                    if (group != null)
                    {
                        callIt = "group";
                    }
                    throw new GraException($"{user.FullName} is the head of a {callIt}. Please remove all {callIt} members first.");
                }
                user.IsDeleted = true;
                user.Username  = null;
                await _userRepository.UpdateSaveAsync(requestedByUserId, user);
            }
            else
            {
                _logger.LogError($"User {requestedByUserId} doesn't have permission to remove user {userIdToRemove}.");
                throw new GraException("Permission denied.");
            }
        }