Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for UserFields in ZendeskApi_v2 #599

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ZendeskApi_v2/Models/Constants/UserFieldTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ZendeskApi_v2.Models.Constants
{
public static class UserFieldTypes
{
public const string Text = "text";
public const string Textarea = "textarea";
public const string Checkbox = "checkbox";
public const string Date = "date";
public const string Integer = "integer";
public const string Decimal = "decimal";
public const string Regexp = "regexp";
public const string Dropdown = "dropdown";
public const string Lookup = "lookup";
public const string Multiselect = "multiselect";
}
}
19 changes: 19 additions & 0 deletions src/ZendeskApi_v2/Models/Shared/CustomFieldOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace ZendeskApi_v2.Models.Shared
{
public class CustomFieldOption
{
[JsonProperty("id")]
public long? Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("raw_name")]
public string RawName { get; set; }

[JsonProperty("value")]
public string Value { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/ZendeskApi_v2/Models/Users/GroupUserFieldResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ZendeskApi_v2.Models.Users
{
public class GroupUserFieldResponse : GroupResponseBase
{
[JsonProperty("user_fields")]
public IList<UserField> UserFields { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/ZendeskApi_v2/Models/Users/IndividualUserFieldResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace ZendeskApi_v2.Models.Users
{
public class IndividualUserFieldResponse
{
[JsonProperty("user_field")]
public UserField UserField { get; set; }
}
}
61 changes: 61 additions & 0 deletions src/ZendeskApi_v2/Models/Users/UserField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ZendeskApi_v2.Models.Shared;

namespace ZendeskApi_v2.Models.Users
{
public class UserField
{
[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("id")]
public long? Id { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("key")]
public string Key { get; set; }

[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("raw_title")]
public string RawTitle { get; set; }

[JsonProperty("raw_description")]
public string RawDescription { get; set; }

[JsonProperty("position")]
public long? Position { get; set; }

[JsonProperty("active")]
public bool? Active { get; set; }

[JsonProperty("system")]
public bool? System { get; set; }

[JsonProperty("regexp_for_validation")]
public string RegexpForValidation { get; set; }

[JsonProperty("created_at")]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTimeOffset? CreatedAt { get; set; }

[JsonProperty("updated_at")]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTimeOffset? UpdatedAt { get; set; }

[JsonProperty("tag")]
public string Tag { get; set; }

[JsonProperty("custom_field_options")]
public IList<CustomFieldOption> CustomFieldOptions { get; set; }
}
}
108 changes: 107 additions & 1 deletion src/ZendeskApi_v2/Requests/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public interface IUsers : ICore
GroupUserExportResponse GetIncrementalUserExportNextPage(string nextPage);

GroupSubscriptionsResponse GetSubscriptions(long userId, SubscriptionSideLoadOptions subscriptionSideLoadOptions = SubscriptionSideLoadOptions.None, int? perPage = null, int? page = null);

GroupUserFieldResponse GetUserFields();
IndividualUserFieldResponse GetUserFieldById(long id);
IndividualUserFieldResponse CreateUserField(UserField userField, bool replaceNameSpacesWithUnderscore = true);
IndividualUserFieldResponse UpdateUserField(UserField userField, bool replaceNameSpacesWithUnderscore = false);
bool DeleteUserField(long id);
#endif

#if ASYNC
Expand Down Expand Up @@ -207,14 +213,20 @@ public interface IUsers : ICore
Task<GroupUserExportResponse> GetIncrementalUserExportNextPageAsync(string nextPage);

Task<GroupSubscriptionsResponse> GetSubscriptionsAsync(long userId, SubscriptionSideLoadOptions subscriptionSideLoadOptions = SubscriptionSideLoadOptions.None, int? perPage = null, int? page = null);

Task<GroupUserFieldResponse> GetUserFieldsAsync();
Task<IndividualUserFieldResponse> GetUserFieldByIdAsync(long id);
Task<IndividualUserFieldResponse> CreateUserFieldAsync(UserField userField, bool replaceNameSpacesWithUnderscore = true);
Task<IndividualUserFieldResponse> UpdateUserFieldAsync(UserField userField, bool replaceNameSpacesWithUnderscore = false);
Task<bool> DeleteUserFieldAsync(long id);
#endif
}

public class Users : Core, IUsers
{
private const string _incremental_export = "incremental/users.json?start_time=";

public Users(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
public Users(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string, string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}
Expand Down Expand Up @@ -478,6 +490,53 @@ public GroupSubscriptionsResponse GetSubscriptions(long userId, SubscriptionSide
{
return GenericPagedGet<GroupSubscriptionsResponse>($"help_center/users/{userId}/subscriptions.json".SubscriptionSideloadUri(subscriptionSideLoadOptions), perPage, page);
}

public GroupUserFieldResponse GetUserFields()
{
return GenericGet<GroupUserFieldResponse>("user_fields.json");
}

public IndividualUserFieldResponse GetUserFieldById(long id)
{
return GenericGet<IndividualUserFieldResponse>($"user_fields/{id}.json");
}
public IndividualUserFieldResponse CreateUserField(UserField userField, bool replaceNameSpacesWithUnderscore = true)
{
if (userField.CustomFieldOptions != null)
{
foreach (var field in userField.CustomFieldOptions)
{
if (replaceNameSpacesWithUnderscore)
{
field.Name = field.Name.Replace(' ', '_');
}
field.Value = field.Value.Replace(' ', '_');
}
}
return GenericPost<IndividualUserFieldResponse>($"user_fields.json", new { user_field = userField });

}

public IndividualUserFieldResponse UpdateUserField(UserField userField, bool replaceNameSpacesWithUnderscore = true)
{
if (userField.CustomFieldOptions != null)
{
foreach (var field in userField.CustomFieldOptions)
{
if (replaceNameSpacesWithUnderscore)
{
field.Name = field.Name.Replace(' ', '_');
}
field.Value = field.Value.Replace(' ', '_');
}
}
return GenericPut<IndividualUserFieldResponse>($"user_fields/{userField.Id}.json", new { user_field = userField });
}

public bool DeleteUserField(long id)
{
return GenericDelete($"user_fields/{id}.json");
}
#endif

#if ASYNC
Expand Down Expand Up @@ -725,6 +784,53 @@ public Task<GroupSubscriptionsResponse> GetSubscriptionsAsync(long userId, Subsc
{
return GenericPagedGetAsync<GroupSubscriptionsResponse>($"help_center/users/{userId}/subscriptions.json".SubscriptionSideloadUri(subscriptionSideLoadOptions), perPage, page);
}

public async Task<GroupUserFieldResponse> GetUserFieldsAsync()
{
return await GenericGetAsync<GroupUserFieldResponse>("user_fields.json");
}

public async Task<IndividualUserFieldResponse> GetUserFieldByIdAsync(long id)
{
return await GenericGetAsync<IndividualUserFieldResponse>($"user_fields/{id}.json");
}
public async Task<IndividualUserFieldResponse> CreateUserFieldAsync(UserField userField, bool replaceNameSpacesWithUnderscore = true)
{
if (userField.CustomFieldOptions != null)
{
foreach (var field in userField.CustomFieldOptions)
{
if (replaceNameSpacesWithUnderscore)
{
field.Name = field.Name.Replace(' ', '_');
}
field.Value = field.Value.Replace(' ', '_');
}
}
return await GenericPostAsync<IndividualUserFieldResponse>($"user_fields.json", new { user_field = userField });

}

public async Task<IndividualUserFieldResponse> UpdateUserFieldAsync(UserField userField, bool replaceNameSpacesWithUnderscore = true)
{
if (userField.CustomFieldOptions != null)
{
foreach (var field in userField.CustomFieldOptions)
{
if (replaceNameSpacesWithUnderscore)
{
field.Name = field.Name.Replace(' ', '_');
}
field.Value = field.Value.Replace(' ', '_');
}
}
return await GenericPutAsync<IndividualUserFieldResponse>($"user_fields/{userField.Id}.json", new { user_field = userField });
}

public async Task<bool> DeleteUserFieldAsync(long id)
{
return await GenericDeleteAsync($"user_fields/{id}.json");
}
#endif
private string GetResourceStringWithSideLoadOptionsParam(string resource, UserSideLoadOptions sideLoadOptions)
{
Expand Down
Loading
Loading