using Newtonsoft.Json; using ReserveManagementSystemApi.Enums; using ReserveManagementSystemLib.Models; using ReserveManagementSystemLib.Repositories; namespace ReserveManagementSystemApi.Services { public class AuditHistoryService : IAuditHistoryService { private readonly IAuditHistoryRepository _auditHistoryRepository; const string SYSTEM_MODIFIED_BY = "RMS"; public AuditHistoryService(IAuditHistoryRepository auditHistoryRepository) { _auditHistoryRepository = auditHistoryRepository; } public async Task> GetAllAsync() { try { return await _auditHistoryRepository.GetAll().OrderByDescending(o => o.ModifiedDate).ToListAsync(); } catch (Exception ex) { Log.Error(ex, "Failure retrieving audit histories."); throw; } } public async Task FindByIdAsync(int id) { AuditHistory history; try { history = await _auditHistoryRepository.FindByIdAsync(id); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving history with id {id}."); throw; } if (history == null) { throw new Exception($"Audit history with id {id} not found."); } return history; } public async Task AddHistoryAsync(AuditHistory history) { return await _auditHistoryRepository.AddHistoryAsync(history); } public async Task AddAuditHistoriesForNonexistingLoansAsync(List loans) { var currentDate = DateTime.Now; var histories = loans.Select(loan => new AuditHistory { LoanId = null, MemberNumber = loan.MemberNumber, LoanNumber = loan.LoanNumber, ModifiedBy = SYSTEM_MODIFIED_BY, ModifiedDate = currentDate, ActivityType = ActivityType.Error.ToString(), AuditDescription = "Loan in closed file not found", OldValue = null, NewValue = null }).ToList(); return await _auditHistoryRepository.AddHistoriesAsync(histories); } public AuditHistory GetAuditHistoryForNonexistingClosedLoan(Loan loan) { var currentDate = DateTime.Now; return new AuditHistory { LoanId = null, MemberNumber = loan.MemberNumber, LoanNumber = loan.LoanNumber, ModifiedBy = SYSTEM_MODIFIED_BY, ModifiedDate = currentDate, ActivityType = ActivityType.Error.ToString(), AuditDescription = "Loan in closed import not found", OldValue = null, NewValue = null }; } public AuditHistory GetExistingLoanUpdateHistoryAsync(Loan loan, Loan originalLoan) { var currentDate = DateTime.Now; return new AuditHistory { LoanId = loan.LoanId, MemberNumber = loan.MemberNumber, LoanNumber = loan.LoanNumber, ModifiedBy = SYSTEM_MODIFIED_BY, ModifiedDate = currentDate, ActivityType = ActivityType.NewLoanUpdate.ToString(), AuditDescription = "Loan found in table, record updated", OldValue = JsonConvert.SerializeObject(originalLoan), NewValue = JsonConvert.SerializeObject(loan) }; } public AuditHistory GetLoanStatusChangeHistory(Loan loan, string oldStatus, string description) { return new AuditHistory { LoanId = loan.LoanId, MemberNumber = loan.MemberNumber, LoanNumber = loan.LoanNumber, ModifiedBy = SYSTEM_MODIFIED_BY, ModifiedDate = DateTime.Now, ActivityType = ActivityType.LoanStatusChange.ToString(), OldValue = oldStatus, NewValue = loan.LoanStatus, AuditDescription = description }; } public async Task AddHistoriesAsync(List histories) { return await _auditHistoryRepository.AddHistoriesAsync(histories); } public async Task AddErrorHistoryAsync(string memberNumber, string loanNumber, string description) { var auditHist = new AuditHistory { MemberNumber = memberNumber, LoanNumber = loanNumber, ActivityType = ActivityType.Error.ToString(), ModifiedDate = DateTime.Now, AuditDescription = description, ModifiedBy = SYSTEM_MODIFIED_BY }; await _auditHistoryRepository.AddHistoryAsync(auditHist); } } }