Skip to content

Commit bbd299c

Browse files
author
Timothy Lim
committed
Add deletion by ID and User ID
1 parent b255527 commit bbd299c

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
@@ -199,6 +199,14 @@ contact = Contact.findByUserID("e1a7d875-d83a-46f7-86f4-73be98a98584");
199199
contact.setName("Stitch Hessian");
200200
Contact updated = Contact.update(contact);
201201

202+
// Update a contact by ID
203+
Contact contact = new Contact().setID("541a144b201ebf2ec5000002").setName("Stitch Hessian");
204+
Contact updated = Contact.update(contact);
205+
206+
// Update a contact by User ID
207+
Contact contact = new Contact().setUserID("e1a7d875-d83a-46f7-86f4-73be98a98584").setName("Stitch Hessian");
208+
Contact updated = Contact.update(contact);
209+
202210
// Read a contact list by email
203211
ContactCollection contacts = Contact.listByEmail("jubal@serenity.io");
204212
while(contacts.hasNext()) {
@@ -216,10 +224,21 @@ ScrollableContactCollection contactsScroll = Contact.scroll();
216224
List<Contact> contacts = contactsScroll.getPage();
217225
contactsScroll = contactsScroll.scroll();
218226

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

220233
// Remove a contact
221234
Contact.delete(contact);
222235

236+
// Remove a contact by id
237+
Contact.delete(contact.getID());
238+
239+
// Remove a contact by user_id
240+
Contact.deleteByUserID(contact.getUserID());
241+
223242
// Convert a contact
224243
User converted = Contact.convert(contact, user);
225244
```

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)