Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit 99bc60c

Browse files
authored
Merge pull request #326 from MJMortimer/history-and-notes
History and notes
2 parents 6cafde2 + af435d5 commit 99bc60c

13 files changed

Lines changed: 211 additions & 1 deletion

CoreTests/CoreTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
<Compile Include="Integration\Files\Support\File.cs" />
111111
<Compile Include="Integration\Files\Support\FilesTest.cs" />
112112
<Compile Include="Integration\Files\Files\AddFileTest.cs" />
113+
<Compile Include="Integration\HistoryAndNotes\CreateNotes.cs" />
114+
<Compile Include="Integration\HistoryAndNotes\Find.cs" />
115+
<Compile Include="Integration\HistoryAndNotes\HistoryAndNotesTest.cs" />
113116
<Compile Include="Integration\Invoices\Find.cs" />
114117
<Compile Include="Integration\Invoices\InvoicesTest.cs" />
115118
<Compile Include="Integration\Invoices\OnlineInvoiceUrl.cs" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
3+
namespace CoreTests.Integration.HistoryAndNotes
4+
{
5+
public class CreateNotes : HistoryAndNotesTest
6+
{
7+
[Test]
8+
public void Can_create_notes()
9+
{
10+
const string details = "Details";
11+
12+
Given_a_contact();
13+
14+
Given_a_note_with_these_details(details);
15+
16+
When_I_retrieve_history_and_notes_for_the_contact();
17+
18+
Then_there_is_a_note_with_the_correct_details(details);
19+
}
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NUnit.Framework;
2+
3+
namespace CoreTests.Integration.HistoryAndNotes
4+
{
5+
public class Find : HistoryAndNotesTest
6+
{
7+
[Test]
8+
public void Can_fetch_history_and_notes()
9+
{
10+
Given_a_contact();
11+
12+
When_I_retrieve_history_and_notes_for_the_contact();
13+
14+
Then_there_are_some_history_records();
15+
}
16+
}
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using Xero.Api.Core.Model;
6+
using Xero.Api.Core.Model.Types;
7+
8+
namespace CoreTests.Integration.HistoryAndNotes
9+
{
10+
public class HistoryAndNotesTest : ApiWrapperTest
11+
{
12+
private Contact _contact;
13+
private IEnumerable<HistoryRecord> _historyRecords;
14+
15+
protected void Given_a_contact()
16+
{
17+
_contact = Api.Contacts.Create(new Contact { Name = Guid.NewGuid().ToString() });
18+
}
19+
20+
protected void Given_a_note_with_these_details(string details)
21+
{
22+
Api.HistoryAndNotes.CreateNote(HistoryAndNotesEndpointCreateType.Contacts, _contact.Id,
23+
new HistoryRecord
24+
{
25+
Details = details
26+
});
27+
}
28+
29+
protected void When_I_retrieve_history_and_notes_for_the_contact()
30+
{
31+
_historyRecords = Api.HistoryAndNotes.Find(HistoryAndNotesEndpointRetrieveType.Contacts, _contact.Id);
32+
}
33+
34+
protected void Then_there_are_some_history_records()
35+
{
36+
Assert.True(_historyRecords.Any(), "Expected some history records to be returned, but there were none");
37+
}
38+
39+
protected void Then_there_is_a_note_with_the_correct_details(string details)
40+
{
41+
Assert.True(_historyRecords.Any(it => it.Details == details), "Expected a note with the expected details to be returned but it was not");
42+
}
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using Xero.Api.Core.Model;
6+
using Xero.Api.Core.Model.Types;
7+
using Xero.Api.Core.Request;
8+
using Xero.Api.Core.Response;
9+
using Xero.Api.Infrastructure.Http;
10+
11+
namespace Xero.Api.Core.Endpoints
12+
{
13+
public interface IHistoryAndNotesEndpoint
14+
{
15+
IEnumerable<HistoryRecord> Find(HistoryAndNotesEndpointRetrieveType type, Guid parent);
16+
HistoryRecord CreateNote(HistoryAndNotesEndpointCreateType type, Guid parent, HistoryRecord note);
17+
}
18+
19+
public class HistoryAndNotesEndpoint : IHistoryAndNotesEndpoint
20+
{
21+
private XeroHttpClient Client { get; set; }
22+
23+
public HistoryAndNotesEndpoint(XeroHttpClient client)
24+
{
25+
Client = client;
26+
}
27+
28+
public IEnumerable<HistoryRecord> Find(HistoryAndNotesEndpointRetrieveType type, Guid parent)
29+
{
30+
return Client.Get<HistoryRecord, HistoryRecordsResponse>(string.Format("api.xro/2.0/{0}/{1:D}/history", type, parent));
31+
}
32+
33+
public HistoryRecord CreateNote(HistoryAndNotesEndpointCreateType type, Guid parent, HistoryRecord note)
34+
{
35+
var request = new HistoryRecordsRequest {note};
36+
37+
return Client.Put<HistoryRecord, HistoryRecordsResponse>(string.Format("api.xro/2.0/{0}/{1:D}/history", type, parent), request).FirstOrDefault();
38+
}
39+
}
40+
}

Xero.Api/Core/IXeroCoreApi.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface IXeroCoreApi
2222
IExpenseClaimsEndpoint ExpenseClaims { get; }
2323
IFilesEndpoint Files { get; }
2424
IFoldersEndpoint Folders { get; }
25+
IHistoryAndNotesEndpoint HistoryAndNotes { get; }
2526
IInboxEndpoint Inbox { get; }
2627
IAssociationsEndpoint Associations { get; }
2728
IInvoicesEndpoint Invoices { get; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace Xero.Api.Core.Model
5+
{
6+
[DataContract(Namespace = "")]
7+
public sealed class HistoryRecord
8+
{
9+
[DataMember]
10+
public string Changes { get; set; }
11+
12+
[DataMember(Name = "DateUTC")]
13+
public DateTime DateUtc { get; set; }
14+
15+
[DataMember]
16+
public string User { get; set; }
17+
18+
[DataMember]
19+
public string Details { get; set; }
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Xero.Api.Core.Model.Types
2+
{
3+
public enum HistoryAndNotesEndpointCreateType
4+
{
5+
BankTransactions,
6+
Contacts,
7+
CreditNotes,
8+
Invoices,
9+
PurchaseOrders
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Xero.Api.Core.Model.Types
2+
{
3+
public enum HistoryAndNotesEndpointRetrieveType
4+
{
5+
BankTransactions,
6+
BankTransfers,
7+
Contacts,
8+
CreditNotes,
9+
ExpenseClaims,
10+
Invoices,
11+
Items,
12+
ManualJournals,
13+
Payments,
14+
PurchaseOrders,
15+
Receipts,
16+
RepeatingInvoices
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Runtime.Serialization;
2+
using Xero.Api.Common;
3+
using Xero.Api.Core.Model;
4+
5+
namespace Xero.Api.Core.Request
6+
{
7+
[CollectionDataContract(Namespace = "", Name = "HistoryRecords")]
8+
public class HistoryRecordsRequest : XeroRequest<HistoryRecord>
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)