Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,29 @@ while(admins.hasNext()) {
### Events

```java
Event event = new Event().setEventName("bought-hat")
// Create an event with a user ID
// This is only valid for users
Event event = new Event()
.setEventName("bought-hat")
.setUserID("1")
.putMetadata("invitee_email", "jayne@serenity.io")
.putMetadata("found_date", System.currentTimeMillis())
.putMetadata("new_signup", true);
Event.create(event);

// Create an event with an email
// This is only valid for users
Event event = new Event()
.setEventName("bought-hat")
.setEmail("test@example.com");
Event.create(event);

// Create an event with an ID
// This is valid for both users and leads
Event event = new Event()
.setEventName("bought-hat")
.setId("599d6aeeda850883ed8ba7c2");
Event.create(event);
```


Expand Down
16 changes: 16 additions & 0 deletions intercom-java/src/main/java/io/intercom/api/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static void validateCreateEvent(Event event) {
}

if (Strings.isNullOrEmpty(event.getUserID())
&& Strings.isNullOrEmpty(event.getId())
&& Strings.isNullOrEmpty(event.getEmail())) {
throw new InvalidException(INVALID_USER);
}
Expand All @@ -94,6 +95,9 @@ static void validateCreateEvent(Event event) {
@JsonProperty("email")
private String email;

@JsonProperty("id")
private String id;

@JsonProperty("user_id")
private String userID;

Expand Down Expand Up @@ -135,6 +139,15 @@ public Event setEmail(String email) {
return this;
}

public String getId() {
return id;
}

public Event setId(String id) {
this.id = id;
return this;
}

public String getUserID() {
return userID;
}
Expand Down Expand Up @@ -197,6 +210,7 @@ public boolean equals(Object o) {
if (!type.equals(event.type)) return false;
//noinspection RedundantIfStatement
if (userID != null ? !userID.equals(event.userID) : event.userID != null) return false;
if (id != null ? !id.equals(event.id) : event.id != null) return false;

return true;
}
Expand All @@ -208,6 +222,7 @@ public int hashCode() {
result = 31 * result + (int) (createdAt ^ (createdAt >>> 32));
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (userID != null ? userID.hashCode() : 0);
result = 31 * result + (id != null ? id.hashCode() : 0);
result = 31 * result + (metadata != null ? metadata.hashCode() : 0);
return result;
}
Expand All @@ -219,6 +234,7 @@ public String toString() {
", eventName='" + eventName + '\'' +
", createdAt=" + createdAt +
", email='" + email + '\'' +
", id='" + id + '\'' +
", userID='" + userID + '\'' +
", metadata=" + metadata +
"} " + super.toString();
Expand Down
53 changes: 52 additions & 1 deletion intercom-java/src/test/java/io/intercom/api/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ public void testMissingEventName() {
}
}

@Test
public void testMissingId() {
final Event event1 = new Event().setEventName("test-id");
try {
Event.create(event1);
fail("an event with no id or email should be invalid");
} catch (InvalidException e) {
assertTrue(e.getFirstError() != null);
}

final Event event2 = new Event()
.setEventName("test-id")
.setId("")
.putMetadata("invitee_email", "jayne@serenity.io");
try {
Event.create(event2);
fail("an event with an empty id should be invalid");
} catch (InvalidException e) {
assertTrue(e.getFirstError() != null);
}

final Event event3 = new Event()
.setId("49bf6b081d661db4408a51e1")
.setEmail("");
try {
Event.create(event3);
fail("an event with an empty email should be invalid");
} catch (InvalidException e) {
assertTrue(e.getFirstError() != null);
}

}

@Test
public void testValid() {
Event event1 = new Event()
Expand Down Expand Up @@ -126,7 +159,25 @@ public void testValid() {
} catch (InvalidException e) {
fail("an event with a user id, email and a name should be valid");
}
}

Event event4 = new Event()
.setId("49bf6b081d661db4408a51e1")
.setEventName("test-id");
try {
Event.validateCreateEvent(event4);
} catch (InvalidException e) {
fail("an event with an id and a name should be valid");
}

Event event5 = new Event()
.setId("49bf6b081d661db4408a51e1")
.setEmail("jayne@serenity.io")
.setEventName("test-id");
try {
Event.validateCreateEvent(event5);
} catch (InvalidException e) {
fail("an event with an id, email and a name should be valid");
}

}
}