Skip to content

Commit 621a39f

Browse files
committed
convert operation for contacts
1 parent 3ebb466 commit 621a39f

File tree

5 files changed

+162
-4
lines changed

5 files changed

+162
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ while(allContacts.hasNext()) {
173173
174174
// Remove a contact
175175
Contact.delete(contact);
176+
177+
// Convert a contact
178+
User converted = Contact.convert(contact, user);
176179
```
177180
178181
### Companies

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
66
import com.fasterxml.jackson.annotation.JsonInclude;
77
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.google.common.annotations.VisibleForTesting;
89
import com.google.common.collect.Maps;
910

1011
import java.net.URI;
12+
import java.util.HashMap;
1113
import java.util.List;
1214
import java.util.Map;
1315

@@ -62,10 +64,115 @@ public static Contact delete(Contact c)
6264
return DataResource.delete(c.getID(), "contacts", Contact.class);
6365
}
6466

67+
public static User convert(Contact c, User u)
68+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
69+
return DataResource.post(ContactConvertBuilder.buildConvert(c, u), convertURI(), User.class);
70+
}
71+
6572
private static URI contactURI(String id) {
6673
return UriBuilder.newBuilder().path("contacts").path(id).build();
6774
}
6875

76+
private static URI convertURI() {
77+
return UriBuilder.newBuilder().path("contacts").path("convert").build();
78+
}
79+
80+
static class ContactConvertBuilder {
81+
82+
static ContactConvert buildConvert(Contact c, User u) throws InvalidException {
83+
return new ContactConvertBuilder().build(c, u);
84+
}
85+
86+
ContactConvert build(Contact c, User u) throws InvalidException {
87+
return new ContactConvert(buildConvertContact(c), buildConvertUser(u));
88+
}
89+
90+
HashMap<String, String> buildConvertUser(User u) {
91+
final HashMap<String, String> convertUser = Maps.newHashMap();
92+
93+
if (u.getId() != null) {
94+
convertUser.put("id", u.getId());
95+
}
96+
97+
if (u.getUserId() != null) {
98+
convertUser.put("user_id", u.getUserId());
99+
}
100+
101+
if (u.getEmail() != null) {
102+
convertUser.put("email", u.getEmail());
103+
}
104+
105+
checkValidConvertContact(convertUser);
106+
107+
return convertUser;
108+
}
109+
110+
void checkValidConvertContact(HashMap<String, String> convertUser) {
111+
if ((!convertUser.containsKey("id")) && (!convertUser.containsKey("user_id")) && (!convertUser.containsKey("email"))) {
112+
throw new InvalidException("a convert user must include at least one of, an id, user_id or email parameter");
113+
}
114+
}
115+
116+
HashMap<String, String> buildConvertContact(Contact c) {
117+
final HashMap<String, String> convertContact = Maps.newHashMap();
118+
119+
if (c.getID() != null) {
120+
convertContact.put("id", c.getID());
121+
}
122+
123+
if (c.getUserID() != null) {
124+
convertContact.put("user_id", c.getUserID());
125+
}
126+
127+
checkValidConvertUser(convertContact);
128+
129+
return convertContact;
130+
}
131+
132+
void checkValidConvertUser(HashMap<String, String> convertContact) {
133+
if ((!convertContact.containsKey("id")) && (!convertContact.containsKey("user_id"))) {
134+
throw new InvalidException("a convert contact must include at least one of, an id or a user_id parameter");
135+
}
136+
}
137+
}
138+
139+
140+
@SuppressWarnings("UnusedDeclaration")
141+
@JsonIgnoreProperties(ignoreUnknown = true)
142+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
143+
static class ContactConvert extends TypedData {
144+
145+
@JsonProperty("contact")
146+
private Map<String, String> contact;
147+
148+
@JsonProperty("user")
149+
private Map<String, String> user;
150+
151+
public ContactConvert() {
152+
}
153+
154+
public ContactConvert(Map<String, String> contact, Map<String, String> user) {
155+
this.contact = contact;
156+
this.user = user;
157+
}
158+
159+
public Map<String, String> getContact() {
160+
return contact;
161+
}
162+
163+
public void setContact(Map<String, String> contact) {
164+
this.contact = contact;
165+
}
166+
167+
public Map<String, String> getUser() {
168+
return user;
169+
}
170+
171+
public void setUser(Map<String, String> user) {
172+
this.user = user;
173+
}
174+
}
175+
69176
@SuppressWarnings("UnusedDeclaration")
70177
@JsonIgnoreProperties(ignoreUnknown = true)
71178
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@@ -289,6 +396,12 @@ public String getID() {
289396
return id;
290397
}
291398

399+
@VisibleForTesting
400+
Contact setID(String id) {
401+
this.id = id;
402+
return this;
403+
}
404+
292405
public String getName() {
293406
return name;
294407
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public static <T, R> R update(T entity, String collectionPath, Class<R> response
2626
return resource.post(response, entity);
2727
}
2828

29+
public static <T, R> R post(T entity, URI path, Class<R> response) {
30+
final HttpClient resource = new HttpClient(path);
31+
return resource.post(response, entity);
32+
}
33+
2934
public static <T, R> R updatePut(T entity, URI collectionPath, Class<R> response) {
3035
final HttpClient resource = new HttpClient(collectionPath);
3136
return resource.put(response, entity);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,18 +530,20 @@ public boolean equals(Object o) {
530530
return true;
531531
}
532532

533+
533534
@Override
534535
public int hashCode() {
535-
int result = type.hashCode();
536+
int result = type != null ? type.hashCode() : 0;
536537
result = 31 * result + (id != null ? id.hashCode() : 0);
537538
result = 31 * result + (name != null ? name.hashCode() : 0);
538539
result = 31 * result + (email != null ? email.hashCode() : 0);
539540
result = 31 * result + (userId != null ? userId.hashCode() : 0);
541+
result = 31 * result + (pseudonym != null ? pseudonym.hashCode() : 0);
540542
result = 31 * result + (avatar != null ? avatar.hashCode() : 0);
541543
result = 31 * result + (int) (createdAt ^ (createdAt >>> 32));
542544
result = 31 * result + (int) (updatedAt ^ (updatedAt >>> 32));
543545
result = 31 * result + (int) (remoteCreatedAt ^ (remoteCreatedAt >>> 32));
544-
result = 31 * result + (unsubscribedFromEmails ? 1 : 0);
546+
result = 31 * result + (unsubscribedFromEmails != null ? unsubscribedFromEmails.hashCode() : 0);
545547
result = 31 * result + sessionCount;
546548
result = 31 * result + (int) (lastRequestAt ^ (lastRequestAt >>> 32));
547549
result = 31 * result + (int) (signedUpAt ^ (signedUpAt >>> 32));
@@ -553,8 +555,8 @@ public int hashCode() {
553555
result = 31 * result + (socialProfileCollection != null ? socialProfileCollection.hashCode() : 0);
554556
result = 31 * result + (segmentCollection != null ? segmentCollection.hashCode() : 0);
555557
result = 31 * result + (tagCollection != null ? tagCollection.hashCode() : 0);
556-
result = 31 * result + (updateLastRequestAt ? 1 : 0);
557-
result = 31 * result + (newSession ? 1 : 0);
558+
result = 31 * result + (updateLastRequestAt != null ? updateLastRequestAt.hashCode() : 0);
559+
result = 31 * result + (newSession != null ? newSession.hashCode() : 0);
558560
result = 31 * result + (untag != null ? untag.hashCode() : 0);
559561
return result;
560562
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.intercom.api;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class ContactTest {
8+
9+
10+
@Test
11+
public void testValidateConvertBuilder() {
12+
Contact.ContactConvertBuilder builder = new Contact.ContactConvertBuilder();
13+
14+
Contact c = new Contact();
15+
User u = new User().setEmail("[email protected]");
16+
17+
try {
18+
builder.build(c,u);
19+
fail();
20+
} catch (InvalidException e) {
21+
assertTrue(e.getMessage().contains("convert contact must"));
22+
}
23+
24+
c = new Contact().setID("1");
25+
u = new User();
26+
27+
try {
28+
builder.build(c,u);
29+
fail();
30+
} catch (InvalidException e) {
31+
assertTrue(e.getMessage().contains("convert user must"));
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)