/// <summary> /// Executes the workflow to send an e-mail to the specified customer. /// </summary> /// <param name="request">The request.</param> /// <returns>The response.</returns> protected override LinkToExistingCustomerResponse Process(InitiateLinkToExistingCustomerRequest request) { ThrowIf.Null(request, "request"); if (string.IsNullOrWhiteSpace(request.EmailAddressOfExistingCustomer)) { throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_RequiredValueNotFound, "An email address is required."); } ICommercePrincipal principal = request.RequestContext.GetPrincipal(); string externalIdentityId = principal.ExternalIdentityId; string externalIdentityIssuer = principal.ExternalIdentityIssuer; if (principal.IsEmployee) { throw new UserAuthorizationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_AuthorizationFailed, "Employee user is not authorized to perform this operation."); } if (string.IsNullOrEmpty(externalIdentityId)) { throw new SecurityException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_AuthenticationFailed, "The external identity is not set on the pricinpal"); } if (string.IsNullOrEmpty(externalIdentityIssuer)) { throw new SecurityException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_AuthenticationFailed, "The external identity issuer is not set on the pricinpal"); } // Validate the customer GlobalCustomer customer = this.GetCustomer(request.EmailAddressOfExistingCustomer); if (customer == null) { throw new DataValidationException( DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CustomerNotFound, string.Format("No customer found using the specified email address. The email address is '{0}'.", request.EmailAddressOfExistingCustomer)); } ICollection <NameValuePair> updatedEmailTemplateProperties = InitiateLinkToExistingCustomerRequestHandler.GetUpdatedEmailTemplateProperties(request.EmailTemplateProperties, request.ActivationToken); // Send email to customer this.SendCustomerEmail(customer.Email, request.EmailTemplateId, updatedEmailTemplateProperties); // Save the request to channel DB var serviceRequest = new InitiateLinkToExistingCustomerServiceRequest( request.EmailAddressOfExistingCustomer, request.ActivationToken, externalIdentityId, externalIdentityIssuer, customer.AccountNumber); var serviceResponse = this.Context.Execute <LinkToExistingCustomerServiceResponse>(serviceRequest); return(new LinkToExistingCustomerResponse(serviceResponse.LinkToExistingCustomerResult)); }
private GlobalCustomer GetCustomer(string emailAddress) { CustomerSearchCriteria criteria = new CustomerSearchCriteria { Keyword = emailAddress }; var searchCustomerRequest = new CustomersSearchServiceRequest(criteria, QueryResultSettings.AllRecords); var searchCustomerResponse = this.Context.Execute <CustomersSearchServiceResponse>(searchCustomerRequest); var matchedSearchResults = searchCustomerResponse.Customers.Results.Where(c => emailAddress.Equals(c.Email, System.StringComparison.OrdinalIgnoreCase)); GlobalCustomer foundCustomer = matchedSearchResults.FirstOrDefault(); return(foundCustomer); }