Skip to content

Commit

Permalink
feat: deal get files endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed Jun 18, 2021
1 parent b23d116 commit 7e7c6d8
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 18 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ You can debug this library right from your application by configuring the [NuGet
- [x] getDealsTimeline
- [x] getDeal
- [x] getDealActivities
- [ ] getDealFiles
- [x] getDealFiles
- [x] getDealUpdates
- [x] getDealFollowers
- [ ] getDealMailMessages
Expand Down
59 changes: 59 additions & 0 deletions src/Pipedrive.net.Tests.Integration/Clients/DealsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,65 @@ public async Task ReturnsCorrectTimeline()
}
}

public class TheGetFilesMethod
{
[IntegrationTest]
public async Task ReturnsCorrectCountWithoutStart()
{
var pipedrive = Helper.GetAuthenticatedClient();

var options = new DealFileFilters
{
PageSize = 3,
PageCount = 1
};

var dealFiles = await pipedrive.Deal.GetFiles(1, options);
Assert.Equal(3, dealFiles.Count);
}

[IntegrationTest]
public async Task ReturnsCorrectCountWithStart()
{
var pipedrive = Helper.GetAuthenticatedClient();

var options = new DealFileFilters
{
PageSize = 2,
PageCount = 1,
StartPage = 1
};

var deals = await pipedrive.Deal.GetFiles(1, options);
Assert.Equal(2, deals.Count);
}

[IntegrationTest]
public async Task ReturnsDistinctInfosBasedOnStartPage()
{
var pipedrive = Helper.GetAuthenticatedClient();

var startOptions = new DealFileFilters
{
PageSize = 1,
PageCount = 1
};

var firstPage = await pipedrive.Deal.GetFiles(1, startOptions);

var skipStartOptions = new DealFileFilters
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};

var secondPage = await pipedrive.Deal.GetFiles(1, skipStartOptions);

Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
}

public class TheGetUpdatesMethod
{
[IntegrationTest]
Expand Down
47 changes: 42 additions & 5 deletions src/Pipedrive.net.Tests/Clients/DealsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,50 @@ public async Task RequestsCorrectUrl()
{
await connection.Get<IReadOnlyList<DealTimeline>>(
Arg.Is<Uri>(u => u.ToString() == "deals/timeline"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 5
Arg.Is<Dictionary<string, string>>(d => d.Count == 4
&& d["start_date"] == "2021-01-01"
&& d["interval"] == "month"
&& d["amount"] == "1"
&& d["field_key"] == "close_time"
&& d["exclude_deals"] == "0"));
&& d["field_key"] == "close_time"));
});
}
}

public class TheGetFilesMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new DealsClient(Substitute.For<IApiConnection>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetFiles(1, null));
}

[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new DealsClient(connection);

var filters = new DealFileFilters
{
PageSize = 1,
PageCount = 1,
StartPage = 0,
IncludeDeletedFiles = true,
};

await client.GetFiles(123, filters);

Received.InOrder(async () =>
{
await connection.GetAll<File>(
Arg.Is<Uri>(u => u.ToString() == "deals/123/files"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
&& d["include_deleted_files"] == "1"),
Arg.Is<ApiOptions>(o => o.PageSize == 1
&& o.PageCount == 1
&& o.StartPage == 0));
});
}
}
Expand Down Expand Up @@ -404,8 +442,7 @@ public async Task RequestsCorrectUrl()
{
await connection.GetAll<EntityUpdateFlow>(
Arg.Is<Uri>(u => u.ToString() == "deals/123/flow"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
&& d["id"] == "123"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 0),
Arg.Is<ApiOptions>(o => o.PageSize == 1
&& o.PageCount == 1
&& o.StartPage == 0));
Expand Down
16 changes: 15 additions & 1 deletion src/Pipedrive.net/Clients/DealsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,26 @@ public Task<IReadOnlyList<DealTimeline>> GetTimeline(DealsTimelineFilters filter
return ApiConnection.Get<IReadOnlyList<DealTimeline>>(ApiUrls.DealsTimeline(), parameters);
}

public Task<IReadOnlyList<File>> GetFiles(long dealId, DealFileFilters filters)
{
Ensure.ArgumentNotNull(filters, nameof(filters));

var parameters = filters.Parameters;
var options = new ApiOptions
{
StartPage = filters.StartPage,
PageCount = filters.PageCount,
PageSize = filters.PageSize
};

return ApiConnection.GetAll<File>(ApiUrls.DealFiles(dealId), parameters, options);
}

public Task<IReadOnlyList<EntityUpdateFlow>> GetUpdates(long dealId, DealUpdateFilters filters)
{
Ensure.ArgumentNotNull(filters, nameof(filters));

var parameters = filters.Parameters;
parameters.Add("id", dealId.ToString());
var options = new ApiOptions
{
StartPage = filters.StartPage,
Expand Down
2 changes: 2 additions & 0 deletions src/Pipedrive.net/Clients/IDealsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface IDealsClient

Task<IReadOnlyList<DealTimeline>> GetTimeline(DealsTimelineFilters filters);

Task<IReadOnlyList<File>> GetFiles(long dealId, DealFileFilters filters);

Task<IReadOnlyList<EntityUpdateFlow>> GetUpdates(long dealId, DealUpdateFilters filters);

Task<IReadOnlyList<DealFollower>> GetFollowers(long dealId);
Expand Down
9 changes: 9 additions & 0 deletions src/Pipedrive.net/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ public static Uri Deal(long id)
return new Uri($"deals/{id}", UriKind.Relative);
}

/// <summary>
/// Returns the <see cref="Uri"/> for all the files of the specified deal.
/// </summary>
/// <param name="id">The id of the deal</param>
public static Uri DealFiles(long id)
{
return new Uri($"deals/{id}/files", UriKind.Relative);
}

/// <summary>
/// Returns the <see cref="Uri"/> for all the updates of the specified deal.
/// </summary>
Expand Down
8 changes: 0 additions & 8 deletions src/Pipedrive.net/Models/Common/ExcludeDeals.cs

This file was deleted.

44 changes: 44 additions & 0 deletions src/Pipedrive.net/Models/Request/Deals/DealFileFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;

namespace Pipedrive
{
public class DealFileFilters
{
public static DealFileFilters None
{
get { return new DealFileFilters(); }
}

public int? StartPage { get; set; }

public int? PageCount { get; set; }

public int? PageSize { get; set; }

public bool? IncludeDeletedFiles { get; set; }

public string Sort { get; set; }

/// <summary>
/// Get the query parameters that will be appending onto the search
/// </summary>
public IDictionary<string, string> Parameters
{
get
{
var d = new Dictionary<string, string>();
if (IncludeDeletedFiles.HasValue)
{
d.Add("include_deleted_files", IncludeDeletedFiles.Value == true ? 1.ToString() : 0.ToString());
}

if (!string.IsNullOrWhiteSpace(Sort))
{
d.Add("sort", Sort);
}

return d;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DealsTimelineFilters
public long? FilterId { get; set; }

[JsonProperty("exclude_deals")]
public ExcludeDeals ExcludeDeals { get; set; }
public bool? ExcludeDeals { get; set; }

[JsonProperty("totals_convert_currency")]
public string TotalsConvertCurrency { get; set; }
Expand Down Expand Up @@ -60,7 +60,10 @@ public IDictionary<string, string> Parameters
d.Add("user_id", UserId.Value.ToString());
}

d.Add("exclude_deals", ((long)ExcludeDeals).ToString());
if (ExcludeDeals.HasValue)
{
d.Add("exclude_deals", ExcludeDeals.Value == true ? 1.ToString() : 0.ToString());
}

if (!string.IsNullOrWhiteSpace(TotalsConvertCurrency))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Pipedrive.Models.Common;

namespace Pipedrive
{
Expand Down

0 comments on commit 7e7c6d8

Please sign in to comment.