public async Task <IActionResult> GetContactBySignInId(string signInId)
        {
            Contact contact = null;

            _logger.LogInformation($" Get Request using signin id = {signInId}");
            try
            {
                var guidId = Guid.Parse(signInId);
                contact = await _contactQueryRepository.GetBySignInId(guidId);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to retrieve contact with signin id : {signInId}");
            }

            if (contact == null)
            {
                throw new ResourceNotFoundException();
            }

            return(Ok(Mapper.Map <ContactResponse>(contact)));
        }
        public async Task <LoginResponse> Handle(LoginRequest request, CancellationToken cancellationToken)
        {
            var response = new LoginResponse();

            var contact = await _contactQueryRepository.GetBySignInId(request.SignInId);

            //Check if username is null or starts with unnown then update the username with users email
            var originalUsername = contact.Username;

            if (string.IsNullOrEmpty(originalUsername) ||
                originalUsername.StartsWith("unknown-", StringComparison.CurrentCultureIgnoreCase))
            {
                await _contactRepository.UpdateUserName(contact.Id, contact.Email);
            }

            if (UserDoesNotHaveAcceptableRole(contact.Id))
            {
                _logger.LogInformation("Invalid Role");
                _logger.LogInformation(LoggingConstants.SignInIncorrectRole);
                response.Result = LoginResult.InvalidRole;
                return(response);
            }

            _logger.LogInformation("Role is good");

            if (contact.OrganisationId == null)
            {
                // This user has no organisation... send them off to find one.
                response.Result = LoginResult.NotRegistered;
                return(response);
            }

            var organisation = await _organisationQueryRepository.Get(contact.OrganisationId.Value);


            if (organisation == null)
            {
                _logger.LogInformation($"Org not registered");
                _logger.LogInformation(LoggingConstants.SignInNotAnEpao);
                response.Result = LoginResult.NotRegistered;
                return(response);
            }

            //ON-1926 If there was an organisation associated with the current contact then if the primary contact in
            //that organisation matches the orginal username then make sure it is updated to reflect the latest username.
            if (!string.IsNullOrEmpty(originalUsername))
            {
                await _registerRepository.UpdateEpaOrganisationPrimaryContact(contact.Id, originalUsername);
            }

            await CheckAndSetPrivileges(contact);

            response.EndPointAssessorOrganisationId = organisation.EndPointAssessorOrganisationId;

            _logger.LogInformation($"Got Org with ukprn: {organisation.EndPointAssessorUkprn}, Id: {organisation.EndPointAssessorOrganisationId}, Status: {organisation.Status}");

            if (organisation.Status == OrganisationStatus.Deleted)
            {
                _logger.LogInformation(LoggingConstants.SignInEpaoDeleted);
                response.Result = LoginResult.NotRegistered;
                return(response);
            }
            else if (organisation.Status == OrganisationStatus.New)
            {
                _logger.LogInformation(LoggingConstants.SignInEpaoNew);

                // Only show true status if contact is marked as being Live
                response.Result = (contact.Status is ContactStatus.Live) ? LoginResult.NotActivated : LoginResult.InvitePending;
                return(response);
            }
            else if (organisation.Status == OrganisationStatus.Applying)
            {
                _logger.LogInformation(LoggingConstants.SignInEpaoNew);

                response.Result = (contact.Status is ContactStatus.Live) ? LoginResult.Applying : LoginResult.NotActivated;
                return(response);
            }
            else
            {
                _logger.LogInformation(LoggingConstants.SignInSuccessful);

                var status = contact.Status; //await GetUserStatus(organisation.EndPointAssessorOrganisationId, request.SignInId);
                switch (status)
                {
                case ContactStatus.Live:
                    response.Result = LoginResult.Valid;
                    break;

                case ContactStatus.InvitePending:
                    response.Result = LoginResult.InvitePending;
                    break;

                case ContactStatus.Inactive:
                    response.Result = LoginResult.Rejected;
                    break;

                case ContactStatus.Applying:
                    response.Result = LoginResult.Applying;
                    break;

                default:
                    response.Result = LoginResult.NotRegistered;
                    break;
                }
            }

            return(response);
        }