/// <summary> /// Migrates the scheduled transaction notes to history. /// </summary> public void MigrateScheduledTransactionNotesToHistory() { var rockContext = new RockContext(); rockContext.Database.CommandTimeout = _commandTimeout; var noteService = new NoteService(rockContext); var historyCategoryId = CategoryCache.Get(Rock.SystemGuid.Category.HISTORY_FINANCIAL_TRANSACTION.AsGuid())?.Id; var entityTypeIdScheduledTransaction = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.FINANCIAL_SCHEDULED_TRANSACTION.AsGuid()); var noteTypeIdScheduledTransaction = NoteTypeCache.GetId(Rock.SystemGuid.NoteType.SCHEDULED_TRANSACTION_NOTE.AsGuid()); if (!historyCategoryId.HasValue || !entityTypeIdScheduledTransaction.HasValue || !noteTypeIdScheduledTransaction.HasValue) { return; } var historyService = new HistoryService(rockContext); var historyQuery = historyService.Queryable().Where(a => a.EntityTypeId == entityTypeIdScheduledTransaction.Value); var captionsToConvert = new string[] { "Created Transaction" , "Updated Transaction" , "Cancelled Transaction" , "Reactivated Transaction" }; var notesToConvertToHistory = noteService.Queryable() .Where(a => a.NoteTypeId == noteTypeIdScheduledTransaction.Value && captionsToConvert.Contains(a.Caption) && a.EntityId.HasValue) .Where(a => !historyQuery.Any(h => h.EntityId == a.EntityId)); var notesToConvertToSummaryList = noteService.Queryable() .Where(a => a.NoteTypeId == noteTypeIdScheduledTransaction.Value && a.Caption == "Created Transaction" && !string.IsNullOrEmpty(a.Text) && a.EntityId.HasValue) .AsNoTracking().ToList(); List <History> historyRecordsToInsert = notesToConvertToHistory.AsNoTracking() .ToList() .Select(n => { var historyRecord = new History { CategoryId = historyCategoryId.Value, EntityTypeId = entityTypeIdScheduledTransaction.Value, EntityId = n.EntityId.Value, Guid = Guid.NewGuid(), CreatedByPersonAliasId = n.CreatedByPersonAliasId, ModifiedByPersonAliasId = n.ModifiedByPersonAliasId, CreatedDateTime = n.CreatedDateTime, ModifiedDateTime = n.ModifiedDateTime }; if (n.Caption == "Cancelled Transaction") { historyRecord.Verb = "MODIFY"; historyRecord.ChangeType = "Property"; historyRecord.ValueName = "Is Active"; historyRecord.NewValue = "False"; } else if (n.Caption == "Reactivated Transaction") { historyRecord.Verb = "MODIFY"; historyRecord.ChangeType = "Property"; historyRecord.ValueName = "Is Active"; historyRecord.NewValue = "True"; } else if (n.Caption == "Updated Transaction") { historyRecord.Verb = "MODIFY"; historyRecord.ValueName = "Transaction"; } else { historyRecord.Verb = "ADD"; historyRecord.ChangeType = "Record"; historyRecord.ValueName = "Transaction"; } return(historyRecord); }).ToList(); rockContext.BulkInsert(historyRecordsToInsert); var qryNotesToDelete = noteService.Queryable().Where(a => a.NoteTypeId == noteTypeIdScheduledTransaction && captionsToConvert.Contains(a.Caption)); rockContext.BulkDelete(qryNotesToDelete); foreach (var noteToConvertToSummary in notesToConvertToSummaryList) { using (var updatedSummaryContext = new RockContext()) { var scheduledTransactionService = new FinancialScheduledTransactionService(updatedSummaryContext); var scheduledTransaction = scheduledTransactionService.Get(noteToConvertToSummary.EntityId.Value); if (scheduledTransaction != null && scheduledTransaction.Summary.IsNullOrWhiteSpace()) { scheduledTransaction.Summary = noteToConvertToSummary.Text; updatedSummaryContext.SaveChanges(disablePrePostProcessing: true); } } } }