Skip to content

Commit

Permalink
Added method getInvoicesForContact
Browse files Browse the repository at this point in the history
  • Loading branch information
benedwards44 committed Jul 2, 2016
1 parent 8928cfa commit a0a6fcf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
33 changes: 30 additions & 3 deletions src/classes/XeroAccountingApi.cls
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public with sharing class XeroAccountingApi {

/**
* @author Ben Edwards ([email protected])
* @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<XeroInvoice> getInvoices () {

List<XeroInvoice> xeroInvoices = new List<XeroInvoice>();

// Execute the callout to the Contacts resource
// Execute the callout to the Invoice resource
HttpResponse response = XeroCalloutUtility.executeCallout('GET', 'Invoices', null);

// If successful response
Expand All @@ -113,6 +113,33 @@ public with sharing class XeroAccountingApi {
return xeroInvoices;
}

/**
* @author Ben Edwards ([email protected])
* @description Method to return all invoices for a given Contact Id
* @return List of Xero Invoice wrapper classes
**/
public static List<XeroInvoice> getInvoicesForContact (String xeroContactId) {

List<XeroInvoice> xeroInvoices = new List<XeroInvoice>();

// 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 ([email protected])
* @description Method to create an invoice in Xero.
Expand Down
27 changes: 25 additions & 2 deletions src/classes/XeroAccountingApiTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public class XeroAccountingApiTest {

Test.stopTest();

system.debug('### ' + xeroInvoices);

// Assert that a contact exists
system.assertEquals(
1,
Expand Down Expand Up @@ -172,6 +170,31 @@ public class XeroAccountingApiTest {
Test.stopTest();
}

/**
* @author Ben Edwards ([email protected])
* @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<XeroInvoice> 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 ([email protected])
* @description Test a successful callout of the createInvoice() method
Expand Down
4 changes: 2 additions & 2 deletions src/classes/XeroCalloutResponseParser.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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<XeroContact>) JSON.deserializeStrict(jsonSerialized, List<XeroContact>.class);
return (List<XeroContact>) JSON.deserialize(jsonSerialized, List<XeroContact>.class);
}

/**
Expand All @@ -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<XeroInvoice>) JSON.deserializeStrict(jsonSerialized, List<XeroInvoice>.class);
return (List<XeroInvoice>) JSON.deserialize(jsonSerialized, List<XeroInvoice>.class);
}

/**
Expand Down

0 comments on commit a0a6fcf

Please sign in to comment.