Skip to content

Commit

Permalink
feat: add get/add/delete follower for the Organization API
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed May 18, 2020
1 parent 1b3e155 commit fd392e8
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ public async Task ReturnsCorrectCount()
{
var pipedrive = Helper.GetAuthenticatedClient();

var dealFollowers = await pipedrive.Deal.GetFollowers(1);
Assert.Equal(1, dealFollowers.Count);
var followers = await pipedrive.Deal.GetFollowers(1);
Assert.Equal(1, followers.Count);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,42 @@ public async Task ReturnsDistinctInfosBasedOnStartPage()
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
}

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

var followers = await pipedrive.Organization.GetFollowers(1);
Assert.Equal(1, followers.Count);
}
}

public class TheAddFollowerMethod
{
[IntegrationTest]
public async Task CanAddFollower()
{
var pipedrive = Helper.GetAuthenticatedClient();
var fixture = pipedrive.Organization;

var addFollower = await fixture.AddFollower(1, 595707);
Assert.NotNull(addFollower);
}
}

public class TheDeleteFollowerMethod
{
[IntegrationTest]
public async Task CanDeleteFollower()
{
var pipedrive = Helper.GetAuthenticatedClient();
var fixture = pipedrive.Organization;

await fixture.DeleteFollower(1, 461);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ public async Task ReturnsCorrectCount()
{
var pipedrive = Helper.GetAuthenticatedClient();

var dealFollowers = await pipedrive.Person.GetFollowers(1);
Assert.Equal(1, dealFollowers.Count);
var followers = await pipedrive.Person.GetFollowers(1);
Assert.Equal(1, followers.Count);
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/Pipedrive.net.Tests/Clients/OrganizationsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,54 @@ await connection.GetAll<Person>(
});
}
}

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

await client.GetFollowers(123);

Received.InOrder(async () =>
{
await connection.GetAll<OrganizationFollower>(
Arg.Is<Uri>(u => u.ToString() == "organizations/123/followers"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
&& d["id"] == "123"));
});
}
}

public class TheAddFollowerMethod
{
[Fact]
public void PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

client.AddFollower(1, 2);

connection.Received().Post<OrganizationFollower>(Arg.Is<Uri>(u => u.ToString() == "organizations/1/followers"),
Arg.Is<object>(o => o.ToString() == new { user_id = 2 }.ToString()));
}
}

public class TheDeleteFollowerMethod
{
[Fact]
public void DeletesCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

client.DeleteFollower(1, 461);

connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "organizations/1/followers/461"));
}
}
}
}
6 changes: 6 additions & 0 deletions src/Pipedrive.net/Clients/IOrganizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ public interface IOrganizationsClient
Task<IReadOnlyList<Deal>> GetDeals(long organizationId, OrganizationDealFilters filters);

Task<IReadOnlyList<Person>> GetPersons(long organizationId, OrganizationFilters filters);

Task<IReadOnlyList<OrganizationFollower>> GetFollowers(long dealId);

Task<OrganizationFollower> AddFollower(long dealId, long userId);

Task DeleteFollower(long dealId, long followerId);
}
}
23 changes: 23 additions & 0 deletions src/Pipedrive.net/Clients/OrganizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,28 @@ public Task<IReadOnlyList<Person>> GetPersons(long id, OrganizationFilters filte

return ApiConnection.GetAll<Person>(ApiUrls.OrganizationPersons(id), parameters, options);
}

public Task<IReadOnlyList<OrganizationFollower>> GetFollowers(long personId)
{
var parameters = new Dictionary<string, string>()
{
{ "id", personId.ToString() }
};

return ApiConnection.GetAll<OrganizationFollower>(ApiUrls.OrganizationFollowers(personId), parameters);
}

public Task<OrganizationFollower> AddFollower(long personId, long userId)
{
return ApiConnection.Post<OrganizationFollower>(ApiUrls.OrganizationFollowers(personId), new
{
user_id = userId
});
}

public Task DeleteFollower(long dealId, long followerId)
{
return ApiConnection.Delete(ApiUrls.DeleteOrganizationFollower(dealId, followerId));
}
}
}
19 changes: 19 additions & 0 deletions src/Pipedrive.net/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,25 @@ public static Uri OrganizationDeal(long id)
return new Uri($"organizations/{id}/deals", UriKind.Relative);
}

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

/// <summary>
/// Returns the <see cref="Uri"/> for deleting the follower of the specified organization.
/// </summary>
/// <param name="id">The id of the organization</param>
/// <param name="organizationFollowerId">The id of the organization follower</param>
public static Uri DeleteOrganizationFollower(long id, long organizationFollowerId)
{
return new Uri($"organizations/{id}/followers/{organizationFollowerId}", UriKind.Relative);
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the organization fields in response to a GET request.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Pipedrive.net/Models/Response/OrganizationFollower.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Pipedrive
{
public class OrganizationFollower : Follower
{
[JsonProperty("organization_id")]
public long OrganizationId { get; set; }
}
}

0 comments on commit fd392e8

Please sign in to comment.