Esempio n. 1
0
        /// <summary>
        /// Resume a pending export: Checks if the export is complete. If the export is complete, then download the export, process the addresses and sent a notification.
        /// </summary>
        /// <param name="sparkDataConfig">The spark data configuration.</param>
        public void PendingExport(SparkDataConfig sparkDataConfig = null)
        {
            if (sparkDataConfig == null)
            {
                sparkDataConfig = GetSettings();
            }

            SparkDataApi.SparkDataApi sparkDataApi = new SparkDataApi.SparkDataApi();
            var credentials = sparkDataApi.NcoaGetCredentials(sparkDataConfig.SparkDataApiKey);

            var ncoaApi = new NcoaApi(credentials);

            if (!ncoaApi.IsReportExportCreated(sparkDataConfig.NcoaSettings.FileName))
            {
                return;
            }

            List <NcoaReturnRecord> ncoaReturnRecords;

            ncoaApi.DownloadExport(sparkDataConfig.NcoaSettings.CurrentReportExportKey, out ncoaReturnRecords);
            var ncoaHistoryList = ncoaReturnRecords.Select(r => r.ToNcoaHistory()).ToList();

            FilterDuplicateLocations(ncoaHistoryList);

            // Making sure that the database is empty to avoid adding duplicate data.
            using (var rockContext = new RockContext())
            {
                NcoaHistoryService ncoaHistoryService = new NcoaHistoryService(rockContext);
                ncoaHistoryService.DeleteRange(ncoaHistoryService.Queryable());
                rockContext.SaveChanges();
            }

            if (ncoaReturnRecords != null && ncoaReturnRecords.Count != 0)
            {
                using (var rockContext = new RockContext())
                {
                    var ncoaHistoryService = new NcoaHistoryService(rockContext);
                    ncoaHistoryService.AddRange(ncoaHistoryList);
                    rockContext.SaveChanges();
                }
            }

            ProcessNcoaResults(sparkDataConfig);

            sparkDataApi.NcoaCompleteReport(sparkDataConfig.SparkDataApiKey, sparkDataConfig.NcoaSettings.FileName, sparkDataConfig.NcoaSettings.CurrentReportExportKey);

            //Notify group
            SentNotification(sparkDataConfig, "finished");

            sparkDataConfig.NcoaSettings.LastRunDate         = RockDateTime.Now;
            sparkDataConfig.NcoaSettings.CurrentReportStatus = "Complete";
            sparkDataConfig.NcoaSettings.FileName            = null;
            SaveSettings(sparkDataConfig);
        }
Esempio n. 2
0
        /// <summary>
        /// Resume a pending report: Checks if the report is complete. If the report is complete, then sent a create report command to NCOA.
        /// </summary>
        /// <param name="sparkDataConfig">The spark data configuration.</param>
        public void PendingReport(SparkDataConfig sparkDataConfig = null)
        {
            if (sparkDataConfig == null)
            {
                sparkDataConfig = GetSettings();
            }

            SparkDataApi.SparkDataApi sparkDataApi = new SparkDataApi.SparkDataApi();
            var credentials = sparkDataApi.NcoaGetCredentials(sparkDataConfig.SparkDataApiKey);
            var ncoaApi     = new NcoaApi(credentials);

            if (!ncoaApi.IsReportCreated(sparkDataConfig.NcoaSettings.CurrentReportKey))
            {
                return;
            }

            string exportFileId;

            ncoaApi.CreateReportExport(sparkDataConfig.NcoaSettings.CurrentReportKey, out exportFileId);

            sparkDataConfig.NcoaSettings.CurrentReportExportKey = exportFileId;
            sparkDataConfig.NcoaSettings.CurrentReportStatus    = "Pending: Export";
            SaveSettings(sparkDataConfig);
        }
Esempio n. 3
0
        /// <summary>
        /// Starts the NCOA request: Check if there is a valid credit card on the Spark Data server. If there is a valid credit card, then get the addresses, initialize a report on NCOA, upload the addresses to NCOA and delete the previous NcoaHistory data.
        /// </summary>
        /// <param name="sparkDataConfig">The spark data configuration.</param>
        public void Start(SparkDataConfig sparkDataConfig)
        {
            if (sparkDataConfig == null)
            {
                sparkDataConfig = GetSettings();
            }

            SparkDataApi.SparkDataApi sparkDataApi = new SparkDataApi.SparkDataApi();
            var accountStatus = sparkDataApi.CheckAccount(sparkDataConfig.SparkDataApiKey);

            switch (accountStatus)
            {
            case SparkDataApi.SparkDataApi.AccountStatus.AccountNoName:
                throw new UnauthorizedAccessException("Account does not have a name.");

            case SparkDataApi.SparkDataApi.AccountStatus.AccountNotFound:
                throw new UnauthorizedAccessException("Account not found.");

            case SparkDataApi.SparkDataApi.AccountStatus.Disabled:
                throw new UnauthorizedAccessException("Account is disabled.");

            case SparkDataApi.SparkDataApi.AccountStatus.EnabledCardExpired:
                throw new UnauthorizedAccessException("Credit card on Spark server expired.");

            case SparkDataApi.SparkDataApi.AccountStatus.EnabledNoCard:
                throw new UnauthorizedAccessException("No credit card found on Spark server.");

            case SparkDataApi.SparkDataApi.AccountStatus.InvalidSparkDataKey:
                throw new UnauthorizedAccessException("Invalid Spark Data Key.");
            }

            var addresses = GetAddresses(sparkDataConfig.NcoaSettings.PersonDataViewId);

            if (addresses.Count == 0)
            {
                sparkDataConfig.NcoaSettings.LastRunDate         = RockDateTime.Now;
                sparkDataConfig.NcoaSettings.CurrentReportStatus = "Complete";
                SaveSettings(sparkDataConfig);
                return;
            }

            GroupNameTransactionKey groupNameTransactionKey = null;

            if (sparkDataConfig.NcoaSettings.FileName.IsNotNullOrWhiteSpace())
            {
                groupNameTransactionKey = sparkDataApi.NcoaRetryReport(sparkDataConfig.SparkDataApiKey, sparkDataConfig.NcoaSettings.FileName);
            }

            if (groupNameTransactionKey == null)
            {
                groupNameTransactionKey = sparkDataApi.NcoaInitiateReport(sparkDataConfig.SparkDataApiKey, addresses.Count, sparkDataConfig.NcoaSettings.PersonFullName);
            }

            if (groupNameTransactionKey == null)
            {
                if (sparkDataConfig.NcoaSettings.CurrentReportStatus == "Failed")
                {
                    // To avoid trying to charging customer over and over again...
                    sparkDataConfig.NcoaSettings.IsEnabled = false;
                }

                throw new Exception("Init NCOA: Failed to initialize request.");
            }

            sparkDataConfig.NcoaSettings.FileName = groupNameTransactionKey.TransactionKey;

            var credentials = sparkDataApi.NcoaGetCredentials(sparkDataConfig.SparkDataApiKey);
            var ncoaApi     = new NcoaApi(credentials);

            string id;

            ncoaApi.CreateFile(sparkDataConfig.NcoaSettings.FileName, groupNameTransactionKey.GroupName, out id);
            sparkDataConfig.NcoaSettings.CurrentReportKey = id;

            ncoaApi.UploadAddresses(addresses, sparkDataConfig.NcoaSettings.CurrentReportKey);

            sparkDataConfig.NcoaSettings.CurrentUploadCount = addresses.Count;
            ncoaApi.CreateReport(sparkDataConfig.NcoaSettings.CurrentReportKey);

            // Delete previous NcoaHistory entries. This prevent an user thinking the previous run's data is the current run's data.
            using (RockContext rockContext = new RockContext())
            {
                NcoaHistoryService ncoaHistoryService = new NcoaHistoryService(rockContext);
                ncoaHistoryService.DeleteRange(ncoaHistoryService.Queryable());
                rockContext.SaveChanges();
            }

            sparkDataConfig.NcoaSettings.CurrentReportStatus = "Pending: Report";
            SaveSettings(sparkDataConfig);
        }