|
using System; |
|
using System.Collections.Generic; |
|
using System.ComponentModel; |
|
using System.Linq; |
|
using System.Net.Http; |
|
using System.Text; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
using Progress.Sitefinity.Renderer.Designers.Attributes; |
|
using SitefinityWebApp; |
|
using Telerik.Sitefinity.Configuration; |
|
using Telerik.Sitefinity.Data.Linq.Dynamic; |
|
using Telerik.Sitefinity.HubSpotConnector.Configuration; |
|
using Telerik.Sitefinity.Security; |
|
using Telerik.Sitefinity.Security.Model; |
|
using Telerik.Sitefinity.ServiceHooks; |
|
using Telerik.Sitefinity.ServiceHooks.Model; |
|
using Telerik.Sitefinity.Services; |
|
|
|
[assembly: SitefinityModule(HubSpotConnectivityModule.ModuleName, typeof(HubSpotConnectivityModule), "HubSpot connectivity", "", StartupType.OnApplicationStart)] |
|
namespace SitefinityWebApp |
|
{ |
|
public class HubSpotContactCreationActionSettings : IServiceHookActionSettings |
|
{ |
|
/// <inheritdoc /> |
|
public virtual string Title |
|
{ |
|
get |
|
{ |
|
return ActionTitle; |
|
} |
|
} |
|
|
|
/// <inheritdoc /> |
|
public virtual string Key |
|
{ |
|
get |
|
{ |
|
return Name; |
|
} |
|
} |
|
|
|
/// <inheritdoc /> |
|
public virtual Type ParametersType |
|
{ |
|
get |
|
{ |
|
return typeof(HubSpotContactCreationActionParameters); |
|
} |
|
} |
|
|
|
/// <inheritdoc /> |
|
public virtual async Task<ServiceHookActionResult> ExecuteActionAsync(ITriggerData triggerData, object parameters, CancellationToken cancellationToken) |
|
{ |
|
if (triggerData == null) |
|
throw new ArgumentNullException(nameof(triggerData)); |
|
|
|
if (parameters == null) |
|
throw new ArgumentNullException(nameof(parameters)); |
|
|
|
if (triggerData.OriginalEvent != null && triggerData.OriginalEvent.UserId != null) |
|
{ |
|
var userId = Guid.Parse(triggerData.OriginalEvent.UserId.ToString()); |
|
if (userId != Guid.Empty) |
|
{ |
|
var actionParameters = parameters as HubSpotContactCreationActionParameters; |
|
if (actionParameters != null) |
|
{ |
|
var apiKey = this.GetApiKey(actionParameters.ApiKey); |
|
|
|
if (!string.IsNullOrEmpty(apiKey)) |
|
{ |
|
var url = $"https://api.hubapi.com/crm/v3/objects/contacts?hapikey={apiKey}"; |
|
|
|
UserManager userManager = UserManager.GetManager(); |
|
User user = userManager.GetUser(userId); |
|
|
|
if (user != null) |
|
{ |
|
UserProfileManager profileManager = UserProfileManager.GetManager(); |
|
var profile = profileManager.GetUserProfile<SitefinityProfile>(user); |
|
var requestContent = new StringContent( |
|
"{{\"properties\":{{\"company\":\"{0}\",\"email\":\"{1}\",\"firstname\":\"{2}\",\"lastname\":\"{3}\",\"phone\":\"{4}\",\"website\":\"{5}\"}}}}" |
|
.Arrange("mycompany", user.Email, profile.FirstName, profile.LastName, "1113334444222", "www.mycompanysite.com"), |
|
Encoding.UTF8, |
|
"application/json"); |
|
|
|
if (this.CanSendRequest(actionParameters.RoleType, userId)) |
|
{ |
|
var request = new HttpRequestMessage(HttpMethod.Post, url); |
|
request.Content = requestContent; |
|
|
|
// If a hubspot contact does not exist, sending will create new |
|
// If it exists, in HubSpot, in the api key logs for request response, you will see "Contact already exists" message. |
|
using (HttpClient httpClient = new HttpClient()) |
|
{ |
|
HttpResponseMessage httpResponseMessage = null; |
|
|
|
try |
|
{ |
|
httpResponseMessage = await httpClient.SendAsync(request); |
|
httpResponseMessage.EnsureSuccessStatusCode(); |
|
} |
|
catch (HttpRequestException exc) |
|
{ |
|
if (httpResponseMessage != null && httpResponseMessage.StatusCode != System.Net.HttpStatusCode.Conflict) |
|
{ |
|
throw exc; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
return new ServiceHookActionResult(null, true); |
|
} |
|
|
|
private string GetApiKey(string paramaterApiKey) |
|
{ |
|
var apiKey = paramaterApiKey; |
|
if (string.IsNullOrEmpty(apiKey)) |
|
{ |
|
var configManager = ConfigManager.GetManager(); |
|
var hubSpotConnectorConfig = configManager.GetSection<HubSpotConnectorConfig>(); |
|
|
|
apiKey = hubSpotConnectorConfig.HubSpotApiKey; |
|
} |
|
|
|
return apiKey; |
|
} |
|
|
|
private bool CanSendRequest(RoleType roleType, Guid userId) |
|
{ |
|
IList<Role> roles = RoleManager.FindRolesForUser(userId); |
|
var canSendRequest = true; |
|
|
|
switch (roleType) |
|
{ |
|
case RoleType.NonAdminstrators: |
|
canSendRequest = roles.Any(role => !SecurityManager.IsAdministrativeRole(role.Id)); |
|
break; |
|
case RoleType.Administrators: |
|
canSendRequest = roles.Any(role => SecurityManager.IsAdministrativeRole(role.Id)); |
|
break; |
|
case RoleType.All: |
|
default: |
|
break; |
|
} |
|
|
|
return canSendRequest; |
|
} |
|
|
|
private const string Name = "HubSpotContactCreation"; |
|
private const string ActionTitle = "HubSpot Contact Creation"; |
|
|
|
public enum RoleType |
|
{ |
|
NonAdminstrators, |
|
Administrators, |
|
All |
|
} |
|
|
|
public class HubSpotContactCreationActionParameters |
|
{ |
|
[DisplayName("Role type")] |
|
[DefaultValue(RoleType.NonAdminstrators)] |
|
[Choice("[{\"Title\":\"Non adminstrators\",\"Name\":\"NonAdminstrators\",\"Value\":\"NonAdminstrators\",\"Icon\":null},{\"Title\":\"Administrators\",\"Name\":\"Administrators\",\"Value\":\"Administrators\",\"Icon\":null},{\"Title\":\"All\",\"Name\":\"All\",\"Value\":\"All\",\"Icon\":null}]", NotResponsive = true)] |
|
public RoleType RoleType { get; set; } |
|
|
|
[DisplayName("Api Key")] |
|
[DefaultValue("")] |
|
public string ApiKey { get; set; } |
|
} |
|
} |
|
} |