Subversion Repository Public Repository

insightly-api

Diff Revisions 4 vs 9 for /trunk/Insightly/InsightlyAPI.cs

Diff revisions: vs.
  @@ -4,6 +4,7 @@
4 4 using RestSharp;
5 5 using TS.Insightly.API.Contract;
6 6 using TS.Insightly.API.Exception;
7 + using TS.Insightly.API.Utility;
7 8
8 9 namespace TS.Insightly.API
9 10 {
  @@ -53,6 +54,8 @@
53 54 return response.Data;
54 55 }
55 56
57 + #region Contacts
58 +
56 59 /// <summary>
57 60 /// Gets the contact for the unique contact ID.
58 61 /// Note that the Id is not the Id shown for the contact in the web browser, but
  @@ -73,7 +76,7 @@
73 76 /// Gets the contacts for email address.
74 77 /// </summary>
75 78 /// <param name="emailAddress">The email address.</param>
76 - /// <returns>List of contacts with a matching email address.</returns>
79 + /// <returns>List of contacts with a matching email address or empty list if no matches found.</returns>
77 80 public List<Contact> GetContactsForEmail(string emailAddress)
78 81 {
79 82 var request = new RestRequest(Method.GET);
  @@ -111,6 +114,58 @@
111 114 }
112 115
113 116 /// <summary>
117 + /// Adds the new contact if a contact does not already exist for the same email,
118 + /// first and last names as the given contact.
119 + /// </summary>
120 + /// <param name="newContact">The new contact.</param>
121 + /// <returns>Structure containing details of the add.</returns>
122 + public ContactAddResult AddNewContactIfNotExists(Contact newContact)
123 + {
124 + ContactAddResult addResult = new ContactAddResult { NewContactAdded = false, Contact = newContact };
125 +
126 + if (newContact.CONTACTINFOS == null)
127 + {
128 + addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) };
129 + }
130 + else
131 + {
132 + bool existingFound = false;
133 +
134 + foreach (var contactInfo in newContact.CONTACTINFOS)
135 + {
136 + if (contactInfo.TYPE.Equals(ContactInfo.InfoType.Email.GetDescription(), StringComparison.InvariantCultureIgnoreCase))
137 + {
138 + var existingContacts = GetContactsForEmail(contactInfo.DETAIL);
139 +
140 + foreach (var contact in existingContacts)
141 + {
142 + if (contact.FIRST_NAME.Equals(newContact.FIRST_NAME, StringComparison.InvariantCultureIgnoreCase)
143 + &&
144 + contact.LAST_NAME.Equals(newContact.LAST_NAME, StringComparison.InvariantCultureIgnoreCase))
145 + {
146 + addResult = new ContactAddResult { NewContactAdded = false, Contact = contact };
147 + existingFound = true;
148 + break;
149 + }
150 + }
151 + }
152 +
153 + if (existingFound)
154 + {
155 + break;
156 + }
157 + }
158 +
159 + if (!existingFound)
160 + {
161 + addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) };
162 + }
163 + }
164 +
165 + return addResult;
166 + }
167 +
168 + /// <summary>
114 169 /// Deletes the contact.
115 170 /// Note that the Id is not the Id shown for the contact in the web browser, but
116 171 /// the contact Id that is unique to insightly.
  @@ -133,5 +188,31 @@
133 188
134 189 return result;
135 190 }
191 +
192 + /// <summary>
193 + /// Result for conditional contact add.
194 + /// </summary>
195 + public struct ContactAddResult
196 + {
197 + /// <summary>
198 + /// <code>true</code> If the condition check resulted in a new contact added.
199 + /// Note if there was an add failure <see cref="Contact"/> may not be valid.
200 + /// </summary>
201 + public bool NewContactAdded;
202 +
203 + /// <summary>
204 + /// The contact, if <see cref="NewContactAdded"/> was <code>true</code> this will
205 + /// be the newly added contact, otherwise it will be the existing contact.
206 + /// </summary>
207 + public Contact Contact;
208 + }
209 +
210 + #endregion Contacts
211 +
212 + #region Organisations
213 +
214 +
215 + #endregion Organisations
216 +
136 217 }
137 218 }