Skip to content

Commit

Permalink
The service can now receive bounce events.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbitounis committed Jun 2, 2020
1 parent 4286420 commit c667739
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 13 deletions.
32 changes: 32 additions & 0 deletions Projects/SesNotifications.App/Factories/DbSesBounceEventFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SesNotifications.App.Models;
using System;
using System.Linq;
using SesBounceEvent = SesNotifications.DataAccess.Entities.SesBounceEvent;

namespace SesNotifications.App.Factories
{
public static class DbSesBounceEventFactory
{
public static SesBounceEvent Create(this SesBounceEventModel bounce, long notificationId)
{
return new SesBounceEvent
{
Id = notificationId,
NotificationId = notificationId,
NotificationType = "Bounce",
SentAt = Convert.ToDateTime(bounce.Mail.Timestamp),
MessageId = bounce.Mail.MessageId,
Source = bounce.Mail.Source,
SourceArn = bounce.Mail.SourceArn,
SourceIp = bounce.Mail.SourceIp,
SendingAccountId = bounce.Mail.SendingAccountId,
BounceType = bounce.Bounce.BounceType,
BounceSubType = bounce.Bounce.BounceSubType,
CreatedAt = Convert.ToDateTime(bounce.Bounce.Timestamp),
FeedbackId = bounce.Bounce.FeedbackId,
ReportingMta = bounce.Bounce.ReportingMta,
BouncedRecipients = string.Join(',', bounce.Bounce.BouncedRecipients.Select(x => x.EmailAddress).ToArray())
};
}
}
}
12 changes: 12 additions & 0 deletions Projects/SesNotifications.App/Models/SesBounceEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SesNotifications.App.Models
{
public class SesBounceEvent
{
public virtual string BounceType { get; set; }
public virtual string BounceSubType { get; set; }
public virtual SesBouncedRecipient[] BouncedRecipients { get; set; }
public virtual string Timestamp { get; set; }
public virtual string FeedbackId { get; set; }
public virtual string ReportingMta { get; set; }
}
}
8 changes: 8 additions & 0 deletions Projects/SesNotifications.App/Models/SesBounceEventModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SesNotifications.App.Models
{
public class SesBounceEventModel : Ses
{
public virtual SesMail Mail { get; set; }
public virtual SesBounceEvent Bounce { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface ISearchService
IList<SesOpenEvent> FindOpenEvents(string email, DateTime start, DateTime end);
IList<SesSendEvent> FindSendEvents(string email, DateTime start, DateTime end);
IList<SesDeliveryEvent> FindDeliveryEvents(string email, DateTime start, DateTime end);
IList<SesBounceEvent> FindBounceEventsEvents(string email, DateTime start, DateTime end);
IList<SesNotification> FindRaw(DateTime start, DateTime end);
SesNotification FindRaw(long id);
}
Expand Down
15 changes: 15 additions & 0 deletions Projects/SesNotifications.App/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class NotificationService : INotificationService
private readonly ISesOpensEventsRepository _sesOpensEventsRepository;
private readonly ISesSendEventsRepository _sesSendEventsRepository;
private readonly ISesDeliveryEventsRepository _sesDeliveryEventsRepository;
private readonly ISesBounceEventsRepository _sesBounceEventsRepository;
private readonly ILogger<NotificationService> _logger;

public NotificationService(INotificationsRepository notificationsRepository,
Expand All @@ -33,6 +34,7 @@ public NotificationService(INotificationsRepository notificationsRepository,
ISesOpensEventsRepository sesOpensEventsRepository,
ISesSendEventsRepository sesSendEventsRepository,
ISesDeliveryEventsRepository sesDeliveryEventsRepository,
ISesBounceEventsRepository sesBounceEventsRepository,
ILogger<NotificationService> logger)
{
_notificationsRepository = notificationsRepository;
Expand All @@ -43,6 +45,7 @@ public NotificationService(INotificationsRepository notificationsRepository,
_sesSendEventsRepository = sesSendEventsRepository;
_sesDeliveriesRepository = sesDeliveriesRepository;
_sesDeliveryEventsRepository = sesDeliveryEventsRepository;
_sesBounceEventsRepository = sesBounceEventsRepository;
_logger = logger;
}

Expand Down Expand Up @@ -101,12 +104,24 @@ private void HandleNotificationInternal(string content)
case Delivery:
HandleDeliveryEvent(content);
break;
case Bounce:
HandleBounceEvent(content);
break;
default:
throw new NotSupportedException($"Unsupported message {content.Substring(0, 50)}...");
}
}
}

private void HandleBounceEvent(string content)
{
var bounceEvent = JsonConvert.DeserializeObject<SesBounceEventModel>(content);

var notification = SaveNotification(bounceEvent.Mail, content);

_sesBounceEventsRepository.Save(bounceEvent.Create(notification.Id));
}

private void HandleDeliveryEvent(string content)
{
var delivery = JsonConvert.DeserializeObject<SesDeliveryEventModel>(content);
Expand Down
10 changes: 10 additions & 0 deletions Projects/SesNotifications.App/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SearchService : ISearchService
private readonly ISesOpensEventsRepository _sesOpensEventsRepository;
private readonly ISesSendEventsRepository _sesSendEventsRepository;
private readonly ISesDeliveryEventsRepository _sesDeliveryEventsRepository;
private readonly ISesBounceEventsRepository _sesBounceEventsRepository;
private readonly ILogger<SearchService> _logger;

public SearchService(INotificationsRepository notificationsRepository,
Expand All @@ -27,6 +28,7 @@ public SearchService(INotificationsRepository notificationsRepository,
ISesOpensEventsRepository sesOpensEventsRepository,
ISesSendEventsRepository sesSendEventsRepository,
ISesDeliveryEventsRepository sesDeliveryEventsRepository,
ISesBounceEventsRepository sesBounceEventsRepository,
ILogger<SearchService> logger)
{
_notificationsRepository = notificationsRepository;
Expand All @@ -36,6 +38,7 @@ public SearchService(INotificationsRepository notificationsRepository,
_sesOpensEventsRepository = sesOpensEventsRepository;
_sesSendEventsRepository = sesSendEventsRepository;
_sesDeliveryEventsRepository = sesDeliveryEventsRepository;
_sesBounceEventsRepository = sesBounceEventsRepository;
_logger = logger;
}

Expand Down Expand Up @@ -81,6 +84,13 @@ public IList<SesDeliveryEvent> FindDeliveryEvents(string email, DateTime start,
: _sesDeliveryEventsRepository.FindByRecipientAndSentDateRange($"%{email}%", start, end);
}

public IList<SesBounceEvent> FindBounceEventsEvents(string email, DateTime start, DateTime end)
{
return string.IsNullOrEmpty(email)
? _sesBounceEventsRepository.FindBySentDateRange(start, end)
: _sesBounceEventsRepository.FindByRecipientAndSentDateRange($"%{email}%", start, end);
}

public IList<SesNotification> FindRaw(DateTime start, DateTime end)
{
return _notificationsRepository.FindBySentDateRange(start, end);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using SesNotifications.App.Factories;
using SesNotifications.App.Tests.Helpers;
using Xunit;

namespace SesNotifications.App.Tests.Factories
{
public class DbSesBounceEventFactoryTests
{
[Fact]
public void Verify()
{
var dt = DateTime.UtcNow;
var bounce = TestHelpers.GetSesBounceEventModel(dt);

var sesBounce = bounce.Create(1);

Assert.Equal(sesBounce.BouncedRecipients, string.Join(',', bounce.Bounce.BouncedRecipients.Select(x => x.EmailAddress).ToArray()));
Assert.Equal(sesBounce.BounceSubType, bounce.Bounce.BounceSubType);
Assert.Equal(sesBounce.BounceType, bounce.Bounce.BounceType);
Assert.Equal(sesBounce.CreatedAt.Iso8601(), dt.Iso8601());
Assert.Equal(sesBounce.FeedbackId, bounce.Bounce.FeedbackId);
Assert.Equal(sesBounce.ReportingMta, bounce.Bounce.ReportingMta);
Assert.Equal(sesBounce.SourceArn, bounce.Mail.SourceArn);
Assert.Equal(sesBounce.NotificationType, bounce.NotificationType);
Assert.Equal(1, sesBounce.NotificationId);
Assert.Equal(sesBounce.MessageId, bounce.Mail.MessageId);
}
}
}
18 changes: 18 additions & 0 deletions Tests/SesNotifications.App.Tests/Helpers/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ public static SesBounceModel GetSesBounceModel(DateTime dt)
};
}

public static SesBounceEventModel GetSesBounceEventModel(DateTime dt)
{
return new SesBounceEventModel
{
NotificationType = "Bounce",
Mail = GetSesMail(dt),
Bounce = new SesBounceEvent
{
BouncedRecipients = new[] { new SesBouncedRecipient { EmailAddress = "address" } },
ReportingMta = null,
BounceSubType = "bounce_sub_type",
Timestamp = dt.Iso8601(),
FeedbackId = "feedback_id",
BounceType = "bounce_type"
}
};
}

public static SesOpenEventModel GetSesOpenModel(DateTime dt)
{
return new SesOpenEventModel
Expand Down
Loading

0 comments on commit c667739

Please sign in to comment.