public async Task <List <ContactResponse> > UpdateEpaOrganisationPhoneNumber(UpdateEpaOrganisationPhoneNumberRequest updateEpaOrganisationPhoneNumberRequest)
 {
     using (var request = new HttpRequestMessage(HttpMethod.Put, $"api/ao/assessment-organisations/update-phone-number"))
     {
         return(await PostPutRequestWithResponse <UpdateEpaOrganisationPhoneNumberRequest, List <ContactResponse> >(request,
                                                                                                                    updateEpaOrganisationPhoneNumberRequest));
     }
 }
Esempio n. 2
0
        public async Task <IActionResult> UpdateEpaOrganisationPhoneNumber([FromBody] UpdateEpaOrganisationPhoneNumberRequest request)
        {
            try
            {
                _logger.LogInformation($"Amending the Organisation [{request.OrganisationId} with Phone Number {request.PhoneNumber}]");
                var result = await _mediator.Send(request);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                _logger.LogError($@"Bad request, Message: [{ex.Message}]");
                return(BadRequest());
            }
        }
        public async Task <List <ContactResponse> > Handle(UpdateEpaOrganisationPhoneNumberRequest request, CancellationToken cancellationToken)
        {
            var organisation = await _mediator.Send(new GetAssessmentOrganisationRequest { OrganisationId = request.OrganisationId });

            var updateEpaOrganisationRequest = Mapper.Map <UpdateEpaOrganisationRequest>(organisation);

            updateEpaOrganisationRequest.PhoneNumber = request.PhoneNumber;

            await _mediator.Send(updateEpaOrganisationRequest);

            var updatedBy = request.UpdatedBy.HasValue
                ? await _contactQueryRepository.GetContactById(request.UpdatedBy.Value)
                : null;

            return(await _mediator.Send(new SendOrganisationDetailsAmendedEmailRequest
            {
                OrganisationId = request.OrganisationId,
                PropertyChanged = "Contact phone number",
                ValueAdded = request.PhoneNumber,
                Editor = updatedBy?.DisplayName ?? "EFSA Staff"
            }));
        }
        public async Task <IActionResult> ChangePhoneNumber(ChangePhoneNumberViewModel vm)
        {
            var epaoid = _contextAccessor.HttpContext.User.FindFirst("http://schemas.portal.com/epaoid")?.Value;

            try
            {
                var organisation = await GetEpaOrganisation(epaoid);

                if (organisation == null)
                {
                    return(RedirectToAction(nameof(HomeController.NotRegistered), nameof(HomeController).RemoveController()));
                }

                if (vm.ActionChoice == "Save")
                {
                    if (ModelState.IsValid)
                    {
                        if (string.Equals(vm.PhoneNumber, organisation.OrganisationData?.PhoneNumber))
                        {
                            return(RedirectToAction(nameof(OrganisationDetails)));
                        }

                        // only check if a phone number has been entered - model has required validator
                        if (await _validationApiClient.ValidatePhoneNumber(vm.PhoneNumber) == false)
                        {
                            ModelState.AddModelError(nameof(ChangePhoneNumberViewModel.PhoneNumber), "Enter a valid phone number");
                        }
                    }

                    if (!ModelState.IsValid)
                    {
                        return(RedirectToAction(nameof(ChangePhoneNumber)));
                    }

                    vm = new ChangePhoneNumberViewModel
                    {
                        PhoneNumber = vm.PhoneNumber
                    };

                    return(View("ChangePhoneNumberConfirm", vm));
                }
                else if (vm.ActionChoice == "Confirm")
                {
                    var userId  = _contextAccessor.HttpContext.User.FindFirst("UserId").Value;
                    var request = new UpdateEpaOrganisationPhoneNumberRequest
                    {
                        PhoneNumber    = vm.PhoneNumber,
                        OrganisationId = organisation.OrganisationId,
                        UpdatedBy      = Guid.Parse(userId)
                    };

                    var notifiedContacts = await _organisationsApiClient.UpdateEpaOrganisationPhoneNumber(request);

                    if (notifiedContacts == null)
                    {
                        throw new Exception("Unable to update phone number");
                    }

                    vm = new ChangePhoneNumberViewModel
                    {
                        PhoneNumber = vm.PhoneNumber,
                        Contacts    = notifiedContacts
                    };

                    return(View("ChangePhoneNumberUpdated", vm));
                }
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "Failed to update phone number");
                ModelState.AddModelError(nameof(ChangePhoneNumberViewModel.PhoneNumber), "Unable to update the contact phone number at this time.");
                return(RedirectToAction(nameof(ChangePhoneNumber)));
            }

            return(RedirectToAction(nameof(OrganisationDetails)));
        }