Ejemplo n.º 1
0
 private AutopilotProvider()
 {
     if (autopilotAPIClient == null)
     {
         autopilotAPIClient = AutopilotAPIClient.Create();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts on registration or external registration(G+,FB). Creates autopilot contact and associates it with created user.
        /// </summary>
        /// <param name="model">model of registration</param>
        /// <param name="userId">id of created user</param>
        /// <returns></returns>
        public async Task <string> CreateAutopilotContact(RegisterGeneralModel model, string userId)
        {
            var autopilotContact = new Contact();

            autopilotContact.FirstName  = model.FirstName;
            autopilotContact.LastName   = model.LastName;
            autopilotContact.Email      = model.Email;
            autopilotContact.LeadSource = "AdvisrApp";
            autopilotContact.ListToAdd  = ConfigurationManager.AppSettings["RegistrationList"];
            autopilotContact.Custom     = new Custom();
            autopilotContact.Custom.PolicyLoadInProgress = false;
            autopilotContact.Custom.LastAppLogin         = DateTime.Now;
            autopilotContact.Custom.PolicyCount          = 0;
            autopilotContact.Custom.AppLoginCount        = 0;
            autopilotContact.Custom.FirstPolicyLoad      = false;

            autopilotAPIClient = AutopilotAPIClient.Create();

            string autopilotcontact_id = "";
            var    addContactResult    = await autopilotAPIClient.AddOrUpdateContact(new AutopilotContactToAdd()
            {
                Contact = autopilotContact
            });

            using (var unitOfWork = UnitOfWork.Create())
            {
                if (!addContactResult.HasError)
                {
                    var user = unitOfWork.UserRepository.GetAll().FirstOrDefault(u => u.Id == userId);
                    if (user != null)
                    {
                        autopilotcontact_id     = addContactResult.Result.ContactId;
                        user.AutopilotContactId = autopilotcontact_id;

                        var getContactResult = await autopilotAPIClient.GetContact(autopilotcontact_id);

                        if (!getContactResult.HasError)
                        {
                            user.AutopilotData = JsonConvert.SerializeObject(getContactResult);
                        }

                        unitOfWork.UserRepository.Edit(user);
                        await unitOfWork.SaveAsync();

                        log.Info("Method: " + MethodBase.GetCurrentMethod().DeclaringType + " .Contact " + autopilotcontact_id + " has been created.");

                        return(autopilotcontact_id);
                    }
                }
                else
                {
                    var autopilotError = new AutopilotErrorBuffer();
                    autopilotError.AutopilotContactId = "";
                    autopilotError.OperationType      = "Registration";
                    autopilotError.RequestData        = JsonConvert.SerializeObject(model);
                    autopilotError.UserId             = userId;

                    unitOfWork.AutopilotErrorRepository.Insert(autopilotError);
                    await unitOfWork.SaveAsync();

                    log.Error("Method: " + MethodBase.GetCurrentMethod().DeclaringType + " .Contact's " + autopilotcontact_id + " creation has been failed.");
                    autopilotcontact_id = "";
                }
            }

            return(string.Empty);
        }