Skip to content

Commit aefbaf4

Browse files
thewheatchoran
authored andcommitted
Add deletion by ID and User ID (#200)
1 parent 5586abb commit aefbaf4

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ contact = Contact.findByUserID("e1a7d875-d83a-46f7-86f4-73be98a98584");
201201
contact.setName("Stitch Hessian");
202202
Contact updated = Contact.update(contact);
203203

204+
// Update a contact by ID
205+
Contact contact = new Contact().setID("541a144b201ebf2ec5000002").setName("Stitch Hessian");
206+
Contact updated = Contact.update(contact);
207+
208+
// Update a contact by User ID
209+
Contact contact = new Contact().setUserID("e1a7d875-d83a-46f7-86f4-73be98a98584").setName("Stitch Hessian");
210+
Contact updated = Contact.update(contact);
211+
204212
// Read a contact list by email
205213
ContactCollection contacts = Contact.listByEmail("[email protected]");
206214
while(contacts.hasNext()) {
@@ -218,10 +226,21 @@ ScrollableContactCollection contactsScroll = Contact.scroll();
218226
List<Contact> contacts = contactsScroll.getPage();
219227
contactsScroll = contactsScroll.scroll();
220228

229+
// List contacts (sorting)
230+
Map<String, String> params = Maps.newHashMap();
231+
params.put("sort", "created_at");
232+
params.put("order", "asc");
233+
ContactCollection contacts = Contact.list(params);
221234

222235
// Remove a contact
223236
Contact.delete(contact);
224237

238+
// Remove a contact by id
239+
Contact.delete(contact.getID());
240+
241+
// Remove a contact by user_id
242+
Contact.deleteByUserID(contact.getUserID());
243+
225244
// Convert a contact
226245
User converted = Contact.convert(contact, user);
227246
```

intercom-java/src/main/java/io/intercom/api/Contact.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.JsonInclude;
77
import com.fasterxml.jackson.annotation.JsonProperty;
88
import com.google.common.annotations.VisibleForTesting;
9+
import com.google.common.base.Strings;
910
import com.google.common.collect.Maps;
1011

1112
import java.net.URI;
@@ -66,7 +67,27 @@ public static Contact update(Contact c)
6667

6768
public static Contact delete(Contact c)
6869
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
69-
return DataResource.delete(c.getID(), "contacts", Contact.class);
70+
if(!Strings.isNullOrEmpty(c.getID())) {
71+
return delete(c.getID());
72+
}
73+
else if(!Strings.isNullOrEmpty(c.getUserID())) {
74+
return deleteByUserID(c.getUserID());
75+
}
76+
else {
77+
throw new InvalidException("to delete a contact you must provide a id or user_id value");
78+
}
79+
}
80+
81+
public static Contact delete(String id)
82+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
83+
return DataResource.delete(id, "contacts", Contact.class);
84+
}
85+
86+
public static Contact deleteByUserID(String user_id)
87+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
88+
Map<String, String> params = new HashMap<String,String>();
89+
params.put("user_id", user_id);
90+
return DataResource.delete(params, "contacts", Contact.class);
7091
}
7192

7293
public static User convert(Contact c, User u)
@@ -416,8 +437,7 @@ public String getID() {
416437
return id;
417438
}
418439

419-
@VisibleForTesting
420-
Contact setID(String id) {
440+
public Contact setID(String id) {
421441
this.id = id;
422442
return this;
423443
}
@@ -457,6 +477,11 @@ public String getUserID() {
457477
return userID;
458478
}
459479

480+
public Contact setUserID(String userID) {
481+
this.userID = userID;
482+
return this;
483+
}
484+
460485
public Avatar getAvatar() {
461486
return avatar;
462487
}

0 commit comments

Comments
 (0)