From 719cf20dd9b3cf2d6dd5af2beb177d3962e64c76 Mon Sep 17 00:00:00 2001 From: Rishabh Joshi Date: Thu, 6 Apr 2017 12:03:26 +0530 Subject: [PATCH] Added the missing support to save names along with the email id, to MailChimp. --- app/controllers/IncomingDataController.java | 27 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/IncomingDataController.java b/app/controllers/IncomingDataController.java index d4ee3f2..3e48a82 100644 --- a/app/controllers/IncomingDataController.java +++ b/app/controllers/IncomingDataController.java @@ -89,10 +89,11 @@ public CompletionStage index() { } private CompletionStage 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 { @@ -105,7 +106,8 @@ private CompletionStage 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(); @@ -120,6 +122,23 @@ private CompletionStage 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),