Skip to content

Commit

Permalink
Overhaul searching/segmenting
Browse files Browse the repository at this point in the history
Resolves #453
Resolves #452
Resolves #317
  • Loading branch information
Jericho committed May 18, 2022
1 parent 101f74e commit fec10e6
Show file tree
Hide file tree
Showing 67 changed files with 447 additions and 1,027 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public async Task RunAsync(IClient client, TextWriter log, CancellationToken can
}
}

var firstNameCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.FirstName, "John");
var LastNameCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.LastName, "Doe");
var firstNameCriteria = new SearchCriteriaEqual(ContactsFilterField.FirstName, "John");
var LastNameCriteria = new SearchCriteriaEqual(ContactsFilterField.LastName, "Doe");
var searchResult = await client.Contacts.SearchAsync(new[] { firstNameCriteria, LastNameCriteria }, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"Found {searchResult.Length} contacts named John Doe").ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using StrongGrid.Models.Search.Legacy;
using StrongGrid.Models.Search;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down
8 changes: 4 additions & 4 deletions Source/StrongGrid.IntegrationTests/Tests/ListsAndSegments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ public async Task RunAsync(IClient client, TextWriter log, CancellationToken can
var contactId3 = await client.Contacts.UpsertAsync("[email protected]", "Bob", "Smith", listIds: new[] { list.Id }, cancellationToken: cancellationToken).ConfigureAwait(false);

// CREATE A SEGMENT (one contact matches the criteria)
var firstNameCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.FirstName, "John");
var LastNameCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.LastName, "Doe");
var firstNameCriteria = new SearchCriteriaEqual(ContactsFilterField.FirstName, "John");
var LastNameCriteria = new SearchCriteriaEqual(ContactsFilterField.LastName, "Doe");
var filterConditions = new[]
{
new KeyValuePair<SearchLogicalOperator, IEnumerable<SearchCriteria<ContactsFilterField>>>(SearchLogicalOperator.And, new[] { firstNameCriteria, LastNameCriteria})
new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.And, new[] { firstNameCriteria, LastNameCriteria})
};
var segment = await client.Segments.CreateAsync("StrongGrid Integration Testing: First Name is John and last name is Doe", filterConditions, list.Id, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"Segment '{segment.Name}' created. Id: {segment.Id}").ConfigureAwait(false);

// UPDATE THE SEGMENT (three contacts match the criteria)
var hotmailCriteria = new SearchCriteriaLike<ContactsFilterField>(ContactsFilterField.EmailAddress, "%hotmail.com");
var hotmailCriteria = new SearchCriteriaLike(ContactsFilterField.EmailAddress, "%hotmail.com");
segment = await client.Segments.UpdateAsync(segment.Id, "StrongGrid Integration Testing: Recipients @ Hotmail", hotmailCriteria, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"Segment {segment.Id} updated. The new name is: '{segment.Name}'").ConfigureAwait(false);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SearchCriteriaTests
public void SearchCriteriaBetween()
{
// Arrange
var searchCriteria = new SearchCriteriaBetween<ContactsFilterField>(ContactsFilterField.FirstName, "Allen", "Bob");
var searchCriteria = new SearchCriteriaBetween(ContactsFilterField.FirstName, "Allen", "Bob");

// Act
var result = searchCriteria.ToString();
Expand All @@ -24,7 +24,7 @@ public void SearchCriteriaBetween()
public void SearchCriteriaEqual()
{
// Arrange
var searchCriteria = new SearchCriteriaEqual<ContactsFilterField>(ContactsFilterField.LastName, "Smith");
var searchCriteria = new SearchCriteriaEqual(ContactsFilterField.LastName, "Smith");

// Act
var result = searchCriteria.ToString();
Expand All @@ -38,7 +38,7 @@ public void SearchCriteriaEqual()
public void SearchCriteriaContains()
{
// Arrange
var searchCriteria = new SearchCriteriaContains<ContactsFilterField>(ContactsFilterField.ListIds, "my-list-id");
var searchCriteria = new SearchCriteriaContains(ContactsFilterField.ListIds, "my-list-id");

// Act
var result = searchCriteria.ToString();
Expand Down
9 changes: 4 additions & 5 deletions Source/StrongGrid.UnitTests/Resources/EmailActivitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using StrongGrid.Json;
using StrongGrid.Models;
using StrongGrid.Models.Search;
using StrongGrid.Models.Search.Legacy;
using StrongGrid.Resources;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -115,7 +114,7 @@ public async Task SearchMessages_multiple_filter_conditions()
var limit = 25;

var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&query=campaign_name%3D%22value1%22+AND+status%3D%22processed%22").Respond("application/json", ONE_MESSAGE_FOUND);
mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&query=marketing_campaign_name%3D%22value1%22+AND+status%3D%22processed%22").Respond("application/json", ONE_MESSAGE_FOUND);

var client = Utils.GetFluentClient(mockHttp);
var emailActivities = (IEmailActivities)new EmailActivities(client);
Expand All @@ -142,15 +141,15 @@ public async Task SearchMessages_complex_filter_conditions()
var limit = 25;

var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&query=campaign_name%3D%22value1%22+OR+msg_id%3D%22value2%22+AND+subject%3D%22value3%22+AND+teammate%3D%22value4%22").Respond("application/json", ONE_MESSAGE_FOUND);
mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&query=marketing_campaign_name%3D%22value1%22+OR+msg_id%3D%22value2%22+AND+subject%3D%22value3%22+AND+teammate%3D%22value4%22").Respond("application/json", ONE_MESSAGE_FOUND);

var client = Utils.GetFluentClient(mockHttp);
var emailActivities = new EmailActivities(client);

var filterConditions = new[]
{
new KeyValuePair<SearchLogicalOperator, IEnumerable<StrongGrid.Models.Search.Legacy.ISearchCriteria>>(SearchLogicalOperator.Or, new[] { new SearchCriteriaEqual(EmailActivitiesFilterField.CampaignName, "value1"), new SearchCriteriaEqual(EmailActivitiesFilterField.MessageId, "value2") }),
new KeyValuePair<SearchLogicalOperator, IEnumerable<StrongGrid.Models.Search.Legacy.ISearchCriteria>>(SearchLogicalOperator.And, new[] { new SearchCriteriaEqual(EmailActivitiesFilterField.Subject, "value3"), new SearchCriteriaEqual(EmailActivitiesFilterField.Teammate, "value4") }),
new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.Or, new[] { new SearchCriteriaEqual(EmailActivitiesFilterField.CampaignName, "value1"), new SearchCriteriaEqual(EmailActivitiesFilterField.MessageId, "value2") }),
new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.And, new[] { new SearchCriteriaEqual(EmailActivitiesFilterField.Subject, "value3"), new SearchCriteriaEqual(EmailActivitiesFilterField.Teammate, "value4") }),
};

// Act
Expand Down
30 changes: 15 additions & 15 deletions Source/StrongGrid/Extensions/Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,9 @@ public static Task<WarmupResult> SendToMultipleRecipientsAsync(
/// <returns>
/// An array of <see cref="EmailMessageActivity" />.
/// </returns>
public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities emailActivities, Models.Search.Legacy.ISearchCriteria criteria, int limit = 20, CancellationToken cancellationToken = default)
public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities emailActivities, ISearchCriteria criteria, int limit = 20, CancellationToken cancellationToken = default)
{
var filterCriteria = criteria == null ? Enumerable.Empty<Models.Search.Legacy.ISearchCriteria>() : new[] { criteria };
var filterCriteria = criteria == null ? Enumerable.Empty<ISearchCriteria>() : new[] { criteria };
return emailActivities.SearchAsync(filterCriteria, limit, cancellationToken);
}

Expand All @@ -1068,10 +1068,10 @@ public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities ema
/// <returns>
/// An array of <see cref="EmailMessageActivity" />.
/// </returns>
public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities emailActivities, IEnumerable<Models.Search.Legacy.ISearchCriteria> filterConditions, int limit = 20, CancellationToken cancellationToken = default)
public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities emailActivities, IEnumerable<ISearchCriteria> filterConditions, int limit = 20, CancellationToken cancellationToken = default)
{
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<Models.Search.Legacy.ISearchCriteria>>>();
if (filterConditions != null && filterConditions.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<Models.Search.Legacy.ISearchCriteria>>(SearchLogicalOperator.And, filterConditions));
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>>();
if (filterConditions != null && filterConditions.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.And, filterConditions));
return emailActivities.SearchAsync(filters, limit, cancellationToken);
}

Expand All @@ -1084,9 +1084,9 @@ public static Task<EmailMessageActivity[]> SearchAsync(this IEmailActivities ema
/// <returns>
/// An array of <see cref="Contact" />.
/// </returns>
public static Task<Contact[]> SearchAsync(this IContacts contacts, SearchCriteria<ContactsFilterField> criteria, CancellationToken cancellationToken = default)
public static Task<Contact[]> SearchAsync(this IContacts contacts, ISearchCriteria criteria, CancellationToken cancellationToken = default)
{
var filterCriteria = criteria == null ? Array.Empty<SearchCriteria<ContactsFilterField>>() : new[] { criteria };
var filterCriteria = criteria == null ? Array.Empty<SearchCriteria>() : new[] { criteria };
return contacts.SearchAsync(filterCriteria, cancellationToken);
}

Expand All @@ -1099,10 +1099,10 @@ public static Task<Contact[]> SearchAsync(this IContacts contacts, SearchCriteri
/// <returns>
/// An array of <see cref="Contact" />.
/// </returns>
public static Task<Contact[]> SearchAsync(this IContacts contacts, IEnumerable<SearchCriteria<ContactsFilterField>> filterConditions, CancellationToken cancellationToken = default)
public static Task<Contact[]> SearchAsync(this IContacts contacts, IEnumerable<ISearchCriteria> filterConditions, CancellationToken cancellationToken = default)
{
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<SearchCriteria<ContactsFilterField>>>>();
if (filterConditions != null && filterConditions.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<SearchCriteria<ContactsFilterField>>>(SearchLogicalOperator.And, filterConditions));
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>>();
if (filterConditions != null && filterConditions.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.And, filterConditions));
return contacts.SearchAsync(filters, cancellationToken);
}

Expand All @@ -1117,9 +1117,9 @@ public static Task<Contact[]> SearchAsync(this IContacts contacts, IEnumerable<S
/// <returns>
/// The <see cref="Segment" />.
/// </returns>
public static Task<Segment> UpdateAsync(this ISegments segments, string segmentId, Parameter<string> name = default, Parameter<SearchCriteria<ContactsFilterField>> criteria = default, CancellationToken cancellationToken = default)
public static Task<Segment> UpdateAsync(this ISegments segments, string segmentId, Parameter<string> name = default, Parameter<SearchCriteria> criteria = default, CancellationToken cancellationToken = default)
{
var filterCriteria = criteria.HasValue && criteria.Value != null ? new[] { criteria.Value } : Array.Empty<SearchCriteria<ContactsFilterField>>();
var filterCriteria = criteria.HasValue && criteria.Value != null ? new[] { criteria.Value } : Array.Empty<SearchCriteria>();
return segments.UpdateAsync(segmentId, name, filterCriteria, cancellationToken);
}

Expand All @@ -1134,10 +1134,10 @@ public static Task<Segment> UpdateAsync(this ISegments segments, string segmentI
/// <returns>
/// The <see cref="Segment" />.
/// </returns>
public static Task<Segment> UpdateAsync(this ISegments segments, string segmentId, Parameter<string> name = default, Parameter<IEnumerable<SearchCriteria<ContactsFilterField>>> filterConditions = default, CancellationToken cancellationToken = default)
public static Task<Segment> UpdateAsync(this ISegments segments, string segmentId, Parameter<string> name = default, Parameter<IEnumerable<ISearchCriteria>> filterConditions = default, CancellationToken cancellationToken = default)
{
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<SearchCriteria<ContactsFilterField>>>>();
if (filterConditions.HasValue && filterConditions.Value != null && filterConditions.Value.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<SearchCriteria<ContactsFilterField>>>(SearchLogicalOperator.And, filterConditions.Value));
var filters = new List<KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>>();
if (filterConditions.HasValue && filterConditions.Value != null && filterConditions.Value.Any()) filters.Add(new KeyValuePair<SearchLogicalOperator, IEnumerable<ISearchCriteria>>(SearchLogicalOperator.And, filterConditions.Value));
return segments.UpdateAsync(segmentId, name, filters, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.Serialization;

namespace StrongGrid.Models.Search.Legacy
namespace StrongGrid.Models.Search
{
/// <summary>
/// Enumeration to indicate the filter field when searching for email activities.
Expand Down Expand Up @@ -34,6 +34,7 @@ public enum EmailActivitiesFilterField
/// <summary>
/// The type of email activity.
/// </summary>
/// <remarks>Valid values include "delivered","not_delivered", and "processing".</remarks>
[EnumMember(Value = "status")]
ActivityType,

Expand All @@ -43,36 +44,25 @@ public enum EmailActivitiesFilterField
[EnumMember(Value = "template_id")]
TemplateId,

/// <summary>
/// The name of the template.
/// </summary>
[EnumMember(Value = "template_name")]
TemplateName,

/// <summary>
/// The name of the campaign.
/// </summary>
[EnumMember(Value = "campaign_name")]
[EnumMember(Value = "marketing_campaign_name")]
CampaignName,

/// <summary>
/// The identifier of the campaign.
/// </summary>
[EnumMember(Value = "campaign_id")]
[EnumMember(Value = "marketing_campaign_id")]
CampaignId,

/// <summary>
/// The identifier of the Api Key.
/// </summary>
/// <remarks>Everything after the first "." and before the second ".".</remarks>
[EnumMember(Value = "api_key_id")]
ApiKeyId,

/// <summary>
/// The name of the Api Key.
/// </summary>
[EnumMember(Value = "api_key_name")]
ApiKeyName,

/// <summary>
/// Seems like a duplicate of 'status'.
/// </summary>
Expand Down Expand Up @@ -104,16 +94,9 @@ public enum EmailActivitiesFilterField
Clicks,

/// <summary>
/// The name of the unsubscribe group.
/// </summary>
[EnumMember(Value = "unsubscribe_group_name")]
UnsubscribeGroupName,

/// <summary>
/// The identified of the unsubscribe group.
/// The group id.
/// </summary>
[EnumMember(Value = "unsubscribe_group_id")]
UnsubscribeGroupId,
AsmGroupId,

/// <summary>
/// The teamates username.
Expand Down
14 changes: 0 additions & 14 deletions Source/StrongGrid/Models/Search/Legacy/ISearchCriteria.cs

This file was deleted.

Loading

0 comments on commit fec10e6

Please sign in to comment.