Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement addLead #174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Pipedrive.net/Clients/ILeadsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ namespace Pipedrive
/// </summary>
/// <remarks>
/// See the <a href="https://developers.pipedrive.com/docs/api/v1/Leads">Lead API documentation</a> for more information.
/// </remarks>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Bugfix: This XML docs don't work because close tag missing.

public interface ILeadsClient
{
Task<IReadOnlyList<Lead>> GetAll(LeadFilters filters);

Task<Lead> Get(Guid id);

Task<LeadCreated> Create(NewLead data);
}
}
8 changes: 8 additions & 0 deletions src/Pipedrive.net/Clients/LeadsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Pipedrive.Helpers;

namespace Pipedrive
Expand Down Expand Up @@ -39,5 +40,12 @@ public Task<Lead> Get(Guid id)
{
return ApiConnection.Get<Lead>(ApiUrls.Lead(id));
}

public Task<LeadCreated> Create(NewLead data)
{
Ensure.ArgumentNotNull(data, nameof(data));

return ApiConnection.Post<LeadCreated>(ApiUrls.Leads(), JObject.FromObject(data));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using JObject.FromObject construction to handle null values.
Without null values handling receive error like:

{
   "success":false,
   "data":null,
   "additional_data":null,
   "error":"provided dataset is not valid",
   "error_info":"\"owner_id\" must be a number"
}

Example of request:

var data = new NewLead("Lead created from C#")
{
    OrganizationId = 1
};
var createdLead = await client.Lead.Create(data);

}
}
}
45 changes: 45 additions & 0 deletions src/Pipedrive.net/Models/Request/Leads/NewLead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Pipedrive.Models.Response;

namespace Pipedrive
{
public class NewLead : IEntityWithCustomFields
{
[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("owner_id", NullValueHandling = NullValueHandling.Ignore)]
public long? OwnerId { get; set; }

[JsonProperty("label_ids", NullValueHandling = NullValueHandling.Ignore)]
public IEnumerable<Guid> LabelIds { get; set; }

[JsonProperty("person_id", NullValueHandling = NullValueHandling.Ignore)]
public long? PersonId { get; set; }

[JsonProperty("organization_id", NullValueHandling = NullValueHandling.Ignore)]
public long? OrganizationId { get; set; }

[JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)]
public CurrencyAmount Value { get; set; }

[JsonProperty("expected_close_date", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? ExpectedCloseDate { get; set; }

[JsonProperty("visible_to", NullValueHandling = NullValueHandling.Ignore)]
public Visibility? VisibleTo { get; set; }

[JsonProperty("was_seen", NullValueHandling = NullValueHandling.Ignore)]
public bool? WasSeen { get; set; }

[JsonIgnore]
public IDictionary<string, ICustomField> CustomFields { get; set; } = new Dictionary<string, ICustomField>();

public NewLead(string title)
{
this.Title = title;
}
}
}
26 changes: 15 additions & 11 deletions src/Pipedrive.net/Models/Response/Leads/AbstractLead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@ public abstract class AbstractLead
[JsonProperty("label_ids")]
public List<Guid> LabelIds { get; set; }

public CurrencyAmount Value { get; set; }

[JsonProperty("expected_close_date")]
public DateTime? ExpectedCloseDate { get; set; }

public string Note { get; set; }

[JsonProperty("person_id")]
public long PersonId { get; set; }
public long? PersonId { get; set; }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added null possible for person_id because, this value can be null. And we receive JsonSerialization exception on try set null in not-nullable PersonId

Exception:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int64'. Path 'person_id'


[JsonProperty("organization_id")]
public long? OrganizationId { get; set; }

[JsonProperty("is_archived")]
public bool IsArchived { get; set; }

[JsonProperty("source_name")]
public string SourceName { get; set; }

[JsonProperty("is_archived")]
public bool IsArchived { get; set; }

[JsonProperty("was_seen")]
public bool WasSeen { get; set; }

public CurrencyAmount Value { get; set; }

[JsonProperty("expected_close_date")]
public DateTime? ExpectedCloseDate { get; set; }

[JsonProperty("next_activity_id")]
public long? NextActivityId { get; set; }

Expand All @@ -50,5 +48,11 @@ public abstract class AbstractLead

[JsonProperty("update_time")]
public DateTime? UpdateTime { get; set; }

[JsonProperty("visible_to")]
public string VisibleTo { get; set; }

[JsonProperty("cc_email")]
public string CcEmail { get; set; }
Comment on lines +52 to +56
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added missing fields

}
}
2 changes: 2 additions & 0 deletions src/Pipedrive.net/Models/Response/Leads/Lead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Pipedrive
[JsonConverter(typeof(CustomFieldConverter))]
public class Lead : AbstractLead, IEntityWithCustomFields
{
public string Note { get; set; }

[JsonIgnore]
public IDictionary<string, ICustomField> CustomFields { get; set; }
}
Expand Down
4 changes: 4 additions & 0 deletions src/Pipedrive.net/Models/Response/Leads/LeadCreated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Pipedrive
{
public class LeadCreated : AbstractLead { }
}