Skip to content

Commit

Permalink
Adding bounce events in the database.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbitounis committed Jun 2, 2020
1 parent 6b13c25 commit 8564761
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Projects/SesNotifications.DataAccess/Entities/SesBounceEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace SesNotifications.DataAccess.Entities
{
public class SesBounceEvent : SesCommon
{
public virtual string BounceType { get; set; }
public virtual string BounceSubType { get; set; }
public virtual DateTime CreatedAt { get; set; }
public virtual string FeedbackId { get; set; }
public virtual string ReportingMta { get; set; }
public virtual string BouncedRecipients { get; set; }
}
}
19 changes: 19 additions & 0 deletions Projects/SesNotifications.DataAccess/Mappings/SesBounceEventMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SesNotifications.DataAccess.Entities;

namespace SesNotifications.DataAccess.Mappings
{
public class SesBounceEventMap : SesCommonMap<SesBounceEvent>
{
public SesBounceEventMap()
{
Table("ses_notifications.bounceevents");
MapCommon();
Map(x => x.BounceType).Column("bounce_type");
Map(x => x.BounceSubType).Column("bounce_sub_type");
Map(x => x.CreatedAt).Column("created_at");
Map(x => x.FeedbackId).Column("feedback_id");
Map(x => x.ReportingMta).Column("reporting_mta");
Map(x => x.BouncedRecipients).Column("bounced_recipients");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SesNotifications.DataAccess.Entities;
using System;
using System.Collections.Generic;

namespace SesNotifications.DataAccess.Repositories.Interfaces
{
public interface ISesBounceEventsRepository
{
void Save(SesBounceEvent sesBounceEvent);
SesBounceEvent FindById(long id);
IList<SesBounceEvent> FindByMessageId(string messageId);
IList<SesBounceEvent> FindBySentDateRange(DateTime start, DateTime end);
IList<SesBounceEvent> FindByRecipient(string email);
IList<SesBounceEvent> FindByRecipientAndSentDateRange(string email, DateTime start, DateTime end);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using NHibernate;
using NHibernate.Criterion;
using SesNotifications.DataAccess.Entities;
using SesNotifications.DataAccess.Repositories.Interfaces;
using System;
using System.Collections.Generic;

namespace SesNotifications.DataAccess.Repositories
{
public class SesBounceEventsRepository : Repository, ISesBounceEventsRepository
{
public SesBounceEventsRepository()
{
}

public SesBounceEventsRepository(ISession session) : base(session)
{
}

public void Save(SesBounceEvent sesBounceEvent)
{
Session.Save(sesBounceEvent);
}

public SesBounceEvent FindById(long id)
{
return Session.Get<SesBounceEvent>(id);
}

public IList<SesBounceEvent> FindByMessageId(string messageId)
{
return Session.CreateCriteria<SesBounceEvent>()
.Add(Restrictions.Eq(nameof(SesBounceEvent.MessageId), messageId))
.List<SesBounceEvent>();
}

public IList<SesBounceEvent> FindBySentDateRange(DateTime start, DateTime end)
{
return Session.CreateCriteria<SesBounceEvent>()
.Add(Restrictions.Ge(nameof(SesBounceEvent.SentAt), start))
.Add(Restrictions.Le(nameof(SesBounceEvent.SentAt), end))
.AddOrder(Order.Desc(nameof(SesBounceEvent.SentAt)))
.List<SesBounceEvent>();
}

public IList<SesBounceEvent> FindByRecipient(string email)
{
return Session.CreateCriteria<SesBounceEvent>()
.Add(Restrictions.InsensitiveLike(nameof(SesBounceEvent.BouncedRecipients), email))
.AddOrder(Order.Desc(nameof(SesBounceEvent.SentAt)))
.List<SesBounceEvent>();
}

public IList<SesBounceEvent> FindByRecipientAndSentDateRange(string email, DateTime start, DateTime end)
{
return Session.CreateCriteria<SesBounceEvent>()
.Add(Restrictions.InsensitiveLike(nameof(SesBounceEvent.BouncedRecipients), email))
.Add(Restrictions.Ge(nameof(SesBounceEvent.SentAt), start))
.Add(Restrictions.Le(nameof(SesBounceEvent.SentAt), end))
.AddOrder(Order.Desc(nameof(SesBounceEvent.SentAt)))
.List<SesBounceEvent>();
}
}
}
2 changes: 2 additions & 0 deletions Projects/SesNotifications.DataAccess/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static IServiceCollection AddNHibernate(this IServiceCollection services,
.Add<SesOpenEventMap>()
.Add<SesSendEventMap>()
.Add<SesDeliveryEventMap>()
.Add<SesBounceEventMap>()
)
.BuildSessionFactory();

Expand All @@ -49,6 +50,7 @@ public static IServiceCollection AddScopedRepositories(this IServiceCollection s
services.AddScoped<ISesOpensEventsRepository, SesOpensEventsRepository>();
services.AddScoped<ISesSendEventsRepository, SesSendEventsRepository>();
services.AddScoped<ISesDeliveryEventsRepository, SesDeliveryEventsRepository>();
services.AddScoped<ISesBounceEventsRepository, SesBounceEventsRepository>();

return services;
}
Expand Down
28 changes: 28 additions & 0 deletions Sql/ses_notifications_init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
CREATE SCHEMA ses_notifications AUTHORIZATION postgres;

CREATE TABLE ses_notifications.bounceevents (
id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
notification_id int8 NOT NULL,
notification_type varchar(32) NOT NULL,
sent_at timestamptz NOT NULL,
message_id varchar(128) NOT NULL,
source varchar(256) NOT NULL COLLATE "ucs_basic",
source_arn varchar(256) NULL,
source_ip varchar(32) NULL,
sending_account_id varchar(128) NULL,
bounce_type varchar(128) NOT NULL,
bounce_sub_type varchar(128) NOT NULL,
created_at timestamptz NULL,
feedback_id varchar(256) NULL,
reporting_mta varchar(256) NULL,
remote_mta_ip varchar(32) NULL,
bounced_recipients varchar(64000) NULL COLLATE "ucs_basic"
);
CREATE INDEX bounceevents_bounce_sub_type_idx ON ses_notifications.bounceevents USING btree (bounce_sub_type);
CREATE INDEX bounceevents_bounce_type_idx ON ses_notifications.bounceevents USING btree (bounce_type);
CREATE INDEX bounceevents_bounced_recipients_idx ON ses_notifications.bounceevents USING btree (bounced_recipients);
CREATE INDEX bounceevents_created_at_idx ON ses_notifications.bounceevents USING btree (created_at);
CREATE INDEX bounceevents_from_idx ON ses_notifications.bounceevents USING btree (source);
CREATE UNIQUE INDEX bounceevents_id_idx ON ses_notifications.bounceevents USING btree (id);
CREATE INDEX bounceevents_message_id_idx ON ses_notifications.bounceevents USING btree (message_id);
CREATE INDEX bounceevents_notification_id_idx ON ses_notifications.bounceevents USING btree (notification_id);
CREATE INDEX bounceevents_sent_at_idx ON ses_notifications.bounceevents USING btree (sent_at);

CREATE TABLE ses_notifications.deliveryevents (
id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
notification_id int8 NOT NULL,
Expand Down

0 comments on commit 8564761

Please sign in to comment.