Skip to content

Commit fd392e8

Browse files
committed
feat: add get/add/delete follower for the Organization API
1 parent 1b3e155 commit fd392e8

8 files changed

Lines changed: 148 additions & 4 deletions

File tree

src/Pipedrive.net.Tests.Integration/Clients/DealsClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public async Task ReturnsCorrectCount()
269269
{
270270
var pipedrive = Helper.GetAuthenticatedClient();
271271

272-
var dealFollowers = await pipedrive.Deal.GetFollowers(1);
273-
Assert.Equal(1, dealFollowers.Count);
272+
var followers = await pipedrive.Deal.GetFollowers(1);
273+
Assert.Equal(1, followers.Count);
274274
}
275275
}
276276

src/Pipedrive.net.Tests.Integration/Clients/OrganizationsClientTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,42 @@ public async Task ReturnsDistinctInfosBasedOnStartPage()
277277
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
278278
}
279279
}
280+
281+
public class TheGetFollowersMethod
282+
{
283+
[IntegrationTest]
284+
public async Task ReturnsCorrectCount()
285+
{
286+
var pipedrive = Helper.GetAuthenticatedClient();
287+
288+
var followers = await pipedrive.Organization.GetFollowers(1);
289+
Assert.Equal(1, followers.Count);
290+
}
291+
}
292+
293+
public class TheAddFollowerMethod
294+
{
295+
[IntegrationTest]
296+
public async Task CanAddFollower()
297+
{
298+
var pipedrive = Helper.GetAuthenticatedClient();
299+
var fixture = pipedrive.Organization;
300+
301+
var addFollower = await fixture.AddFollower(1, 595707);
302+
Assert.NotNull(addFollower);
303+
}
304+
}
305+
306+
public class TheDeleteFollowerMethod
307+
{
308+
[IntegrationTest]
309+
public async Task CanDeleteFollower()
310+
{
311+
var pipedrive = Helper.GetAuthenticatedClient();
312+
var fixture = pipedrive.Organization;
313+
314+
await fixture.DeleteFollower(1, 461);
315+
}
316+
}
280317
}
281318
}

src/Pipedrive.net.Tests.Integration/Clients/PersonsClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ public async Task ReturnsCorrectCount()
245245
{
246246
var pipedrive = Helper.GetAuthenticatedClient();
247247

248-
var dealFollowers = await pipedrive.Person.GetFollowers(1);
249-
Assert.Equal(1, dealFollowers.Count);
248+
var followers = await pipedrive.Person.GetFollowers(1);
249+
Assert.Equal(1, followers.Count);
250250
}
251251
}
252252

src/Pipedrive.net.Tests/Clients/OrganizationsClientTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,54 @@ await connection.GetAll<Person>(
265265
});
266266
}
267267
}
268+
269+
public class TheGetFollowersMethod
270+
{
271+
[Fact]
272+
public async Task RequestsCorrectUrl()
273+
{
274+
var connection = Substitute.For<IApiConnection>();
275+
var client = new OrganizationsClient(connection);
276+
277+
await client.GetFollowers(123);
278+
279+
Received.InOrder(async () =>
280+
{
281+
await connection.GetAll<OrganizationFollower>(
282+
Arg.Is<Uri>(u => u.ToString() == "organizations/123/followers"),
283+
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
284+
&& d["id"] == "123"));
285+
});
286+
}
287+
}
288+
289+
public class TheAddFollowerMethod
290+
{
291+
[Fact]
292+
public void PostsToTheCorrectUrl()
293+
{
294+
var connection = Substitute.For<IApiConnection>();
295+
var client = new OrganizationsClient(connection);
296+
297+
client.AddFollower(1, 2);
298+
299+
connection.Received().Post<OrganizationFollower>(Arg.Is<Uri>(u => u.ToString() == "organizations/1/followers"),
300+
Arg.Is<object>(o => o.ToString() == new { user_id = 2 }.ToString()));
301+
}
302+
}
303+
304+
public class TheDeleteFollowerMethod
305+
{
306+
[Fact]
307+
public void DeletesCorrectUrl()
308+
{
309+
var connection = Substitute.For<IApiConnection>();
310+
var client = new OrganizationsClient(connection);
311+
312+
client.DeleteFollower(1, 461);
313+
314+
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "organizations/1/followers/461"));
315+
}
316+
}
268317
}
269318
}

src/Pipedrive.net/Clients/IOrganizationsClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ public interface IOrganizationsClient
2727
Task<IReadOnlyList<Deal>> GetDeals(long organizationId, OrganizationDealFilters filters);
2828

2929
Task<IReadOnlyList<Person>> GetPersons(long organizationId, OrganizationFilters filters);
30+
31+
Task<IReadOnlyList<OrganizationFollower>> GetFollowers(long dealId);
32+
33+
Task<OrganizationFollower> AddFollower(long dealId, long userId);
34+
35+
Task DeleteFollower(long dealId, long followerId);
3036
}
3137
}

src/Pipedrive.net/Clients/OrganizationsClient.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,28 @@ public Task<IReadOnlyList<Person>> GetPersons(long id, OrganizationFilters filte
112112

113113
return ApiConnection.GetAll<Person>(ApiUrls.OrganizationPersons(id), parameters, options);
114114
}
115+
116+
public Task<IReadOnlyList<OrganizationFollower>> GetFollowers(long personId)
117+
{
118+
var parameters = new Dictionary<string, string>()
119+
{
120+
{ "id", personId.ToString() }
121+
};
122+
123+
return ApiConnection.GetAll<OrganizationFollower>(ApiUrls.OrganizationFollowers(personId), parameters);
124+
}
125+
126+
public Task<OrganizationFollower> AddFollower(long personId, long userId)
127+
{
128+
return ApiConnection.Post<OrganizationFollower>(ApiUrls.OrganizationFollowers(personId), new
129+
{
130+
user_id = userId
131+
});
132+
}
133+
134+
public Task DeleteFollower(long dealId, long followerId)
135+
{
136+
return ApiConnection.Delete(ApiUrls.DeleteOrganizationFollower(dealId, followerId));
137+
}
115138
}
116139
}

src/Pipedrive.net/Helpers/ApiUrls.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,25 @@ public static Uri OrganizationDeal(long id)
306306
return new Uri($"organizations/{id}/deals", UriKind.Relative);
307307
}
308308

309+
/// <summary>
310+
/// Returns the <see cref="Uri"/> for all the followers of the specified organization.
311+
/// </summary>
312+
/// <param name="id">The id of the organization</param>
313+
public static Uri OrganizationFollowers(long id)
314+
{
315+
return new Uri($"organizations/{id}/followers", UriKind.Relative);
316+
}
317+
318+
/// <summary>
319+
/// Returns the <see cref="Uri"/> for deleting the follower of the specified organization.
320+
/// </summary>
321+
/// <param name="id">The id of the organization</param>
322+
/// <param name="organizationFollowerId">The id of the organization follower</param>
323+
public static Uri DeleteOrganizationFollower(long id, long organizationFollowerId)
324+
{
325+
return new Uri($"organizations/{id}/followers/{organizationFollowerId}", UriKind.Relative);
326+
}
327+
309328
/// <summary>
310329
/// Returns the <see cref="Uri"/> that returns all of the organization fields in response to a GET request.
311330
/// </summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Pipedrive
4+
{
5+
public class OrganizationFollower : Follower
6+
{
7+
[JsonProperty("organization_id")]
8+
public long OrganizationId { get; set; }
9+
}
10+
}

0 commit comments

Comments
 (0)