Ejemplo n.º 1
0
        public void Setup()
        {
            _registerRepository = new Mock <IRegisterRepository>();
            _cleanserService    = new Mock <ISpecialCharacterCleanserService>();
            _validator          = new Mock <IEpaOrganisationValidator>();
            _logger             = new Mock <ILogger <CreateEpaOrganisationContactHandler> >();
            _idGenerator        = new Mock <IEpaOrganisationIdGenerator>();
            _organisationId     = "EPA999";
            _firstName          = "Testy";
            _lastName           = "McTestFace";

            _email                               = "*****@*****.**";
            _phoneNumber                         = "555 5555";
            _requestNoIssuesUserName             = "******";
            _requestNoIssues                     = BuildRequest(_organisationId, _firstName, _lastName, _email, _phoneNumber);
            _expectedOrganisationContactNoIssues = BuildOrganisationContact(_requestNoIssues, _requestNoIssuesUserName);

            _registerRepository.Setup(r => r.CreateEpaOrganisationContact(It.IsAny <EpaContact>()))
            .Returns(Task.FromResult(_expectedOrganisationContactNoIssues.Username));

            _cleanserService.Setup(c => c.CleanseStringForSpecialCharacters(It.IsAny <string>()))
            .Returns((string s) => s);

            _validator.Setup(v => v.ValidatorCreateEpaOrganisationContactRequest(_requestNoIssues))
            .Returns(new ValidationResponse());
            _idGenerator.Setup(i => i.GetNextContactUsername()).Returns(_requestNoIssuesUserName);

            _createEpaOrganisationContactHandler = new CreateEpaOrganisationContactHandler(_registerRepository.Object, _validator.Object, _cleanserService.Object, _logger.Object, _idGenerator.Object);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateOrganisationContact([FromBody] CreateEpaOrganisationContactRequest request)
        {
            try
            {
                _logger.LogInformation("Creating new Organisation Contact");
                var result = await _mediator.Send(request);

                return(Ok(new EpaOrganisationContactResponse(result)));
            }
            catch (AlreadyExistsException ex)
            {
                _logger.LogError($@"Record already exists for organisation/email [{request.EndPointAssessorOrganisationId}, {request.Email}]");
                return(Conflict(new EpaOrganisationContactResponse(ex.Message)));
            }
            catch (BadRequestException ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(new EpaOrganisationContactResponse(ex.Message)));
            }
            catch (Exception ex)
            {
                _logger.LogError($@"Bad request, Message: [{ex.Message}]");
                return(BadRequest());
            }
        }
        public ValidationResponse ValidatorCreateEpaOrganisationContactRequest(
            CreateEpaOrganisationContactRequest request)
        {
            var validationResult = new ValidationResponse();

            RunValidationCheckAndAppendAnyError("Email", CheckIfEmailIsPresentAndInSuitableFormat(request.Email),
                                                validationResult, ValidationStatusCode.BadRequest);
            RunValidationCheckAndAppendAnyError("Email",
                                                CheckIfEmailAlreadyPresentInAnotherOrganisation(request.Email, request.EndPointAssessorOrganisationId),
                                                validationResult, ValidationStatusCode.AlreadyExists);
            RunValidationCheckAndAppendAnyError("Email",
                                                EmailAlreadyPresent(request.Email),
                                                validationResult, ValidationStatusCode.AlreadyExists);


            if (validationResult.IsValid)
            {
                RunValidationCheckAndAppendAnyError("ContactDetails",
                                                    CheckIfContactDetailsAlreadyPresentInSystem(request.FirstName, request.LastName, request.Email, request.PhoneNumber,
                                                                                                null), validationResult, ValidationStatusCode.AlreadyExists);
            }

            RunValidationCheckAndAppendAnyError("EndPointAssessorOrganisationId",
                                                CheckIfOrganisationNotFound(request.EndPointAssessorOrganisationId), validationResult,
                                                ValidationStatusCode.BadRequest);
            RunValidationCheckAndAppendAnyError("FirstName", CheckFirstName(request.FirstName), validationResult,
                                                ValidationStatusCode.BadRequest);
            RunValidationCheckAndAppendAnyError("LastName", CheckLastName(request.LastName), validationResult,
                                                ValidationStatusCode.BadRequest);

            return(validationResult);
        }
        private async Task AssignPrimaryContactToOrganisation(CreateEpaOrganisationContactRequest primaryContact, string organisationId)
        {
            if (primaryContact != null && !string.IsNullOrEmpty(organisationId))
            {
                var primaryContactId = Guid.Empty;

                var assessorContact = await _apiClient.GetEpaContactByEmail(primaryContact.Email);

                if (assessorContact is null)
                {
                    _logger.LogInformation($"Creating a new primary contact ({primaryContact.Email}) for {organisationId}");
                    var validationResponse = await _assessorValidationService.ValidateNewContactRequest(primaryContact);

                    if (!validationResponse.IsValid)
                    {
                        _logger.LogWarning($"Cannot create new primary contact in assessor for {organisationId}. Validation errors: {validationResponse.Errors.Select(err => err.ErrorMessage)}");
                    }
                    else
                    {
                        //Create a new contact in assessor table,
                        //Assumption is that this user will need to have an account created in aslogon too
                        //And then when they login the signinid etc wll get populated as it does for existing users
                        var id = await _apiClient.CreateEpaContact(primaryContact);

                        if (Guid.TryParse(id, out primaryContactId))
                        {
                            _logger.LogInformation($"Contact created successfully - {primaryContactId}");
                        }
                    }
                }
                else
                {
                    _logger.LogInformation($"Primary contact ({primaryContact.Email}) already exists");
                    primaryContactId = assessorContact.Id;
                }

                if (primaryContactId != Guid.Empty)
                {
                    _logger.LogInformation($"Associating primary contact ({primaryContact.Email}) to organisation {organisationId}");
                    var request = new AssociateEpaOrganisationWithEpaContactRequest
                    {
                        ContactId            = primaryContactId,
                        OrganisationId       = organisationId,
                        ContactStatus        = ContactStatus.Live,
                        MakePrimaryContact   = true,
                        AddDefaultRoles      = true,
                        AddDefaultPrivileges = false
                    };

                    await _apiClient.AssociateOrganisationWithEpaContact(request);
                }
            }
        }
Ejemplo n.º 5
0
 private EpaContact BuildOrganisationContact(CreateEpaOrganisationContactRequest request, string username)
 {
     return(new EpaContact
     {
         Id = Guid.NewGuid(),
         EndPointAssessorOrganisationId = request.EndPointAssessorOrganisationId,
         DisplayName = $"{request.FirstName} {request.LastName}",
         Username = username,
         Email = request.Email,
         Status = "Live",
         PhoneNumber = request.PhoneNumber
     });
 }
Ejemplo n.º 6
0
        public async Task <IActionResult> AddContact(RegisterAddContactViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var addContactRequest = new CreateEpaOrganisationContactRequest
            {
                EndPointAssessorOrganisationId = viewModel.EndPointAssessorOrganisationId,
                FirstName   = viewModel.FirstName,
                LastName    = viewModel.LastName,
                Email       = viewModel.Email,
                PhoneNumber = viewModel.PhoneNumber
            };

            var contactId = await _apiClient.CreateEpaContact(addContactRequest);

            return(Redirect($"/register/view-contact/{contactId}"));
        }
        public void Arrange()
        {
            _mediator = new Mock <IMediator>();
            _logger   = new Mock <ILogger <RegisterController> >();

            _request = new CreateEpaOrganisationContactRequest
            {
                EndPointAssessorOrganisationId = "EPA0009",
                FirstName   = "Tester",
                LastName    = "McTestFace",
                Email       = "*****@*****.**",
                PhoneNumber = "555 5555"
            };

            _mediator.Setup(m =>
                            m.Send(_request, new CancellationToken())).Throws <BadRequestException>();

            _controller = new RegisterController(_mediator.Object, _logger.Object);
            _result     = _controller.CreateOrganisationContact(_request).Result;
        }
        public async Task <string> CreateEpaContact(CreateEpaOrganisationContactRequest request)
        {
            var result = await Post <CreateEpaOrganisationContactRequest, EpaOrganisationContactResponse>("api/ao/assessment-organisations/contacts", request);

            return(result.Details);
        }
Ejemplo n.º 9
0
 private CreateEpaContactValidationRequest MapToCreateEpaContactValidateRequest(CreateEpaOrganisationContactRequest request)
 {
     return(new CreateEpaContactValidationRequest
     {
         OrganisationId = request?.EndPointAssessorOrganisationId,
         FirstName = request?.FirstName,
         LastName = request?.LastName,
         Email = request?.Email,
         Phone = request?.PhoneNumber
     });
 }
Ejemplo n.º 10
0
        public async Task <ValidationResponse> ValidateNewContactRequest(CreateEpaOrganisationContactRequest request)
        {
            var validationRequest = MapToCreateEpaContactValidateRequest(request);

            return(await _apiClient.CreateEpaContactValidate(validationRequest));
        }