Skip to content

Commit

Permalink
* Added send event handling.
Browse files Browse the repository at this point in the history
* Added send find page.
* Fixed some broken tests.
  • Loading branch information
n.bitounis committed Jun 1, 2020
1 parent c9ea481 commit cfc61bc
Show file tree
Hide file tree
Showing 18 changed files with 336 additions and 19 deletions.
26 changes: 26 additions & 0 deletions Projects/SesNotifications.App/Factories/DbSesSendFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using SesNotifications.App.Models;
using SesSend = SesNotifications.DataAccess.Entities.SesSend;

namespace SesNotifications.App.Factories
{
public static class DbSesSendFactory
{
public static SesSend Create(this SesSendModel open, long notificationId)
{
return new SesSend
{
Id = notificationId,
NotificationId = notificationId,
NotificationType = "Send",
SentAt = Convert.ToDateTime(open.Mail.Timestamp),
MessageId = open.Mail.MessageId,
Source = open.Mail.Source,
SourceArn = open.Mail.SourceArn,
SourceIp = open.Mail.SourceIp,
SendingAccountId = open.Mail.SendingAccountId,
Recipients = string.Join(',', open.Mail.Destination)
};
}
}
}
6 changes: 6 additions & 0 deletions Projects/SesNotifications.App/Models/SesSend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SesNotifications.App.Models
{
public class SesSend
{
}
}
8 changes: 8 additions & 0 deletions Projects/SesNotifications.App/Models/SesSendModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SesNotifications.App.Models
{
public class SesSendModel : Ses
{
public virtual SesMail Mail { get; set; }
public virtual SesSend Send { get; set; }
}
}
68 changes: 68 additions & 0 deletions Projects/SesNotifications.App/Pages/FindSends.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@page
@model SesNotifications.App.Pages.FindSendsModel
@{
ViewData["Title"] = "Find sends";
}

<!DOCTYPE html>

<h1>@ViewData["Title"]</h1>
<h4>Search for sends by date range and optional recipient.</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label>Starting date</label>
<input asp-for="Input.Start" class="form-control" />
<span asp-validation-for="Input.Start" class="text-danger"></span>

<label>Ending date</label>
<input asp-for="Input.End" class="form-control" />
<span asp-validation-for="Input.End" class="text-danger"></span>

<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control" />
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br />

<table class="table">
<tr>
<th>ID</th>
<th>Notification ID</th>
<th>Notification Type</th>
<th>Sent at</th>
<th>Message ID</th>
<th>Source</th>
<th>Recipients</th>
<th>Source ARN</th>
<th>Source IP</th>
<th>Sending Account ID</th>
<th>FeedbackId</th>
<th>Reporting MTA</th>
</tr>
@foreach (var record in Model.Bounces)
{
<tr>
<td>@record.Id</td>
<td>@record.NotificationId</td>
<td>@record.NotificationType</td>
<td>@record.SentAt.ToString("yyyy-MM-dd HH:mm:ssZ")</td>
<td>@record.MessageId</td>
<td>@record.Source</td>
<td>@record.Recipients</td>
<td>@record.SourceArn</td>
<td>@record.SourceIp</td>
<td>@record.SendingAccountId</td>
<td><a target="_blank" href="@Url.Action("FindRawById", "Searches", new {id = record.NotificationId})">RAW (MODEL)</a></td>
<td><a target="_blank" href="@string.Concat(Url.Action("FindMessageById", "Searches", new {id = record.NotificationId}), "/text")">RAW (MESSAGE)</a></td>
</tr>
}
</table>
</div>
</div>
44 changes: 44 additions & 0 deletions Projects/SesNotifications.App/Pages/FindSends.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SesNotifications.App.Services.Interfaces;
using SesNotifications.DataAccess.Entities;

namespace SesNotifications.App.Pages
{
public class FindSendsModel : PageModel
{
[BindProperty]
public InputModel Input { get; set; }

public IList<SesSend> Bounces { get; set; } = new List<SesSend>();

public class InputModel
{
[Required]
[DataType(DataType.Date)]
public DateTime Start { get; set; }

[Required]
[DataType(DataType.Date)]
public DateTime End { get; set; }

public string Email { get; set; }
}

private readonly ISearchService _searchService;

public FindSendsModel(ISearchService searchService)
{
_searchService = searchService;
}

public IActionResult OnPost()
{
Bounces = _searchService.FindSends(Input.Email, Input.Start, Input.End);
return Page();
}
}
}
1 change: 1 addition & 0 deletions Projects/SesNotifications.App/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<a class="dropdown-item" href="~/FindComplaints">Complaints</a>
<a class="dropdown-item" href="~/FindBounces">Bounces</a>
<a class="dropdown-item" href="~/FindOpens">Opens</a>
<a class="dropdown-item" href="~/FindSends">Sends</a>
<a class="dropdown-item" href="~/FindRaw">Raw</a>
</div>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface ISearchService
IList<SesComplaint> FindComplaints(string email, DateTime start, DateTime end);
IList<SesBounce> FindBounces(string email, DateTime start, DateTime end);
IList<SesOpen> FindOpens(string email, DateTime start, DateTime end);
IList<SesSend> FindSends(string email, DateTime start, DateTime end);
IList<SesNotification> FindRaw(DateTime start, DateTime end);
SesNotification FindRaw(long id);
}
Expand Down
19 changes: 16 additions & 3 deletions Projects/SesNotifications.App/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@ public class NotificationService : INotificationService
private const string Delivery = "delivery";
private const string Complaint = "complaint";
private const string Open = "open";
private const string Send = "send";

private readonly INotificationsRepository _notificationsRepository;
private readonly ISesBouncesRepository _sesBouncesRepository;
private readonly ISesComplaintsRepository _sesComplaintsRepository;
private readonly ISesDeliveriesRepository _sesDeliveriesRepository;
private readonly ISesOpensRepository _sesOpensRepository;
private readonly ISesSendsRepository _sesSendsRepository;
private readonly ILogger<NotificationService> _logger;

public NotificationService(INotificationsRepository notificationsRepository,
ISesBouncesRepository sesBouncesRepository,
ISesComplaintsRepository sesComplaintsRepository,
ISesDeliveriesRepository sesDeliveriesRepository,
ISesOpensRepository sesOpensRepository,
ISesSendsRepository sesSendsRepository,
ILogger<NotificationService> logger)
{
_notificationsRepository = notificationsRepository;
_sesBouncesRepository = sesBouncesRepository;
_sesComplaintsRepository = sesComplaintsRepository;
_sesDeliveriesRepository = sesDeliveriesRepository;
_sesOpensRepository = sesOpensRepository;
_sesSendsRepository = sesSendsRepository;
_logger = logger;
}

Expand Down Expand Up @@ -75,9 +79,6 @@ private void HandleNotificationInternal(string content)
case Bounce:
HandleBounce(content);
break;
case Open:
HandleOpen(content);
break;
default:
throw new NotSupportedException($"Unsupported message {content.Substring(0, 50)}...");
}
Expand All @@ -90,6 +91,9 @@ private void HandleNotificationInternal(string content)
case Open:
HandleOpen(content);
break;
case Send:
HandleSend(content);
break;
default:
throw new NotSupportedException($"Unsupported message {content.Substring(0, 50)}...");
}
Expand Down Expand Up @@ -132,6 +136,15 @@ private void HandleOpen(string content)
_sesOpensRepository.Save(open.Create(notification.Id));
}

private void HandleSend(string content)
{
var send = JsonConvert.DeserializeObject<SesSendModel>(content);

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

_sesSendsRepository.Save(send.Create(notification.Id));
}

private SesNotification SaveNotification(SesMail mail, string content)
{
var notification = mail.Create(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 @@ -16,20 +16,23 @@ public class SearchService : ISearchService
private readonly ISesComplaintsRepository _sesComplaintsRepository;
private readonly ISesDeliveriesRepository _sesDeliveriesRepository;
private readonly ISesOpensRepository _sesOpensRepository;
private readonly ISesSendsRepository _sesSendsRepository;
private readonly ILogger<SearchService> _logger;

public SearchService(INotificationsRepository notificationsRepository,
ISesBouncesRepository sesBouncesRepository,
ISesComplaintsRepository sesComplaintsRepository,
ISesDeliveriesRepository sesDeliveriesRepository,
ISesOpensRepository sesOpensRepository,
ISesSendsRepository sesSendsRepository,
ILogger<SearchService> logger)
{
_notificationsRepository = notificationsRepository;
_sesBouncesRepository = sesBouncesRepository;
_sesComplaintsRepository = sesComplaintsRepository;
_sesDeliveriesRepository = sesDeliveriesRepository;
_sesOpensRepository = sesOpensRepository;
_sesSendsRepository = sesSendsRepository;
_logger = logger;
}

Expand Down Expand Up @@ -61,6 +64,13 @@ public IList<SesOpen> FindOpens(string email, DateTime start, DateTime end)
: _sesOpensRepository.FindByRecipientAndSentDateRange($"%{email}%", start, end);
}

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

public IList<SesNotification> FindRaw(DateTime start, DateTime end)
{
return _notificationsRepository.FindBySentDateRange(start, end);
Expand Down
7 changes: 7 additions & 0 deletions Projects/SesNotifications.DataAccess/Entities/SesSend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SesNotifications.DataAccess.Entities
{
public class SesSend : SesCommon
{
public virtual string Recipients { get; set; }
}
}
14 changes: 14 additions & 0 deletions Projects/SesNotifications.DataAccess/Mappings/SesSendMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SesNotifications.DataAccess.Entities;

namespace SesNotifications.DataAccess.Mappings
{
public class SesSendMap : SesCommonMap<SesSend>
{
public SesSendMap()
{
Table("ses_notifications.sends");
MapCommon();
Map(x => x.Recipients).Column("recipients");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using SesNotifications.DataAccess.Entities;

namespace SesNotifications.DataAccess.Repositories.Interfaces
{
public interface ISesSendsRepository
{
void Save(SesSend sesSend);
SesSend FindById(long id);
IList<SesSend> FindByMessageId(string messageId);
IList<SesSend> FindBySentDateRange(DateTime start, DateTime end);
IList<SesSend> FindByRecipient(string email);
IList<SesSend> FindByRecipientAndSentDateRange(string email, DateTime start, DateTime end);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public SesOpensRepository(ISession session) : base(session)
{
}

public void Save(SesOpen sesDelivery)
public void Save(SesOpen sesOpen)
{
Session.Save(sesDelivery);
Session.Save(sesOpen);
}

public SesOpen FindById(long id)
Expand Down
Loading

0 comments on commit cfc61bc

Please sign in to comment.