/// <summary>
        /// Gets the settings.
        /// </summary>
        private void GetSettings()
        {
            // Get Spark Data settings
            _sparkDataConfig = Ncoa.GetSettings();
            if (_sparkDataConfig.SparkDataApiKey.IsNullOrWhiteSpace())
            {
                pnlSparkDataEdit.Visible    = false;
                pnlSignIn.Visible           = true;
                pnlAccountStatus.Visible    = false;
                pwNcoaConfiguration.Visible = false;
                bbtnNcoaSaveConfig.Visible  = false;

                txtSparkDataApiKeyLogin.Text      = _sparkDataConfig.SparkDataApiKey;
                grpNotificationGroupLogin.GroupId = _sparkDataConfig.GlobalNotificationApplicationGroupId;
            }
            else
            {
                pnlSparkDataEdit.Visible    = false;
                pnlSignIn.Visible           = false;
                pnlAccountStatus.Visible    = true;
                pwNcoaConfiguration.Visible = true;
                bbtnNcoaSaveConfig.Visible  = true;

                // Get NCOA configuration settings
                nbNcoaMinMoveDistance.Text             = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_MINIMUM_MOVE_DISTANCE_TO_INACTIVATE);
                cbNcoa48MonAsPrevious.Checked          = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_SET_48_MONTH_AS_PREVIOUS).AsBoolean();
                cbNcoaInvalidAddressAsPrevious.Checked = Rock.Web.SystemSettings.GetValue(SystemSetting.NCOA_SET_INVALID_AS_PREVIOUS).AsBoolean();

                txtSparkDataApiKeyEdit.Text      = _sparkDataConfig.SparkDataApiKey;
                grpNotificationGroupEdit.GroupId = _sparkDataConfig.GlobalNotificationApplicationGroupId;

                // Get NCOA settings
                if (_sparkDataConfig.NcoaSettings == null)
                {
                    _sparkDataConfig.NcoaSettings = new NcoaSettings();
                }

                dvpNcoaPersonDataView.SetValue(_sparkDataConfig.NcoaSettings.PersonDataViewId);
                cbNcoaRecurringEnabled.Checked   = _sparkDataConfig.NcoaSettings.RecurringEnabled;
                nbNcoaRecurrenceInterval.Enabled = _sparkDataConfig.NcoaSettings.RecurringEnabled;
                nbNcoaRecurrenceInterval.Text    = _sparkDataConfig.NcoaSettings.RecurrenceInterval.ToStringSafe();
                cbNcoaAcceptTerms.Checked        = _sparkDataConfig.NcoaSettings.IsAcceptedTerms;
                cbNcoaAckPrice.Checked           = _sparkDataConfig.NcoaSettings.IsAckPrice;
                if (_sparkDataConfig.NcoaSettings.InactiveRecordReasonId.HasValue)
                {
                    dvpNcoaInactiveRecordReason.SetValue(_sparkDataConfig.NcoaSettings.InactiveRecordReasonId.Value);
                }

                nbNcoaCreditCard.Visible = false;

                if (_sparkDataConfig.NcoaSettings.CurrentReportStatus == null)
                {
                    _sparkDataConfig.NcoaSettings.CurrentReportStatus = string.Empty;
                }

                if (_sparkDataConfig.SparkDataApiKey.IsNullOrWhiteSpace())
                {
                    pnlSignIn.Visible        = true;
                    pnlSparkDataEdit.Visible = false;
                }
                else
                {
                    pnlSignIn.Visible = false;
                    bool accountValid = false;

                    SparkDataApi sparkDataApi = new SparkDataApi();
                    try
                    {
                        var accountStatus = sparkDataApi.CheckAccount(_sparkDataConfig.SparkDataApiKey);
                        switch (accountStatus)
                        {
                        case SparkDataApi.AccountStatus.AccountNoName:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Account does not have a name";
                            break;

                        case SparkDataApi.AccountStatus.AccountNotFound:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Account not found";
                            break;

                        case SparkDataApi.AccountStatus.Disabled:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Disabled";
                            break;

                        case SparkDataApi.AccountStatus.EnabledCardExpired:
                            hlAccountStatus.LabelType = LabelType.Danger;
                            hlAccountStatus.Text      = "Enabled - Card expired";
                            break;

                        case SparkDataApi.AccountStatus.EnabledNoCard:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Enabled - No card on file";
                            nbNcoaCreditCard.Visible  = true;
                            break;

                        case SparkDataApi.AccountStatus.EnabledCard:
                            hlAccountStatus.LabelType = LabelType.Success;
                            hlAccountStatus.Text      = "Enabled - Card on file";
                            accountValid = true;
                            break;

                        case SparkDataApi.AccountStatus.InvalidSparkDataKey:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Invalid Spark Data Key";
                            break;

                        case SparkDataApi.AccountStatus.EnabledCardNoExpirationDate:
                            hlAccountStatus.LabelType = LabelType.Warning;
                            hlAccountStatus.Text      = "Enabled - Card expiration date not on file";
                            break;
                        }

                        string cost = sparkDataApi.GetPrice("CF20766E-80F9-E282-432F-6A9E19F0BFF1");
                        cbNcoaAckPrice.Text = cbNcoaAckPrice.Text.Replace("$xx", "$" + cost);
                    }
                    catch
                    {
                        hlAccountStatus.LabelType = LabelType.Danger;
                        hlAccountStatus.Text      = "Error connecting to Spark server";
                    }

                    cbNcoaConfiguration.Checked = _sparkDataConfig.NcoaSettings.IsEnabled && accountValid;
                    cbNcoaConfiguration.Enabled = accountValid;
                    SetStartNcoaEnabled();
                    SetPanels();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Job to get a National Change of Address (NCOA) report for all active people's addresses.
        ///
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with
        /// the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute(IJobExecutionContext context)
        {
            Exception exception = null;
            // Get the job setting(s)
            JobDataMap      dataMap         = context.JobDetail.JobDataMap;
            SparkDataConfig sparkDataConfig = Ncoa.GetSettings();

            if (!sparkDataConfig.NcoaSettings.IsEnabled || !sparkDataConfig.NcoaSettings.IsValid())
            {
                return;
            }

            try
            {
                Guid?sparkDataApiKeyGuid = sparkDataConfig.SparkDataApiKey.AsGuidOrNull();
                if (sparkDataApiKeyGuid == null)
                {
                    exception = new Exception($"Spark Data Api Key '{sparkDataConfig.SparkDataApiKey.ToStringSafe()}' is empty or invalid. The Spark Data Api Key can be configured in System Settings > Spark Data Settings.");
                    return;
                }

                switch (sparkDataConfig.NcoaSettings.CurrentReportStatus)
                {
                case "":
                case null:
                    if (sparkDataConfig.NcoaSettings.RecurringEnabled)
                    {
                        StatusStart(sparkDataConfig);
                    }

                    break;

                case "Start":
                    StatusStart(sparkDataConfig);
                    break;

                case "Failed":
                    StatusFailed(sparkDataConfig);
                    break;

                case "Pending: Report":
                    StatusPendingReport(sparkDataConfig);
                    break;

                case "Pending: Export":
                    StatusPendingExport(sparkDataConfig);
                    break;

                case "Complete":
                    StatusComplete(sparkDataConfig);
                    break;
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (exception != null)
                {
                    context.Result = $"NCOA Job failed: {exception.Message}";

                    sparkDataConfig.NcoaSettings.CurrentReportStatus = "Failed";
                    sparkDataConfig.Messages.Add($"NOCA job failed: {RockDateTime.Now.ToString()} - {exception.Message}");
                    Ncoa.SaveSettings(sparkDataConfig);

                    try
                    {
                        var ncoa = new Ncoa();
                        ncoa.SentNotification(sparkDataConfig, "failed");
                    }
                    catch
                    {
                    }


                    if (sparkDataConfig.SparkDataApiKey.IsNotNullOrWhiteSpace() && sparkDataConfig.NcoaSettings.FileName.IsNotNullOrWhiteSpace())
                    {
                        SparkDataApi sparkDataApi = new SparkDataApi();
                    }

                    Exception   ex       = new AggregateException("NCOA job failed.", exception);
                    HttpContext context2 = HttpContext.Current;
                    ExceptionLogService.LogException(ex, context2);
                    throw ex;
                }
                else
                {
                    string msg;
                    if (sparkDataConfig.NcoaSettings.CurrentReportStatus == "Complete")
                    {
                        using (RockContext rockContext = new RockContext())
                        {
                            NcoaHistoryService ncoaHistoryService = new NcoaHistoryService(rockContext);
                            msg = $"NCOA request processed, {ncoaHistoryService.Count()} {(ncoaHistoryService.Count() == 1 ? "address" : "addresses")} processed, {ncoaHistoryService.MovedCount()} {(ncoaHistoryService.MovedCount() > 1 ? "were" : "was")} marked as 'moved'";
                        }
                    }
                    else
                    {
                        msg = $"Job complete. NCOA status: {sparkDataConfig.NcoaSettings.CurrentReportStatus}";
                    }

                    context.Result = msg;
                    sparkDataConfig.Messages.Add($"{msg}: {RockDateTime.Now.ToString()}");
                    Ncoa.SaveSettings(sparkDataConfig);
                }
            }
        }