Skip to content

Commit

Permalink
Created an easier way to associate contacts/companies with a deal
Browse files Browse the repository at this point in the history
  • Loading branch information
Psypher9 committed Jun 9, 2019
1 parent 49b3a5c commit 391f0be
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions HubSpot.NET/Api/Deal/Dto/DealHubSpotModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using HubSpot.NET.Api.Company.Dto;
using HubSpot.NET.Api.Contact.Dto;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Api.Deal.Dto
Expand All @@ -12,10 +14,30 @@ namespace HubSpot.NET.Api.Deal.Dto
public class DealHubSpotAssociations
{
[DataMember(Name = "associatedCompanyIds")]
public long[] AssociatedCompany { get; set; }
public long[] AssociatedCompanies { get; set; }

[DataMember(Name = "associatedVids")]
public long[] AssociatedContacts { get; set; }

public void AddContactById(long contactId)
{
int index = AssociatedContacts.Length - 1;

if (index < 0)
index = 0;

AssociatedContacts[index] = contactId;
}

public void AddCompanyById(long companyId)
{
int index = AssociatedCompanies.Length - 1;

if (index < 0)
index = 0;

AssociatedCompanies[index] = companyId;
}
}

/// <summary>
Expand Down Expand Up @@ -62,6 +84,18 @@ public DealHubSpotModel()

public bool IsNameValue => true;

public void AssociateContact(ContactHubSpotModel contact)
{
if(contact.Id.HasValue)
Associations.AddContactById(contact.Id.Value);
}

public void AssociateCompany(CompanyHubSpotModel company)
{
if (company.Id.HasValue)
Associations.AddCompanyById(company.Id.Value);
}

public virtual void ToHubSpotDataEntity(ref DealHubSpotModel converted)
{
converted.Associations = Associations;
Expand All @@ -72,7 +106,7 @@ public virtual void FromHubSpotDataEntity(DealHubSpotModel hubspotData)
if (hubspotData.Associations != null)
{
Associations.AssociatedContacts = hubspotData.Associations.AssociatedContacts;
Associations.AssociatedCompany = hubspotData.Associations.AssociatedCompany;
Associations.AssociatedCompanies = hubspotData.Associations.AssociatedCompanies;
}
}
}
Expand Down

1 comment on commit 391f0be

@Psypher9
Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the name of AssociatedCompany to AssociatedCompanies because even though it might be reasonable to think that only one company would be associated to a deal at a time. The functionality is there associate multiple and the actually property in the JSON object is an array, it made more sense to indicate that convention in the naming

Please sign in to comment.