public async Task <List <ContactResponse> > Handle(UpdateEpaOrganisationPrimaryContactRequest request, CancellationToken cancellationToken)
        {
            var organisation = await _mediator.Send(new GetAssessmentOrganisationRequest { OrganisationId = request.OrganisationId });

            var success = await _mediator.Send(new AssociateEpaOrganisationWithEpaContactRequest
            {
                ContactId            = request.PrimaryContactId,
                OrganisationId       = organisation.OrganisationId,
                ContactStatus        = ContactStatus.Live,
                MakePrimaryContact   = true,
                AddDefaultRoles      = false,
                AddDefaultPrivileges = false
            });

            if (success)
            {
                var primaryContact = await _contactQueryRepository.GetContactById(request.PrimaryContactId);

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

                try
                {
                    var primaryContactaAmendedEmailTemplate = await _eMailTemplateQueryRepository.GetEmailTemplate(EmailTemplateNames.EPAOPrimaryContactAmended);

                    if (primaryContactaAmendedEmailTemplate != null)
                    {
                        _logger.LogInformation($"Sending email to notify updated primary contact {primaryContact.Username} for organisation {organisation.Name}");

                        await _mediator.Send(new SendEmailRequest(primaryContact.Email,
                                                                  primaryContactaAmendedEmailTemplate, new
                        {
                            Contact = primaryContact.GivenNames,
                            ServiceName = "Apprenticeship assessment service",
                            Organisation = organisation.Name,
                            ServiceTeam = "Apprenticeship assessment services team",
                            Editor = updatedBy?.DisplayName ?? "EFSA Staff"
                        }), cancellationToken);
                    }
                }
                catch (Exception)
                {
                    _logger.LogInformation($"Unable to send email to notify updated primary contact {primaryContact.Username} for organisation {organisation.Name}");
                }

                return(await _mediator.Send(new SendOrganisationDetailsAmendedEmailRequest
                {
                    OrganisationId = request.OrganisationId,
                    PropertyChanged = "Contact name",
                    ValueAdded = primaryContact.DisplayName,
                    Editor = updatedBy?.DisplayName ?? "ESFA Staff"
                }));
            }

            return(null);
        }
示例#2
0
        public async Task <IActionResult> UpdateEpaOrganisationPrimaryContact([FromBody] UpdateEpaOrganisationPrimaryContactRequest request)
        {
            try
            {
                _logger.LogInformation($"Amending the Organisation [{request.OrganisationId} with Primary Contact {request.PrimaryContactId}]");
                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> > UpdateEpaOrganisationPrimaryContact(UpdateEpaOrganisationPrimaryContactRequest updateEpaOrganisationPrimaryContactRequest)
 {
     using (var request = new HttpRequestMessage(HttpMethod.Put, $"api/ao/assessment-organisations/update-primary-contact"))
     {
         return(await PostPutRequestWithResponse <UpdateEpaOrganisationPrimaryContactRequest, List <ContactResponse> >(request,
                                                                                                                       updateEpaOrganisationPrimaryContactRequest));
     }
 }
        public async Task <IActionResult> SelectOrChangeContactName(SelectOrChangeContactNameViewModel vm)
        {
            var epaoid = _contextAccessor.HttpContext.User.FindFirst("http://schemas.portal.com/epaoid")?.Value;

            try
            {
                var organisation = await _organisationsApiClient.GetEpaOrganisation(epaoid);

                var primaryContact = !string.IsNullOrEmpty(vm.PrimaryContact)
                    ? await _contactsApiClient.GetByUsername(vm.PrimaryContact)
                    : null;

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

                        if (organisation.Id != primaryContact?.OrganisationId)
                        {
                            ModelState.AddModelError(nameof(SelectOrChangeContactNameViewModel.PrimaryContact), "The contact name cannot be changed to a contact of a different organisation");
                        }
                    }

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

                    vm = new SelectOrChangeContactNameViewModel
                    {
                        Contacts           = null,
                        PrimaryContact     = vm.PrimaryContact,
                        PrimaryContactName = primaryContact.DisplayName
                    };

                    return(View("SelectOrChangeContactNameConfirm", vm));
                }
                else if (vm.ActionChoice == "Confirm")
                {
                    var userId  = _contextAccessor.HttpContext.User.FindFirst("UserId").Value;
                    var request = new UpdateEpaOrganisationPrimaryContactRequest
                    {
                        PrimaryContactId = primaryContact.Id,
                        OrganisationId   = organisation.OrganisationId,
                        UpdatedBy        = Guid.Parse(userId)
                    };

                    var notifiedContacts = await _organisationsApiClient.UpdateEpaOrganisationPrimaryContact(request);

                    if (notifiedContacts != null)
                    {
                        vm = new SelectOrChangeContactNameViewModel
                        {
                            Contacts           = notifiedContacts,
                            PrimaryContact     = vm.PrimaryContact,
                            PrimaryContactName = primaryContact.DisplayName
                        };

                        return(View("SelectOrChangeContactNameUpdated", vm));
                    }
                    else
                    {
                        ModelState.AddModelError(nameof(SelectOrChangeContactNameViewModel.PrimaryContact), "Unable to update the contact name at this time.");
                        return(RedirectToAction(nameof(SelectOrChangeContactName)));
                    }
                }
            }
            catch (EntityNotFoundException e)
            {
                _logger.LogWarning(e, "Failed to find organisation");
                return(RedirectToAction(nameof(HomeController.NotRegistered), nameof(HomeController).RemoveController()));
            }

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