Skip to content

Commit 303aca3

Browse files
authored
Merge pull request #12 from naveego/revert-9-revert-8-issue/DATAINT-1917/fix-hubspot-endpoint-ilsid
Revert "Revert "DATAINT-1917: Extend membership endpoint to take manual ID input""
2 parents a0fb069 + 6ca7734 commit 303aca3

5 files changed

Lines changed: 113 additions & 124 deletions

File tree

PluginHubspot/API/Utility/EndpointHelperEndpoints/MembershipsEndpoints.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Net.Http;
43
using System.Text;
54
using System.Threading.Tasks;
@@ -38,12 +37,13 @@ public override async Task<string> WriteRecordAsync(IApiClient apiClient, Schema
3837
Encoding.UTF8,
3938
"application/json"
4039
);
41-
42-
var publisherInfo = JsonConvert.DeserializeObject<Hubspot>(schema.PublisherMetaJson);
43-
var listId = publisherInfo.EndpointSettings.MembershipsSettings?.ParseIlsId() ?? "";
40+
41+
var publisherInfo = JsonConvert.DeserializeObject<CustomWriteFormData>(schema.PublisherMetaJson);
42+
var manualIlsId = publisherInfo.MembershipsSettings?.ManualIlsId ?? "";
43+
var listId = string.IsNullOrWhiteSpace(manualIlsId) ? publisherInfo.MembershipsSettings?.ParseIlsId() ?? "" : manualIlsId.Trim();
4444
var url = string.Format(BasePath.TrimEnd('/'), listId);
4545
Logger.Info($"list id:{listId}, record id:{recordId}, action:{record.Action}");
46-
46+
4747
HttpResponseMessage response;
4848
if (record.Action == Record.Types.Action.Delete)
4949
{
@@ -122,5 +122,36 @@ public override Task<Schema> GetStaticSchemaAsync(IApiClient apiClient, Schema s
122122
}
123123
},
124124
};
125+
126+
public static async Task<List<string>> GetAllListId(IApiClient client)
127+
{
128+
var listIds = new List<string> ();
129+
const string path = "crm/v3/lists/search";
130+
var json = new StringContent(
131+
"{\"listIds\":[],\"offset\":0,\"processingTypes\":[\"MANUAL\", \"SNAPSHOT\"]}",
132+
Encoding.UTF8,
133+
"application/json"
134+
);
135+
var response = await client.PostAsync(path, json);
136+
var result = await response.Content.ReadAsStringAsync();
137+
var obj = JsonConvert.DeserializeObject<dynamic>(result);
138+
var lists = obj?.lists;
139+
140+
if (lists == null) return listIds;
141+
142+
foreach (var list in lists)
143+
{
144+
listIds.Add($"{list!.name.ToString()} ({list.listId.ToString()})");
145+
}
146+
return listIds;
147+
}
148+
149+
public static async Task<bool> DoListExist(IApiClient client, string listId)
150+
{
151+
var path = $"crm/v3/lists/{listId}";
152+
var response = await client.GetAsync(path);
153+
return response.IsSuccessStatusCode;
154+
}
155+
125156
}
126157
}
Lines changed: 37 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,69 @@
11
using System.Collections.Generic;
2-
using System.Net.Http;
3-
using System.Text;
4-
using System.Threading.Tasks;
2+
using System.Linq;
53
using Newtonsoft.Json;
6-
using PluginHubspot.API.Factory;
74
using PluginHubspot.API.Utility;
85

96
namespace PluginHubspot.API.Write
107
{
118
public static partial class Write
129
{
13-
public static string GetSchemaJson(string [] listIds)
10+
public static string GetSchemaJson(List<string> listIds)
1411
{
12+
// add empty string to the dropdown list
13+
var listIdsInDropdown = (new string[]{""}).Concat(listIds).ToArray();
14+
1515
var schemaJsonObj = new Dictionary<string, object>
1616
{
1717
{"type", "object"},
1818
{"properties", new Dictionary<string, object>
19+
{
20+
{"Endpoint", new Dictionary<string, object>
21+
{
22+
{"type", "string"},
23+
{"title", "Endpoint"},
24+
{"enum", new []{Constants.EndpointMemberships}}
25+
}},
26+
}},
27+
{"required", new [] {"Endpoint"}},
28+
{"dependencies", new Dictionary<string, object>
29+
{
30+
{"Endpoint", new Dictionary<string, object>
1931
{
20-
{"Hubspot", new Dictionary<string, object>
32+
{"oneOf", new []{new Dictionary<string, object>
2133
{
22-
{"type", "object"},
2334
{"properties", new Dictionary<string, object>
2435
{
2536
{"Endpoint", new Dictionary<string, object>
2637
{
27-
{"type", "string"},
28-
{"title", "Endpoint"},
2938
{"enum", new []{Constants.EndpointMemberships}}
3039
}},
31-
}},
32-
{"required", new [] {"Endpoint"}},
33-
{"dependencies", new Dictionary<string, object>
34-
{
35-
{"Endpoint", new Dictionary<string, object>
40+
{"MembershipsSettings", new Dictionary<string, object>
3641
{
37-
{"oneOf", new []{new Dictionary<string, object>
42+
{"type", "object"},
43+
{"title", "Target membership list in Hubspot"},
44+
{"description", "Either select a target list from the dropdown below or manually enter a list ID within the field below."},
45+
{"properties", new Dictionary<string, object>
3846
{
39-
{"properties", new Dictionary<string, object>
47+
{"IlsId", new Dictionary<string, object>
4048
{
41-
{"Endpoint", new Dictionary<string, object>
42-
{
43-
{"enum", new []{Constants.EndpointMemberships}}
44-
}},
45-
{"EndpointSettings", new Dictionary<string, object>
46-
{
47-
{"type", "object"},
48-
{"title", "Endpoint Settings"},
49-
{"properties", new Dictionary<string, object>
50-
{
51-
{"MembershipsSettings", new Dictionary<string, object>
52-
{
53-
{"type", "object"},
54-
{"title", ""},
55-
{"properties", new Dictionary<string, object>
56-
{
57-
{"IlsId", new Dictionary<string, object>
58-
{
59-
{"type", "string"},
60-
{"title", "Target membership list in Hubspot"},
61-
{"description", "Name of the Hubspot target list with ID in parenthesis."},
62-
{"enum", listIds}
63-
}},
64-
}},
65-
{"required", new []{"IlsId"}}
66-
}}
67-
}}
68-
}}
69-
}}
70-
}}}
49+
{"type", "string"},
50+
{"title", "NAME OF THE HUBSPOT TARGET LIST WITH ID IN PARETHESIS (LIMITED UP TO 20 RESULTS)."},
51+
{"enum", listIdsInDropdown}
52+
}},
53+
{"ManualIlsId", new Dictionary<string, object>
54+
{
55+
{"type", "string"},
56+
{"title", "MANUALLY ENTER LIST ID."},
57+
}},
58+
}},
7159
}}
7260
}}
73-
}}
74-
}
75-
76-
},
77-
{
78-
"required", new[]
79-
{
80-
"Hubspot"
81-
}
82-
}
61+
}}}
62+
}}
63+
}}
8364
};
8465

8566
return JsonConvert.SerializeObject(schemaJsonObj);
8667
}
87-
88-
public static async Task<string[]> GetAllListId(IApiClient client)
89-
{
90-
var listIds = new List<string>();
91-
const string path = "crm/v3/lists/search";
92-
var json = new StringContent(
93-
"{\"listIds\":[],\"offset\":0,\"processingTypes\":[\"MANUAL\", \"SNAPSHOT\"]}",
94-
Encoding.UTF8,
95-
"application/json"
96-
);
97-
var response = await client.PostAsync(path, json);
98-
var result = await response.Content.ReadAsStringAsync();
99-
var obj = JsonConvert.DeserializeObject<dynamic>(result);
100-
var lists = obj?.lists;
101-
102-
if (lists == null) return listIds.ToArray();
103-
104-
foreach (var list in lists)
105-
{
106-
listIds.Add($"{list.name.ToString()} ({list.listId.ToString()})");
107-
}
108-
return listIds.ToArray();
109-
}
110-
11168
}
11269
}

PluginHubspot/API/Write/GetUIJson.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,8 @@ public static partial class Write
77
{
88
public static string GetUIJson()
99
{
10-
var uiJsonObj = new Dictionary<string, object>
11-
{
12-
{"ui:order", new []
13-
{
14-
"Hubspot"
15-
}},
16-
{"Hubspot", new Dictionary<string, object>
17-
{
18-
{"ui:order", new []
19-
{
20-
"Endpoint","*"
21-
}},
22-
}}
23-
};
10+
var uiJsonObj = new Dictionary<string, object>{};
11+
2412
return JsonConvert.SerializeObject(uiJsonObj);
2513
}
2614
}
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net.Http;
4-
using System.Text;
5-
using Newtonsoft.Json;
6-
7-
namespace PluginHubspot.DataContracts
1+
namespace PluginHubspot.DataContracts
82
{
93
public class CustomWriteFormData
10-
{
11-
public Hubspot Hubspot { get; set; }
12-
}
13-
14-
public class Hubspot
154
{
165
public string Endpoint { get; set; }
17-
public EndpointSettings EndpointSettings { get; set; }
18-
}
19-
20-
public class EndpointSettings
21-
{
226
public MembershipsSettings? MembershipsSettings { get; set; }
237
}
248

259
public class MembershipsSettings
2610
{
27-
public string IlsId { get; set; }
11+
public string? IlsId { get; set; }
12+
public string? ManualIlsId { get; set; }
2813
public string ParseIlsId()
2914
{
30-
return IlsId[(IlsId.LastIndexOf('(') + 1)..IlsId.LastIndexOf(')')];
15+
return IlsId![(IlsId!.LastIndexOf('(') + 1)..IlsId.LastIndexOf(')')];
3116
}
3217
}
3318
}

PluginHubspot/Plugin/Plugin.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using PluginHubspot.API.Factory;
1515
using PluginHubspot.API.Read;
1616
using PluginHubspot.API.Utility;
17+
using PluginHubspot.API.Utility.EndpointHelperEndpoints;
1718
using PluginHubspot.API.Write;
1819
using PluginHubspot.DataContracts;
1920
using PluginHubspot.Helper;
@@ -329,7 +330,7 @@ public override async Task<DiscoverSchemasResponse> DiscoverSchemas(DiscoverSche
329330
Logger.SetLogPrefix("discover");
330331
Logger.Info("Discovering Schemas...");
331332

332-
var sampleSize = checked((int) request.SampleSize);
333+
var sampleSize = checked((int)request.SampleSize);
333334

334335
DiscoverSchemasResponse discoverSchemasResponse = new DiscoverSchemasResponse();
335336

@@ -483,7 +484,7 @@ public override async Task<ConfigureWriteResponse> ConfigureWrite(ConfigureWrite
483484
{
484485
Logger.Info("Configuring write...");
485486

486-
var listIds = await Write.GetAllListId(_apiClient);
487+
var listIds = await MembershipsEndpointHelper.GetAllListId(_apiClient);
487488
var schemaJson = Write.GetSchemaJson(listIds);
488489
var uiJson = Write.GetUIJson();
489490

@@ -509,12 +510,39 @@ public override async Task<ConfigureWriteResponse> ConfigureWrite(ConfigureWrite
509510
{
510511
// get form data
511512
var formData = JsonConvert.DeserializeObject<CustomWriteFormData>(request.Form.DataJson);
512-
var endpoint = EndpointHelper.GetEndpointForCustomSchema(formData.Hubspot.Endpoint);
513+
514+
if (formData.Endpoint == Constants.EndpointMemberships)
515+
{
516+
if (formData.MembershipsSettings == null)
517+
{
518+
throw new Exception("Invalid endpoint settings!");
519+
}
520+
else
521+
{
522+
var ilsId = formData.MembershipsSettings.IlsId;
523+
var manualIlsId = formData.MembershipsSettings.ManualIlsId;
524+
if (string.IsNullOrWhiteSpace(ilsId) && string.IsNullOrWhiteSpace(manualIlsId))
525+
{
526+
throw new Exception("A valid list ID is required!");
527+
}
528+
529+
if (!string.IsNullOrWhiteSpace(manualIlsId))
530+
{
531+
var doListExist = await MembershipsEndpointHelper.DoListExist(_apiClient, manualIlsId.Trim());
532+
if (!doListExist)
533+
{
534+
throw new Exception("List ID does not exist!");
535+
}
536+
}
537+
}
538+
}
539+
540+
var endpoint = EndpointHelper.GetEndpointForCustomSchema(formData.Endpoint);
513541
var schema = new Schema
514542
{
515543
Id = endpoint!.Id,
516544
Name = endpoint.Name,
517-
PublisherMetaJson = JsonConvert.SerializeObject(formData.Hubspot),
545+
PublisherMetaJson = JsonConvert.SerializeObject(formData),
518546
DataFlowDirection = endpoint.GetDataFlowDirection(),
519547
Query = ""
520548
};
@@ -542,7 +570,7 @@ public override async Task<ConfigureWriteResponse> ConfigureWrite(ConfigureWrite
542570
Form = new ConfigurationFormResponse
543571
{
544572
DataJson = request.Form.DataJson,
545-
Errors = {e.Message},
573+
Errors = { e.Message },
546574
SchemaJson = schemaJson,
547575
UiJson = uiJson,
548576
StateJson = request.Form.StateJson

0 commit comments

Comments
 (0)