Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarwal4 committed Mar 11, 2024
2 parents 9aef2c0 + 92abe6a commit 6f4de79
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions backend/core/src/Core.Infrastructure/Jobs/ProcessEmailsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,50 @@ public ProcessEmailsJob(

public async Task Execute(IJobExecutionContext context)
{
try
{
var mails = _mailsRepository.GetMailsAsync(MailStatus.ReadyToSend.ToString(), context.CancellationToken).Result;
var mails = _mailsRepository.GetMailsAsync(MailStatus.ReadyToSend.ToString(), context.CancellationToken).Result;

if (mails != null && mails.Any())
if (mails != null && mails.Any())
{
foreach (var mail in mails)
{
foreach (var mail in mails)
{
var customerCode = mail.References?.CustomerCode;
var customerCode = mail.References?.CustomerCode;

if (string.IsNullOrWhiteSpace(customerCode))
{
throw new CustomErrorsException("MailService", "customerCode", "An error occured while sending mail.");
}
if (string.IsNullOrWhiteSpace(customerCode))
{
throw new CustomErrorsException("MailService", "customerCode", "An error occured while sending mail.");
}

var customer = await _customerRepository.GetAsync(customerCode, context.CancellationToken);
var customer = await _customerRepository.GetAsync(customerCode, context.CancellationToken);

var query = new Dictionary<string, string>
var query = new Dictionary<string, string>
{
{ "customerCode", customerCode }
};

var transactions = await _transactionRepository.GetAsync(query, 1, 1, context.CancellationToken);
var transactions = await _transactionRepository.GetAsync(query, 1, 1, context.CancellationToken);

Transaction? transaction = null;
Transaction? transaction = null;

if (transactions != null && transactions.Items.Any() && mail.References != null)
{
transaction = transactions.Items.FirstOrDefault(t => t.TransactionCode == mail.References!.TokenPaymentCode);
if (transactions != null && transactions.Items.Any() && mail.References != null)
{
transaction = transactions.Items.FirstOrDefault(t => t.TransactionCode == mail.References!.TokenPaymentCode);

try
{
await _sendGridMailService.SendMailAsync(mail, customer, transaction!);
}
catch (Exception ex)
{
_logger.LogError("An error occured sending email {code} with message {message}", mail.Code, ex.Message);
}
try
{
await _sendGridMailService.SendMailAsync(mail, customer, transaction!);
}
catch (Exception ex)
{
_logger.LogError("An error occured sending email {code} with message {message}", mail.Code, ex.Message);
}

// once email has been sent, call nexus to update the status of this mail to 'Sent'
await _mailsRepository.UpdateMailSent(mail.Code);
// once email has been sent, call nexus to update the status of this mail to 'Sent'
await _mailsRepository.UpdateMailSent(mail.Code);

await _unitOfWork.SaveChangesAsync();
}
await _unitOfWork.SaveChangesAsync();
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while processing emails: {exception}", ex);
}
}
}
}
Expand Down

0 comments on commit 6f4de79

Please sign in to comment.