using System.Collections.Generic; using System.Threading.Tasks; using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using LCU.API.Internal.Operations.IncentiveApiLib.Persistence; using Microsoft.EntityFrameworkCore; using Serilog; namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories; public class ProcessorHistoryRepository : BaseRepository, IProcessorHistoryRepository { public ProcessorHistoryRepository(IApplicationDbContext context) : base(context) { } public async Task SaveProcessorRunHistory(ProcessorHistoryRecord newHistoryRecord) { var existingProcess = await _context.ProcessorRunHistory.FindAsync(newHistoryRecord.Process); if (existingProcess == null) { await _context.ProcessorRunHistory.AddAsync(newHistoryRecord); return newHistoryRecord.Process; } if (newHistoryRecord.LastSuccessfulRunTime < existingProcess.LastSuccessfulRunTime) { Log.Warning( $"ProcessorHistoryRepository Saved over a record with older data." + $"Old LastSuccess {existingProcess.LastSuccessfulRunTime} New LastSuccess: {newHistoryRecord.LastSuccessfulRunTime}"); } if (newHistoryRecord.LastProcessedRecordTimestamp < existingProcess.LastProcessedRecordTimestamp) { Log.Warning( $"ProcessorHistoryRepository Saved over a record with older data." + $"Old LastProcessed {existingProcess.LastProcessedRecordTimestamp} New LastProcessed: {newHistoryRecord.LastProcessedRecordTimestamp}"); } if (newHistoryRecord.LastProcessedRecordId < existingProcess.LastProcessedRecordId) { Log.Warning( $"ProcessorHistoryRepository Saved over a record with older data." + $"Old ID {existingProcess.LastProcessedRecordId} New ID: {newHistoryRecord.LastProcessedRecordId}"); } existingProcess.LastProcessedRecordId = newHistoryRecord.LastProcessedRecordId; existingProcess.LastProcessedRecordTimestamp = newHistoryRecord.LastProcessedRecordTimestamp; existingProcess.LastSuccessfulRunTime = newHistoryRecord.LastSuccessfulRunTime; return existingProcess.Process; } public async Task GetProcessorRunHistory(Process process) { return await _context.ProcessorRunHistory.FindAsync(process); } public async Task> GetAllProcessorRunHistory() { return await _context.ProcessorRunHistory.ToListAsync(); } }