Skip to content

Commit 73f7f8c

Browse files
authored
ID field option for create events (#151)
* Added id field to the Event object to allow for creation of events where the intercom id is known. * Adding tests for create event with ID PR
1 parent 85e80ed commit 73f7f8c

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,29 @@ while(admins.hasNext()) {
258258
### Events
259259

260260
```java
261-
Event event = new Event().setEventName("bought-hat")
261+
// Create an event with a user ID
262+
// This is only valid for users
263+
Event event = new Event()
264+
.setEventName("bought-hat")
262265
.setUserID("1")
263266
.putMetadata("invitee_email", "jayne@serenity.io")
264267
.putMetadata("found_date", System.currentTimeMillis())
265268
.putMetadata("new_signup", true);
266269
Event.create(event);
267270

271+
// Create an event with an email
272+
// This is only valid for users
273+
Event event = new Event()
274+
.setEventName("bought-hat")
275+
.setEmail("test@example.com");
276+
Event.create(event);
277+
278+
// Create an event with an ID
279+
// This is valid for both users and leads
280+
Event event = new Event()
281+
.setEventName("bought-hat")
282+
.setId("599d6aeeda850883ed8ba7c2");
283+
Event.create(event);
268284
```
269285

270286

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static void validateCreateEvent(Event event) {
7676
}
7777

7878
if (Strings.isNullOrEmpty(event.getUserID())
79+
&& Strings.isNullOrEmpty(event.getId())
7980
&& Strings.isNullOrEmpty(event.getEmail())) {
8081
throw new InvalidException(INVALID_USER);
8182
}
@@ -94,6 +95,9 @@ static void validateCreateEvent(Event event) {
9495
@JsonProperty("email")
9596
private String email;
9697

98+
@JsonProperty("id")
99+
private String id;
100+
97101
@JsonProperty("user_id")
98102
private String userID;
99103

@@ -135,6 +139,15 @@ public Event setEmail(String email) {
135139
return this;
136140
}
137141

142+
public String getId() {
143+
return id;
144+
}
145+
146+
public Event setId(String id) {
147+
this.id = id;
148+
return this;
149+
}
150+
138151
public String getUserID() {
139152
return userID;
140153
}
@@ -197,6 +210,7 @@ public boolean equals(Object o) {
197210
if (!type.equals(event.type)) return false;
198211
//noinspection RedundantIfStatement
199212
if (userID != null ? !userID.equals(event.userID) : event.userID != null) return false;
213+
if (id != null ? !id.equals(event.id) : event.id != null) return false;
200214

201215
return true;
202216
}
@@ -208,6 +222,7 @@ public int hashCode() {
208222
result = 31 * result + (int) (createdAt ^ (createdAt >>> 32));
209223
result = 31 * result + (email != null ? email.hashCode() : 0);
210224
result = 31 * result + (userID != null ? userID.hashCode() : 0);
225+
result = 31 * result + (id != null ? id.hashCode() : 0);
211226
result = 31 * result + (metadata != null ? metadata.hashCode() : 0);
212227
return result;
213228
}
@@ -219,6 +234,7 @@ public String toString() {
219234
", eventName='" + eventName + '\'' +
220235
", createdAt=" + createdAt +
221236
", email='" + email + '\'' +
237+
", id='" + id + '\'' +
222238
", userID='" + userID + '\'' +
223239
", metadata=" + metadata +
224240
"} " + super.toString();

intercom-java/src/test/java/io/intercom/api/EventTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,39 @@ public void testMissingEventName() {
9797
}
9898
}
9999

100+
@Test
101+
public void testMissingId() {
102+
final Event event1 = new Event().setEventName("test-id");
103+
try {
104+
Event.create(event1);
105+
fail("an event with no id or email should be invalid");
106+
} catch (InvalidException e) {
107+
assertTrue(e.getFirstError() != null);
108+
}
109+
110+
final Event event2 = new Event()
111+
.setEventName("test-id")
112+
.setId("")
113+
.putMetadata("invitee_email", "jayne@serenity.io");
114+
try {
115+
Event.create(event2);
116+
fail("an event with an empty id should be invalid");
117+
} catch (InvalidException e) {
118+
assertTrue(e.getFirstError() != null);
119+
}
120+
121+
final Event event3 = new Event()
122+
.setId("49bf6b081d661db4408a51e1")
123+
.setEmail("");
124+
try {
125+
Event.create(event3);
126+
fail("an event with an empty email should be invalid");
127+
} catch (InvalidException e) {
128+
assertTrue(e.getFirstError() != null);
129+
}
130+
131+
}
132+
100133
@Test
101134
public void testValid() {
102135
Event event1 = new Event()
@@ -126,7 +159,25 @@ public void testValid() {
126159
} catch (InvalidException e) {
127160
fail("an event with a user id, email and a name should be valid");
128161
}
129-
}
130162

163+
Event event4 = new Event()
164+
.setId("49bf6b081d661db4408a51e1")
165+
.setEventName("test-id");
166+
try {
167+
Event.validateCreateEvent(event4);
168+
} catch (InvalidException e) {
169+
fail("an event with an id and a name should be valid");
170+
}
131171

172+
Event event5 = new Event()
173+
.setId("49bf6b081d661db4408a51e1")
174+
.setEmail("jayne@serenity.io")
175+
.setEventName("test-id");
176+
try {
177+
Event.validateCreateEvent(event5);
178+
} catch (InvalidException e) {
179+
fail("an event with an id, email and a name should be valid");
180+
}
181+
182+
}
132183
}

0 commit comments

Comments
 (0)