diff --git a/src/classes/XeroAccountingApi.cls b/src/classes/XeroAccountingApi.cls index 8fd9775..99aa892 100644 --- a/src/classes/XeroAccountingApi.cls +++ b/src/classes/XeroAccountingApi.cls @@ -88,14 +88,14 @@ public with sharing class XeroAccountingApi { /** * @author Ben Edwards (ben@benedwards.co.nz) - * @description Method to return ALL contacts for your Xero environment - * @return List of Xero Contact wrapper classes + * @description Method to return ALL invoices for your Xero environment + * @return List of Xero Invoice wrapper classes **/ public static List getInvoices () { List xeroInvoices = new List(); - // Execute the callout to the Contacts resource + // Execute the callout to the Invoice resource HttpResponse response = XeroCalloutUtility.executeCallout('GET', 'Invoices', null); // If successful response @@ -113,6 +113,33 @@ public with sharing class XeroAccountingApi { return xeroInvoices; } + /** + * @author Ben Edwards (ben@benedwards.co.nz) + * @description Method to return all invoices for a given Contact Id + * @return List of Xero Invoice wrapper classes + **/ + public static List getInvoicesForContact (String xeroContactId) { + + List xeroInvoices = new List(); + + // Execute the callout to the Invoice resource with the Contact ID filter + HttpResponse response = XeroCalloutUtility.executeCallout('GET', 'Invoices?where=Contact.ContactID%20%3D%20Guid%28%22' + xeroContactId + '%22%29', null); + + // If successful response + if (response.getStatusCode() == 200) { + + // Use the parser to convert the response into Xero objects + xeroInvoices = XeroCalloutResponseParser.parseInvoices(response.getBody()); + } + else { + + // Raise error + throw new XeroAccountingApiException(response.getStatusCode() + ': ' + response.getBody()); + } + + return xeroInvoices; + } + /** * @author Ben Edwards (ben@benedwards.co.nz) * @description Method to create an invoice in Xero. diff --git a/src/classes/XeroAccountingApiTest.cls b/src/classes/XeroAccountingApiTest.cls index e6427c8..0fd2328 100644 --- a/src/classes/XeroAccountingApiTest.cls +++ b/src/classes/XeroAccountingApiTest.cls @@ -132,8 +132,6 @@ public class XeroAccountingApiTest { Test.stopTest(); - system.debug('### ' + xeroInvoices); - // Assert that a contact exists system.assertEquals( 1, @@ -172,6 +170,31 @@ public class XeroAccountingApiTest { Test.stopTest(); } + /** + * @author Ben Edwards (ben@benedwards.co.nz) + * @description Test a successful callout of the getInvoicesForContact() method + **/ + @isTest + static void getInvoicesForContactSuccess() { + + // Set the Mock Class for the callout + Test.setMock(HttpCalloutMock.class, getStaticMock(200, 'XeroInvoicesMock')); + + Test.startTest(); + + // Execute the callout + List xeroInvoices = XeroAccountingApi.getInvoicesForContact('ABC123'); + + Test.stopTest(); + + // Assert that a contact exists + system.assertEquals( + 1, + xeroInvoices.size(), + 'There should be one invoice returned from the callout' + ); + } + /** * @author Ben Edwards (ben@benedwards.co.nz) * @description Test a successful callout of the createInvoice() method diff --git a/src/classes/XeroCalloutResponseParser.cls b/src/classes/XeroCalloutResponseParser.cls index 6cb567c..80aa7d4 100644 --- a/src/classes/XeroCalloutResponseParser.cls +++ b/src/classes/XeroCalloutResponseParser.cls @@ -15,7 +15,7 @@ public with sharing class XeroCalloutResponseParser { String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Contacts'); // And finally parse the now re-serialized contact list back into Contact objects - return (List) JSON.deserializeStrict(jsonSerialized, List.class); + return (List) JSON.deserialize(jsonSerialized, List.class); } /** @@ -29,7 +29,7 @@ public with sharing class XeroCalloutResponseParser { String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Invoices'); // And finally parse the now re-serialized contact list back into Contact objects - return (List) JSON.deserializeStrict(jsonSerialized, List.class); + return (List) JSON.deserialize(jsonSerialized, List.class); } /**