Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from rishabh9/add-customer-name
Browse files Browse the repository at this point in the history
Save customer name along with email
  • Loading branch information
rishabh9 authored Apr 6, 2017
2 parents 99c15bc + 719cf20 commit 77c5c9d
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/controllers/IncomingDataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ public CompletionStage<Result> index() {
}

private CompletionStage<Result> executeOrderEvent(JsonNode json, Messages messages) {
String email = json.findPath("customerEmail").textValue();
String customerEmail = json.findPath("customerEmail").textValue();
String customerName = json.findPath("customerName").textValue();
String sellerId = json.findPath("seller").findPath("id").textValue();
log.debug("To add email {} into mailing list of seller {}", email, sellerId);
if (email == null) {
log.debug("To add email {} into mailing list of seller {}", customerEmail, sellerId);
if (customerEmail == null) {
log.warn("Incoming data is missing email parameter");
return complete(badRequest(ErrorUtil.toJson(BAD_REQUEST, messages.at(MISSING_PARAM_EMAIL))));
} else {
Expand All @@ -105,7 +106,8 @@ private CompletionStage<Result> executeOrderEvent(JsonNode json, Messages messag
.setAuth("username", apiKey.get())
.setContentType(Http.MimeTypes.JSON);
ObjectNode data = Json.newObject();
data.put("email_address", email);
data.put("email_address", customerEmail);
data.set("merge_fields", getFirstAndLastName(customerName));
data.put("status", "subscribed");
return request.post(data).thenApply(wsResponse -> {
JsonNode jsonResponse = wsResponse.asJson();
Expand All @@ -120,6 +122,23 @@ private CompletionStage<Result> executeOrderEvent(JsonNode json, Messages messag
}
}

private JsonNode getFirstAndLastName(String customerName) {
ObjectNode data = Json.newObject();
if (StringUtils.hasText(customerName)) {
String[] firstAndLastName = customerName.trim().split(" ");
if (firstAndLastName.length == 2) {
data.put("FNAME", firstAndLastName[0]);
data.put("LNAME", firstAndLastName[1]);
} else if (firstAndLastName.length > 2) {
data.put("FNAME", firstAndLastName[0]);
data.put("LNAME", firstAndLastName[firstAndLastName.length - 1]);
} else {
data.put("FNAME", customerName);
}
}
return data;
}

private String getUrl(Installation installation, String configuredListId) {
return String.format(
config.getString(Constants.MAILCHIMP_ADD_EMAIL_URL),
Expand Down

0 comments on commit 77c5c9d

Please sign in to comment.