/// <summary> /// Called to save a customer to the device /// </summary> /// <param name="e">The event arguments</param> /// <param name="smsSendAttempts">Number of attempts</param> /// <param name="smsAttemptLimit">The Limit</param> /// <returns>An empty task</returns> private async Task SaveCustomerToDevice(CustomerService.RegistrationStatusChangedEventArgs e, int smsSendAttempts, int smsAttemptLimit) { bool succeeded = e.SmsRegistrationSucceeded || e.Response.RegistrationSuccessful; if (!succeeded) { this.Logger.Debug(string.Format("Sms send attempts = {0} and sms send limts = {1}", smsSendAttempts, smsAttemptLimit)); succeeded = e.Channel == DataChannel.Fallback && smsAttemptLimit == smsSendAttempts; if (!succeeded) { return; } } this.Logger.Debug("Attempting to save customer to device"); if (e.Response == null || e.Response.Customer == null) { return; } ProspectsController prospectsController = new ProspectsController(); Mkopa.Core.BL.Models.People.Prospect prospect = await prospectsController .GetByPhoneNumberAsync(e.Response.Customer.Phone); await Resolver.Instance.Get <ISQLiteDB>().Connection.RunInTransactionAsync( async(SQLiteConnection connTran) => { try { this.Logger.Debug("Trying to save customer"); CustomersController customersController = new CustomersController(); e.Response.Customer.Channel = e.Channel; string customerPhone = e.Response.Customer.Phone; Guid custGuid; Mkopa.Core.BL.Models.People.Customer customer = await customersController.GetSingleByCriteria(CriteriaBuilder.New() .Add("Phone", customerPhone)); // customer to have more than one product if (customer == null || customer.Id == default(Guid)) { SaveResponse <Mkopa.Core.BL.Models.People.Customer> saveResponse = await customersController.SaveAsync(connTran, e.Response.Customer, true); if (saveResponse.SavedModel == null || saveResponse.SavedModel.Id == default(Guid)) { new ReusableScreens(this.Activity) .ShowInfo( Resource.String.customer_save_err_actionbar, Resource.String.customer_save_err_title, Resource.String.customer_save_err_message, Resource.String.customer_save_err_button); return; } custGuid = saveResponse.SavedModel.Id; } else { custGuid = customer.Id; } await this.SaveCustomerProductToDevice(custGuid, connTran); if (prospect != null) { this.Logger.Debug("There was a prospect with the same number so we convert to customer"); prospect.Converted = true; await prospectsController.SaveAsync(connTran, prospect); } RecordStatus syncStatus = e.Channel == DataChannel.Fallback ? (e.SmsRegistrationSucceeded ? RecordStatus.FallbackSent : RecordStatus.Pending) : RecordStatus.Synced; SyncingController syncController = new SyncingController(); SyncRecord syncRec = await syncController.GetSyncRecordAsync(e.Response.Customer.RequestId); this.Logger.Debug("Fetching sync record for customer"); if (syncRec == null) { this.Logger.Debug("The sync record is null so we generate one"); syncRec = new SyncRecord { ModelId = e.Response.Customer.Id, ModelType = e.Response.Customer.TableName, Status = syncStatus, RequestId = e.Response.Customer.RequestId }; this.Logger.Debug("Saving generated sync record"); SaveResponse <SyncRecord> syncSaveResponse = await syncController.SaveAsync( connTran, syncRec); this.Logger.Debug("Sync record was saved correctly"); } else { syncRec.Status = syncStatus; } await syncController.SaveAsync(connTran, syncRec); } catch (Exception exception) { this.Logger.Error(exception); } }); }