Files
2026-04-20 14:47:24 -05:00

92 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LCU.API.Collections.CollectionsApiLib.Models;
using LCU.API.Collections.RepoApi.DataAccess;
using LCU.API.Collections.RepoApi.Exceptions;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Repositories;
using Serilog;
namespace LCU.API.Collections.RepoApi.Services
{
public class RepoLetterService : IRepoLetterService
{
private readonly ILetterRepository _letterRepository;
private readonly ICollectionsDao _collectionsDao;
public RepoLetterService(ILetterRepository letterRepository,
ICollectionsDao collectionsDao)
{
_letterRepository = letterRepository;
_collectionsDao = collectionsDao;
}
public async Task<IReadOnlyCollection<RepoLetter>> ListByAccountIdAsync(int accountId)
{
try
{
var collectionsHistory = await _collectionsDao.RetrieveHistoryAsync(accountId);
var collectionsLetters =
collectionsHistory.Where(history => history.Type == HistoryType.Letter).ToList();
if (!collectionsLetters.Any())
{
return new List<RepoLetter>();
}
var letters = await _letterRepository.ListAsync();
return ProcessLetters(collectionsLetters, letters.ToList());
}
catch (MissingRequirementException ex)
{
throw;
}
catch (Exception ex)
{
Log.Error(ex, "Failure retrieving Letter List");
throw new ServerErrorException();
}
}
public static IReadOnlyCollection<RepoLetter> ProcessLetters(List<TemenosHistory> collectionsLetters,
List<Letter> letters)
{
// GJF: Are you here to fix a bug? Probably. These don't match up, who wrote this?!?! This code is fueled by hope.
// Ok, so you really need to fix it. Go out to Temenos, go to System Management (the button is labelled 'tools'!)
// Find the Communication section, expand it and choose 'letters & forms'. Now find the right Letter and
// update the LetterDescription column in the Letters table of RepoDb for the correct letter.
var resultList = new List<RepoLetter>();
foreach (var letter in letters)
{
foreach (var collectionsLetter in collectionsLetters)
{
if (string.IsNullOrWhiteSpace(letter.LetterDescription))
{
// If this exception is happening, see LetterUpdate.sql. Also See Above.
throw new MissingRequirementException("LetterDescription", "Letter table requires LetterDescription Column entry.");
}
if (collectionsLetter.Message.Trim().ToUpper()
.Contains(letter.LetterDescription.Trim().ToUpper()))
{
resultList.Add(new RepoLetter
{
NoticeType = collectionsLetter.Message,
DateSent = collectionsLetter.Created,
DateExpired = collectionsLetter.Created.AddDays(letter.DaysToWait ?? 0),
DaysToRedeem = letter.DaysToWait ?? 0,
ActionCode = letter.ActionCode.Trim()
});
}
}
}
return resultList
.GroupBy(x => x.NoticeType.Trim(),
(key, g) => g.OrderByDescending(x => x.DateSent).FirstOrDefault())
.OrderByDescending(x => x.DateSent).ToList();
}
}
}