Adding
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
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 RefundStatusService : IRefundStatusService
|
||||
{
|
||||
private IRefundStatusRepository _refundStatusRepository;
|
||||
private IUnitOfWork _unitOfWork;
|
||||
private IRepoAccountService _repoAccountService;
|
||||
private IEmailer _emailer;
|
||||
|
||||
public RefundStatusService(
|
||||
IRefundStatusRepository refundStatusRepository,
|
||||
IUnitOfWork unitOfWork,
|
||||
IRepoAccountService repoAccountService,
|
||||
IEmailer emailer)
|
||||
{
|
||||
_refundStatusRepository = refundStatusRepository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_repoAccountService = repoAccountService;
|
||||
_emailer = emailer;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<RefundStatus>> ListAllAsync()
|
||||
{
|
||||
return await _refundStatusRepository.ListAsync();
|
||||
}
|
||||
|
||||
public async Task<RefundStatus> GetAsync(int repoId)
|
||||
{
|
||||
RefundStatus refundStatus;
|
||||
try
|
||||
{
|
||||
refundStatus = await _refundStatusRepository.FindByRepoIdAsync(repoId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failure retrieving Refund Status for repoId: {repoId}");
|
||||
throw new NotFoundException($"Refund Status with repoId {repoId} not found");
|
||||
}
|
||||
|
||||
if (refundStatus == null)
|
||||
{
|
||||
throw new NotFoundException($"Refund Status with repoId {repoId} not found");
|
||||
}
|
||||
|
||||
return refundStatus;
|
||||
}
|
||||
|
||||
public async Task<RefundStatus> SaveAsync(RefundStatus refundStatus)
|
||||
{
|
||||
await _repoAccountService.Get(refundStatus.RepoId.Value);
|
||||
|
||||
try
|
||||
{
|
||||
var newRefundStatus = await _refundStatusRepository.AddAsync(refundStatus);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
return newRefundStatus;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failure saving Refund Status: {refundStatus}", refundStatus);
|
||||
throw new ServerErrorException();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<RefundStatus> UpdateAsync(RefundStatus refundStatus)
|
||||
{
|
||||
var existingRefundStatus = await GetAsync(refundStatus.RepoId.Value);
|
||||
var existingRepoAccount = await _repoAccountService.Get(refundStatus.RepoId.Value);
|
||||
var isInBankruptcy = existingRepoAccount.Bankruptcy != null;
|
||||
var isEmailNeeded = refundStatus.RefundsReceivedDate != null && existingRefundStatus.RefundsReceivedDate != refundStatus.RefundsReceivedDate && !isInBankruptcy;
|
||||
|
||||
try
|
||||
{
|
||||
existingRefundStatus.WarrantyRefundStatus = refundStatus.WarrantyRefundStatus;
|
||||
existingRefundStatus.InsuranceRefundStatus = refundStatus.InsuranceRefundStatus;
|
||||
existingRefundStatus.PremiumDefiRefundStatus = refundStatus.PremiumDefiRefundStatus;
|
||||
existingRefundStatus.CPIRefundStatus = refundStatus.CPIRefundStatus;
|
||||
existingRefundStatus.RefundsReceivedDate = refundStatus.RefundsReceivedDate;
|
||||
|
||||
_refundStatusRepository.Update(existingRefundStatus);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
|
||||
if (isEmailNeeded)
|
||||
{
|
||||
await _emailer.SendSurplusDeficiencyLetterEmailAsync(existingRepoAccount);
|
||||
}
|
||||
|
||||
return existingRefundStatus;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failure saving Refund Status: {refundStatus}", refundStatus);
|
||||
throw new ServerErrorException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user