Skip to content

Commit

Permalink
feat: update contact now provides update_mask
Browse files Browse the repository at this point in the history
  • Loading branch information
Dovchik committed Jan 17, 2024
1 parent 50c2974 commit 0f23470
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 13 deletions.
92 changes: 83 additions & 9 deletions src/Sinch/Conversation/Contacts/Contact.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinch.Conversation.Messages;
using Sinch.Core;

namespace Sinch.Conversation.Contacts
{
public class Contact
public sealed class Contact
{
/// <summary>
/// Tracks the fields which where initialized.
/// </summary>
private readonly ISet<string> _setFields = new HashSet<string>();

private List<ChannelIdentity> _channelIdentities;
private List<ConversationChannel> _channelPriority;
private string _displayName;
private string _email;
private string _externalId;
private string _id;
private string _language;
private string _metadata;

/// <summary>
/// List of channel identities.
Expand All @@ -30,44 +39,109 @@ public List<ChannelIdentity> ChannelIdentities
/// <summary>
/// List of channels defining the channel priority.
/// </summary>
public List<ConversationChannel> ChannelPriority { get; set; }
public List<ConversationChannel> ChannelPriority
{
get => _channelPriority;
set
{
_setFields.Add(nameof(ChannelPriority));
_channelPriority = value;
}
}


/// <summary>
/// The display name. A default &#39;Unknown&#39; will be assigned if left empty.
/// </summary>
public string DisplayName { get; set; }
public string DisplayName
{
get => _displayName;
set
{
_setFields.Add(nameof(DisplayName));
_displayName = value;
}
}


/// <summary>
/// Email of the contact.
/// </summary>
public string Email { get; set; }
public string Email
{
get => _email;
set
{
_setFields.Add(nameof(Email));
_email = value;
}
}


/// <summary>
/// Contact identifier in an external system.
/// </summary>
public string ExternalId { get; set; }
public string ExternalId
{
get => _externalId;
set
{
_setFields.Add(nameof(ExternalId));
_externalId = value;
}
}


/// <summary>
/// The ID of the contact.
/// </summary>
public string Id { get; set; }
public string Id
{
get => _id;
set
{
_setFields.Add(nameof(Id));
_id = value;
}
}


/// <summary>
/// Gets or Sets Language
/// </summary>
public string Language { get; set; }
public string Language
{
get => _language;
set
{
_setFields.Add(nameof(Language));
_language = value;
}
}


/// <summary>
/// Metadata associated with the contact. Up to 1024 characters long.
/// </summary>
public string Metadata { get; set; }
public string Metadata
{
get => _metadata;
set
{
_setFields.Add(nameof(Metadata));
_metadata = value;
}
}

/// <summary>
/// Get the comma separated snake_case list of properties which were directly initialized in this object.
/// If, for example, DisplayName and Metadata were set, will return <example>display_name,metadata</example>
/// </summary>
/// <returns></returns>
internal string GetPropertiesMask()
{
return string.Join(',', _setFields.Select(StringUtils.ToSnakeCase));
}

/// <summary>
/// Returns the string presentation of the object
Expand Down
5 changes: 4 additions & 1 deletion src/Sinch/Conversation/Contacts/Contacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ public Task<ChannelProfile> GetChannelProfile(GetChannelProfileRequest request,
public Task<Contact> Update(Contact contact, CancellationToken cancellationToken = default)
{
_logger?.LogDebug("Updating a {contactId} of {projectId}", contact.Id, _projectId);
var uri = new Uri(_baseAddress, $"/v1/projects/{_projectId}/contacts/{contact.Id}");
// the update_mask param will regulate which properties to set.
// Keep in mind that no depth is supported: for example, you cannot mask channel_identities.identity
var uri = new Uri(_baseAddress,
$"/v1/projects/{_projectId}/contacts/{contact.Id}?update_mask={contact.GetPropertiesMask()}");
return _http.Send<Contact, Contact>(uri, HttpMethod.Patch, contact,
cancellationToken);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sinch/Conversation/Messages/ContactId.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sinch.Conversation.Messages
{
public sealed class Contact : IRecipient
public sealed class ContactRecipient : IRecipient
{
/// <summary>
/// The ID of the contact.
Expand Down
2 changes: 1 addition & 1 deletion src/Sinch/Conversation/Messages/IRecipient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sinch.Conversation.Messages
{
[JsonDerivedType(typeof(Contact))]
[JsonDerivedType(typeof(ContactRecipient))]
[JsonDerivedType(typeof(Identified))]
public interface IRecipient
{
Expand Down
42 changes: 42 additions & 0 deletions tests/Sinch.Tests/Conversation/ContactsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using FluentAssertions;
using Sinch.Conversation;
using Sinch.Conversation.Contacts;
using Sinch.Conversation.Messages;
using Xunit;

namespace Sinch.Tests.Conversation
{
public class ContactsTests : ConversationTestBase
{
[Fact]
public void ContactMaskTwoFields()
{
var contact = new Contact()
{
DisplayName = "hola",
Metadata = null
};
contact.GetPropertiesMask().Should().BeEquivalentTo("display_name,metadata");
}

[Fact]
public void ContactMaskAllFields()
{
var contact = new Contact()
{
DisplayName = "hola",
Metadata = "aaaa",
ExternalId = "id",
ChannelPriority = new List<ConversationChannel>(),
Email = "mail",
ChannelIdentities = new List<ChannelIdentity>(),
Language = "lang",
Id = "id",
};
contact.GetPropertiesMask().Should()
.BeEquivalentTo(
"display_name,metadata,external_id,channel_priority,email,channel_identities,language,id");
}
}
}
2 changes: 1 addition & 1 deletion tests/Sinch.Tests/Conversation/SendMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SendMessageTests : ConversationTestBase
ExplicitChannelMessage = null,
AdditionalProperties = null
},
Recipient = new Contact()
Recipient = new ContactRecipient()
{
ContactId = "ContactEasy"
}
Expand Down

0 comments on commit 0f23470

Please sign in to comment.