From 62278a3a14625fc50c09aa40528f2da25d482558 Mon Sep 17 00:00:00 2001 From: Elizabeth Schneider Date: Wed, 30 Nov 2016 04:14:56 -0700 Subject: [PATCH] Copied code from sync version of the create and Update methods for TicketField to the async (#257) --- src/Tests/TicketTests.cs | 112 ++++++++++++++++++- src/ZendeskApi_v2/Requests/Tickets.cs | 153 ++++++++++++++++++++++---- 2 files changed, 240 insertions(+), 25 deletions(-) diff --git a/src/Tests/TicketTests.cs b/src/Tests/TicketTests.cs index 46470a6f..11b60789 100644 --- a/src/Tests/TicketTests.cs +++ b/src/Tests/TicketTests.cs @@ -24,6 +24,19 @@ public class TicketTests ZendeskApi api = new ZendeskApi(Settings.Site, Settings.Email, Settings.Password); TicketSideLoadOptionsEnum ticketSideLoadOptions = TicketSideLoadOptionsEnum.Users | TicketSideLoadOptionsEnum.Organizations | TicketSideLoadOptionsEnum.Groups; + [OneTimeTearDown] + public async Task TestCleanUp() + { + var response = await api.Tickets.GetTicketFieldsAsync(); + foreach (var item in response.TicketFields) + { + if (item.Title == "My Tagger 2") + { + await api.Tickets.DeleteTicketFieldAsync(item.Id.Value); + } + } + } + [Test] public void CanGetTicketsAsync() { @@ -331,7 +344,7 @@ public void BooleanCustomFieldValuesArePreservedOnUpdate() }; var res = api.Tickets.CreateTicket(ticket).Ticket; - Assert.AreEqual(ticket.CustomFields[1].Value, res.CustomFields[1].Value); + Assert.AreEqual(ticket.CustomFields[1].Value, res.CustomFields.Where(f => f.Id == Settings.CustomBoolFieldId).FirstOrDefault().Value); //var updateResponse = api.Tickets.UpdateTicket(res, new Comment() { Body = "Just trying to update it!", Public = true}); //res.UpdatedAt = null; @@ -1134,5 +1147,102 @@ public void TicketGroupsAndTags() Assert.That(groups.Count(), Is.GreaterThan(0)); Assert.That(tagCount, Is.GreaterThan(0)); } + + [Test] + public async Task TicketField() + { + var tField = new TicketField + { + Type = TicketFieldTypes.Tagger, + Title = "My Tagger 2", + Description = "test description", + TitleInPortal = "Test Tagger", + CustomFieldOptions = new List + { + new CustomFieldOptions + { + Name = "test entryA", + Value = "Test2" + }, + new CustomFieldOptions + { + Name = "test entryB", + Value = "test3" + } + } + }; + + var res = api.Tickets.CreateTicketField(tField); + Assert.That(res.TicketField, Is.Not.Null); + Assert.That(res.TicketField.Id, Is.Not.Null); + Assert.That(res.TicketField.CustomFieldOptions.Count, Is.EqualTo(2)); + } + + [Test] + public async Task CanCreateUpdateOptionsAndDeleteTaggerTicketFieldAsync() + { + string option1 = "test_value_a"; + string option1_Update = "test_value_a_newtag"; + string option2 = "test_value_b"; + string option3 = "test_value_c"; + + var tField = new TicketField() + { + Type = TicketFieldTypes.Tagger, + Title = "My Tagger 2", + Description = "test description", + TitleInPortal = "Test Tagger", + CustomFieldOptions = new List() + }; + + tField.CustomFieldOptions.Add(new CustomFieldOptions() + { + Name = "test entryA", + Value = option1 + }); + + tField.CustomFieldOptions.Add(new CustomFieldOptions() + { + Name = "test entryB", + Value = option2 + }); + + var res = await api.Tickets.CreateTicketFieldAsync(tField); + Assert.That(res.TicketField, Is.Not.Null); + Assert.That(res.TicketField.Id, Is.Not.Null); + Assert.That(res.TicketField.CustomFieldOptions.Count, Is.EqualTo(2)); + Assert.That(res.TicketField.CustomFieldOptions[0].Value, Is.EqualTo(option1)); + Assert.That(res.TicketField.CustomFieldOptions[1].Value, Is.EqualTo(option2)); + + long id = res.TicketField.Id.Value; + + var tFieldU = new TicketField() + { + Id = id, + CustomFieldOptions = new List() + }; + + //update CustomFieldOption A + tFieldU.CustomFieldOptions.Add(new CustomFieldOptions() + { + Name = "test entryA newTitle", + Value = option1_Update + }); + //delete CustomFieldOption B + //add CustomFieldOption C + tFieldU.CustomFieldOptions.Add(new CustomFieldOptions() + { + Name = "test entryC", + Value = option3 + }); + + var resU = await api.Tickets.UpdateTicketFieldAsync(tFieldU); + + Assert.That(resU.TicketField.CustomFieldOptions.Count, Is.EqualTo(2)); + Assert.That(resU.TicketField.CustomFieldOptions[0].Value, Is.EqualTo(option1_Update)); + Assert.That(resU.TicketField.CustomFieldOptions[1].Value, Is.Not.EqualTo(option2)); + + Assert.True(await api.Tickets.DeleteTicketFieldAsync(id)); + } } } diff --git a/src/ZendeskApi_v2/Requests/Tickets.cs b/src/ZendeskApi_v2/Requests/Tickets.cs index 3394de86..c2d652e8 100644 --- a/src/ZendeskApi_v2/Requests/Tickets.cs +++ b/src/ZendeskApi_v2/Requests/Tickets.cs @@ -1,9 +1,13 @@ using System; using System.Collections.Generic; using System.Net; + #if ASYNC + using System.Threading.Tasks; + #endif + using ZendeskApi_v2.Extensions; using ZendeskApi_v2.Models.Requests; using ZendeskApi_v2.Models.Shared; @@ -29,26 +33,44 @@ public enum TicketSideLoadOptionsEnum public interface ITickets : ICore { - #if SYNC + GroupTicketFormResponse GetTicketForms(); + IndividualTicketFormResponse CreateTicketForm(TicketForm ticketForm); + IndividualTicketFormResponse GetTicketFormById(long id); + IndividualTicketFormResponse UpdateTicketForm(TicketForm ticketForm); + bool ReorderTicketForms(long[] orderedTicketFormIds); + IndividualTicketFormResponse CloneTicketForm(long ticketFormId); + bool DeleteTicketForm(long id); + GroupTicketResponse GetAllTickets(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetTicketsByViewID(long viewId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetTicketsByOrganizationID(long id, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetTicketsByOrganizationID(long id, int pageNumber, int itemsPerPage, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetRecentTickets(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetTicketsByUserID(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); - GroupTicketResponse GetAssignedTicketsByUserID( long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None ); + + GroupTicketResponse GetAssignedTicketsByUserID(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetTicketsWhereUserIsCopied(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + IndividualTicketResponse GetTicket(long id, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupCommentResponse GetTicketComments(long ticketId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketResponse GetMultipleTickets(IEnumerable ids, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + IndividualTicketResponse CreateTicket(Ticket ticket); /// @@ -60,19 +82,32 @@ public interface ITickets : ICore IndividualTicketResponse UpdateTicket(Ticket ticket, Comment comment = null); JobStatusResponse BulkUpdate(IEnumerable ids, BulkUpdate info); + bool Delete(long id); + bool DeleteMultiple(IEnumerable ids); + GroupUserResponse GetCollaborators(long id); + GroupTicketResponse GetIncidents(long id); + GroupTicketResponse GetProblems(); + GroupTicketResponse AutoCompleteProblems(string text); + GroupAuditResponse GetAudits(long ticketId); + GroupAuditResponse GetAuditsNextPage(string NextPage); + IndividualAuditResponse GetAuditById(long ticketId, long auditId); + bool MarkAuditAsTrusted(long ticketId, long auditId); + [Obsolete("This has been deprecated. Please use GetIncrementalTicketExport", true)] GroupTicketExportResponse GetInrementalTicketExport(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketExportResponse GetIncrementalTicketExport(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketExportResponse GetIncrementalTicketExportNextPage(string nextPage); /// @@ -83,36 +118,63 @@ public interface ITickets : ICore /// [Obsolete("This has been deprecated. Please use __TestOnly__GetIncrementalTicketExport", true)] GroupTicketExportResponse __TestOnly__GetInrementalTicketExport(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + GroupTicketExportResponse __TestOnly__GetIncrementalTicketExport(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); GroupTicketFieldResponse GetTicketFields(); + IndividualTicketFieldResponse GetTicketFieldById(long id); + IndividualTicketFieldResponse CreateTicketField(TicketField ticketField); + IndividualTicketFieldResponse UpdateTicketField(TicketField ticketField); + bool DeleteTicketField(long id); + GroupSuspendedTicketResponse GetSuspendedTickets(); + IndividualSuspendedTicketResponse GetSuspendedTicketById(long id); + bool RecoverSuspendedTicket(long id); + bool RecoverManySuspendedTickets(IEnumerable ids); + bool DeleteSuspendedTickets(long id); + bool DeleteManySuspendedTickets(IEnumerable ids); + GroupTicketMetricResponse GetAllTicketMetrics(); + IndividualTicketMetricResponse GetTicketMetricsForTicket(long ticket_id); + IndividualTicketResponse ImportTicket(TicketImport ticket); + JobStatusResponse BulkImportTickets(IEnumerable tickets); + #endif #if ASYNC + Task GetAllTicketsAsync(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketsByViewIDAsync(long viewId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketsByOrganizationIDAsync(long id, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetRecentTicketsAsync(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketsByUserIDAsync(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); - Task GetAssignedTicketsByUserIDAsync( long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None ); + + Task GetAssignedTicketsByUserIDAsync(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketsWhereUserIsCopiedAsync(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketAsync(long id, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetTicketCommentsAsync(long ticketId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task GetMultipleTicketsAsync(IEnumerable ids, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); + Task CreateTicketAsync(Ticket ticket); /// @@ -124,15 +186,25 @@ public interface ITickets : ICore Task UpdateTicketAsync(Ticket ticket, Comment comment = null); Task BulkUpdateAsync(IEnumerable ids, BulkUpdate info); + Task DeleteAsync(long id); + Task DeleteMultipleAsync(IEnumerable ids); + Task GetCollaboratorsAsync(long id); + Task GetIncidentsAsync(long id); + Task GetProblemsAsync(); + Task AutoCompleteProblemsAsync(string text); + Task GetAuditsAsync(long ticketId); + Task GetAuditByIdAsync(long ticketId, long auditId); + Task MarkAuditAsTrustedAsync(long ticketId, long auditId); + Task GetInrementalTicketExportAsync(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); /// @@ -144,27 +216,49 @@ public interface ITickets : ICore Task __TestOnly__GetInrementalTicketExportAsync(DateTimeOffset startTime, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None); Task GetTicketFieldsAsync(); + Task GetTicketFieldByIdAsync(long id); + Task CreateTicketFieldAsync(TicketField ticketField); + Task UpdateTicketFieldAsync(TicketField ticketField); + Task DeleteTicketFieldAsync(long id); + Task GetSuspendedTicketsAsync(); + Task GetSuspendedTicketByIdAsync(long id); + Task RecoverSuspendedTicketAsync(long id); + Task RecoverManySuspendedTicketsAsync(IEnumerable ids); + Task DeleteSuspendedTicketsAsync(long id); + Task DeleteManySuspendedTicketsAsync(IEnumerable ids); + Task GetTicketFormsAsync(); + Task CreateTicketFormAsync(TicketForm ticketForm); + Task GetTicketFormByIdAsync(long id); + Task UpdateTicketFormAsync(TicketForm ticketForm); + Task ReorderTicketFormsAsync(long[] orderedTicketFormIds); + Task CloneTicketFormAsync(long ticketFormId); + Task DeleteTicketFormAsync(long id); + Task GetAllTicketMetricsAsync(); + Task GetTicketMetricsForTicketAsync(long ticket_id); + Task ImportTicketAsync(TicketImport ticket); + Task BulkImportTicketsAsync(IEnumerable tickets); + #endif } @@ -208,7 +302,6 @@ public IndividualTicketFormResponse UpdateTicketForm(TicketForm ticketForm) return GenericPut(string.Format("{0}/{1}.json", _ticket_forms, ticketForm.Id), body); } - public bool ReorderTicketForms(long[] orderedTicketFormIds) { var body = new { ticket_form_ids = orderedTicketFormIds }; @@ -225,8 +318,6 @@ public bool DeleteTicketForm(long id) return GenericDelete(string.Format("{0}/{1}.json", _ticket_forms, id)); } - - public GroupTicketResponse GetAllTickets(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) { string resource = GetResourceStringWithSideLoadOptionsParam(_tickets + ".json", sideLoadOptions); @@ -262,10 +353,11 @@ public GroupTicketResponse GetTicketsByUserID(long userId, int? perPage = null, string resource = GetResourceStringWithSideLoadOptionsParam(string.Format("users/{0}/tickets/requested.json", userId), sideLoadOptions); return GenericPagedGet(resource, perPage, page); } - public GroupTicketResponse GetAssignedTicketsByUserID( long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None ) + + public GroupTicketResponse GetAssignedTicketsByUserID(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) { - string resource = GetResourceStringWithSideLoadOptionsParam( string.Format( "users/{0}/tickets/assigned.json", userId ), sideLoadOptions ); - return GenericPagedGet( resource, perPage, page ); + string resource = GetResourceStringWithSideLoadOptionsParam(string.Format("users/{0}/tickets/assigned.json", userId), sideLoadOptions); + return GenericPagedGet(resource, perPage, page); } public GroupTicketResponse GetTicketsWhereUserIsCopied(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) @@ -292,7 +384,6 @@ public GroupTicketResponse GetMultipleTickets(IEnumerable ids, TicketSideL return GenericGet(resource); } - public IndividualTicketResponse CreateTicket(Ticket ticket) { var body = new { ticket }; @@ -376,12 +467,14 @@ public GroupAuditResponse GetAudits(long ticketId) { return GenericGet(string.Format("tickets/{0}/audits.json", ticketId)); } + public GroupAuditResponse GetAuditsNextPage(string NextPage) { var resource = NextPage.Replace(ZendeskUrl, string.Empty); return GenericGet(resource); } + public IndividualAuditResponse GetAuditById(long ticketId, long auditId) { return GenericGet(string.Format("tickets/{0}/audits/{1}.json", ticketId, auditId)); @@ -538,6 +631,7 @@ public bool DeleteManySuspendedTickets(IEnumerable ids) } #region TicketMetrics + public GroupTicketMetricResponse GetAllTicketMetrics() { return GenericGet(_ticket_metrics + ".json"); @@ -548,11 +642,12 @@ public IndividualTicketMetricResponse GetTicketMetricsForTicket(long ticket_id) return GenericGet(string.Format("{0}/{1}/metrics.json", _tickets, ticket_id)); } - #endregion -#endif + #endregion TicketMetrics +#endif #if ASYNC + public async Task GetAllTicketsAsync(int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) { string resource = GetResourceStringWithSideLoadOptionsParam(_tickets + ".json", sideLoadOptions); @@ -583,11 +678,12 @@ public async Task GetTicketsByUserIDAsync(long userId, int? return await GenericPagedGetAsync(resource, perPage, page); } - public async Task GetAssignedTicketsByUserIDAsync( long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None ) + public async Task GetAssignedTicketsByUserIDAsync(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) { - string resource = GetResourceStringWithSideLoadOptionsParam( string.Format( "users/{0}/tickets/assigned.json", userId ), sideLoadOptions ); - return await GenericPagedGetAsync( resource, perPage, page ); + string resource = GetResourceStringWithSideLoadOptionsParam(string.Format("users/{0}/tickets/assigned.json", userId), sideLoadOptions); + return await GenericPagedGetAsync(resource, perPage, page); } + public async Task GetTicketsWhereUserIsCopiedAsync(long userId, int? perPage = null, int? page = null, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None) { string resource = GetResourceStringWithSideLoadOptionsParam(string.Format("users/{0}/tickets/ccd.json", userId), sideLoadOptions); @@ -638,7 +734,6 @@ public async Task BulkImportTicketsAsync(IEnumerable(_imports + "/" + _tickets + "/" + _create_many + ".json", body); - } /// @@ -737,13 +832,18 @@ public async Task GetTicketFieldByIdAsync(long id public async Task CreateTicketFieldAsync(TicketField ticketField) { - var body = new + if (ticketField.CustomFieldOptions != null) { - ticket_field = new + foreach (var custom in ticketField.CustomFieldOptions) { - type = ticketField.Type, - title = ticketField.Title + custom.Name = custom.Name.Replace(' ', '_'); + custom.Value = custom.Value.Replace(' ', '_'); } + } + + var body = new + { + ticket_field = ticketField }; var res = GenericPostAsync("ticket_fields.json", body); @@ -752,7 +852,12 @@ public async Task CreateTicketFieldAsync(TicketFi public async Task UpdateTicketFieldAsync(TicketField ticketField) { - return await GenericPutAsync(string.Format("ticket_fields/{0}.json", ticketField.Id), ticketField); + var body = new + { + ticket_field = ticketField + }; + + return await GenericPutAsync(string.Format("ticket_fields/{0}.json", ticketField.Id), body); } public async Task DeleteTicketFieldAsync(long id) @@ -815,7 +920,6 @@ public async Task UpdateTicketFormAsync(TicketForm return await GenericPutAsync(string.Format("{0}/{1}.json", _ticket_forms, ticketForm.Id), ticketForm); } - public async Task ReorderTicketFormsAsync(long[] orderedTicketFormIds) { var body = new { ticket_form_ids = orderedTicketFormIds }; @@ -833,6 +937,7 @@ public async Task DeleteTicketFormAsync(long id) } #region TicketMetrics + public Task GetAllTicketMetricsAsync() { return GenericGetAsync(_ticket_metrics + ".json"); @@ -843,8 +948,8 @@ public Task GetTicketMetricsForTicketAsync(long return GenericGetAsync(string.Format("{0}/{1}/metrics.json", _tickets, ticket_id)); } + #endregion TicketMetrics - #endregion #endif private string GetResourceStringWithSideLoadOptionsParam(string resource, TicketSideLoadOptionsEnum sideLoadOptions) @@ -862,4 +967,4 @@ private string GetResourceStringWithSideLoadOptionsParam(string resource, Ticket return resource; } } -} +} \ No newline at end of file