public async Task <IActionResult> CreateWorkerContact([FromBody] ViewModels.Contact item) { // get the current user. UserSettings userSettings = UserSettings.CreateFromHttpContext(_httpContextAccessor); // first check to see that we have the correct inputs. var contactSiteminderGuid = userSettings.SiteMinderGuid; if (contactSiteminderGuid == null || contactSiteminderGuid.Length == 0) { _logger.LogDebug(LoggingEvents.Error, "No Contact Siteminder Guid exernal id"); throw new Exception("Error. No ContactSiteminderGuid exernal id"); } // get the contact record. MicrosoftDynamicsCRMcontact userContact = null; // see if the contact exists. try { userContact = _dynamicsClient.GetActiveContactByExternalId(contactSiteminderGuid); if (userContact != null) { throw new Exception("Contact already Exists"); } } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error getting contact by Siteminder Guid."); throw new HttpOperationException("Error getting contact by Siteminder Guid"); } // create a new contact. var contact = new MicrosoftDynamicsCRMcontact(); var worker = new MicrosoftDynamicsCRMadoxioWorker { AdoxioFirstname = item.firstname, AdoxioMiddlename = item.middlename, AdoxioLastname = item.lastname, AdoxioIsmanual = 0 // 0 for false - is a portal user. }; contact.CopyValues(item); // set the type to Retail Worker. contact.Customertypecode = 845280000; if (userSettings.NewWorker != null) { // get additional information from the service card headers. contact.CopyContactUserSettings(userSettings.NewContact); worker.CopyValues(userSettings.NewWorker); } //Default the country to Canada if (string.IsNullOrEmpty(contact.Address1Country)) { contact.Address1Country = "Canada"; } if (string.IsNullOrEmpty(contact.Address2Country)) { contact.Address2Country = "Canada"; } contact.AdoxioExternalid = DynamicsExtensions.GetServiceCardID(contactSiteminderGuid); try { worker.AdoxioContactId = contact; worker = await _dynamicsClient.Workers.CreateAsync(worker); contact = await _dynamicsClient.GetContactById(Guid.Parse(worker._adoxioContactidValue)); await CreateSharepointDynamicsLink(worker); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating contact"); _logger.LogError(httpOperationException.Response.Content); //fail throw httpOperationException; } // if we have not yet authenticated, then this is the new record for the user. if (userSettings.IsNewUserRegistration) { userSettings.ContactId = contact.Contactid; // we can now authenticate. if (userSettings.AuthenticatedUser == null) { var user = new User(); user.Active = true; user.ContactId = Guid.Parse(userSettings.ContactId); user.UserType = userSettings.UserType; user.SmUserId = userSettings.UserId; userSettings.AuthenticatedUser = user; } userSettings.IsNewUserRegistration = false; var userSettingsString = JsonConvert.SerializeObject(userSettings); _logger.LogDebug("userSettingsString --> " + userSettingsString); // add the user to the session. _httpContextAccessor.HttpContext.Session.SetString("UserSettings", userSettingsString); _logger.LogDebug("user added to session. "); } else { _logger.LogDebug(LoggingEvents.Error, "Invalid user registration."); throw new Exception("Invalid user registration."); } return(new JsonResult(contact.ToViewModel())); }
public async Task <IActionResult> CreateContact([FromBody] ViewModels.Contact item) { // get the current user. UserSettings userSettings = UserSettings.CreateFromHttpContext(_httpContextAccessor); // first check to see that a contact exists. var contactSiteminderGuid = userSettings.SiteMinderGuid; if (contactSiteminderGuid == null || contactSiteminderGuid.Length == 0) { _logger.LogDebug(LoggingEvents.Error, "No Contact Siteminder Guid exernal id"); throw new Exception("Error. No ContactSiteminderGuid exernal id"); } // get the contact record. MicrosoftDynamicsCRMcontact userContact = null; // see if the contact exists. try { userContact = _dynamicsClient.GetActiveContactByExternalId(contactSiteminderGuid); if (userContact != null) { throw new Exception("Contact already Exists"); } } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error getting contact by Siteminder Guid."); throw new HttpOperationException("Error getting contact by Siteminder Guid"); } // create a new contact. var contact = new MicrosoftDynamicsCRMcontact(); contact.CopyValues(item); if (userSettings.IsNewUserRegistration) { // get additional information from the service card headers. contact.CopyHeaderValues(_httpContextAccessor); } contact.AdoxioExternalid = DynamicsExtensions.GetServiceCardID(contactSiteminderGuid); try { contact = await _dynamicsClient.Contacts.CreateAsync(contact); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error creating contact. "); } catch (Exception e) { _logger.LogError(e, "Unknown error creating contact."); } // if we have not yet authenticated, then this is the new record for the user. if (userSettings.IsNewUserRegistration) { userSettings.ContactId = contact.Contactid; // we can now authenticate. if (userSettings.AuthenticatedUser == null) { var user = new User(); user.Active = true; user.ContactId = Guid.Parse(userSettings.ContactId); user.UserType = userSettings.UserType; user.SmUserId = userSettings.UserId; userSettings.AuthenticatedUser = user; } userSettings.IsNewUserRegistration = false; var userSettingsString = JsonConvert.SerializeObject(userSettings); _logger.LogDebug("userSettingsString --> " + userSettingsString); // add the user to the session. _httpContextAccessor.HttpContext.Session.SetString("UserSettings", userSettingsString); _logger.LogDebug("user added to session. "); } else { _logger.LogDebug(LoggingEvents.Error, "Invalid user registration."); throw new Exception("Invalid user registration."); } return(new JsonResult(contact.ToViewModel())); }