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 #4 from rishabh9/bug-fix-email-not-added-to-list
Browse files Browse the repository at this point in the history
Fix for emails not being added to the configured mailchimp mailing list.
  • Loading branch information
rishabh9 authored Apr 4, 2017
2 parents 588eda1 + 7527e1e commit f9c5dcf
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/controllers/IncomingDataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private CompletionStage<Result> executeOrderEvent(JsonNode json, Messages messag
private String getUrl(Installation installation, String configuredListId) {
return String.format(
config.getString(Constants.MAILCHIMP_ADD_EMAIL_URL),
utility.getDataCentre(installation), configuredListId);
utility.getDataCentre(installation).get(), configuredListId);
}

private Result executeInstallationEvents(String event, JsonNode json, Messages messages) {
Expand Down
7 changes: 2 additions & 5 deletions app/controllers/helpers/MailchimpKeyVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public MailchimpKeyVerifier(WSClient ws, ConfigurationProvider configProvider) {

/**
* Verify we have all the data and complete installation.
*
* @param installation
* @return
*/
Optional<Result> verifyAndCompleteInstallation(Installation installation, Messages messages) {
Map<String, List<Value>> prefsMap = new HashMap<>();
Expand All @@ -71,7 +68,7 @@ Optional<Result> verifyAndCompleteInstallation(Installation installation, Messag
return Optional.empty();
} else {
log.info("The provided Mailchimp API key doesn't seems to be valid one. Installation {}",
installation.getId().toHexString());
installation.getInstallationId());
return returnMailchimpApiKeyError(messages, MessageKey.MC_API_KEY_INVALID);
}
} catch (InterruptedException | ExecutionException e) {
Expand All @@ -85,7 +82,7 @@ Optional<Result> verifyAndCompleteInstallation(Installation installation, Messag
return returnMailchimpApiKeyError(messages, MessageKey.MC_API_KEY_INVALID);
}
} else {
log.error("Mailchimp API key is null/empty/invalid for installation {}", installation.getId().toHexString());
log.error("Mailchimp API key is null/empty/invalid for installation {}", installation.getInstallationId());
return returnMailchimpApiKeyError(messages, MessageKey.MC_API_KEY_INVALID);
}
}
Expand Down
26 changes: 20 additions & 6 deletions app/controllers/helpers/NewInstallationHelper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package controllers.helpers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.WriteResult;
import daos.InstallationDao;
import models.Installation;
import models.payload.Data;
import org.bson.types.ObjectId;
import org.springframework.util.StringUtils;
import play.Logger;
import play.i18n.Messages;
Expand Down Expand Up @@ -48,9 +49,11 @@ public Result newInstall(Data data, Messages messages) {
return badRequest(ErrorUtil.toJson(BAD_REQUEST, messages.at(INVALID_JSON), validationErrors));
}
Installation installation = installationDao.getByUserId(data.getUser().getId());
boolean isUpdate = false;
if (null != installation) {
log.debug("Found an existing entry for user {}", data.getUser().getId());
copyOverData(data, installation);
isUpdate = true;
} else {
log.debug("Brand new user {}!", data.getUser().getId());
installation = createInstallationFromData(data);
Expand All @@ -62,18 +65,29 @@ public Result newInstall(Data data, Messages messages) {
return maybeValidationError.get();
}

WriteResult result = installationDao.insert(installation);
WriteResult result;
if (isUpdate) {
result = installationDao.update(installation);
} else {
result = installationDao.insert(installation);
}
if (result.wasAcknowledged()) {
ObjectId installationId = result.getUpsertedId() == null ? installation.getId() : (ObjectId) result.getUpsertedId();
Installation inst = installationDao.get(installationId);
log.info("Installation {} updated", inst.getId());
return ok(Json.toJson(inst));
log.info("Installation {}: {}", isUpdate ? "updated" : "created", installation.getId());
return ok(wrap(installation));
} else {
log.error("Error while persisting installation. {}", result.toString());
return internalServerError(ErrorUtil.toJson(INTERNAL_SERVER_ERROR, messages.at(UNEXPECTED_ERROR)));
}
}

private JsonNode wrap(Installation installation) {
ObjectNode node = Json.newObject();
node.put("installationId", installation.getInstallationId());
node.set("user", Json.toJson(installation.getUser()));
node.set("preferences", Json.toJson(installation.getPreferences()));
return node;
}

private Map<String, String> getValidationErrors(Data data, Messages messages) {
Map<String, String> errors = new HashMap<>();
if (!StringUtils.hasText(data.getId())) {
Expand Down
6 changes: 3 additions & 3 deletions app/daos/GenericDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ interface GenericDao<T, PK extends ObjectId> {
WriteResult insert(T object);

/**
* Generic method to delete a document.
* Generic method to update a document.
*
* @param object the document to delete.
* @param object the document to update.
* @return the result of the operation as a {@link WriteResult} object.
*/
WriteResult delete(T object);
WriteResult update(T object);

/**
* Generic method to delete a document.
Expand Down
8 changes: 4 additions & 4 deletions app/daos/impl/InstallationDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public boolean exists(ObjectId id) {
}

@Override
public WriteResult insert(Installation data) {
return installations().save(data);
public WriteResult insert(Installation installation) {
return installations().insert(installation);
}

@Override
public WriteResult delete(Installation data) {
return installations().remove(data.getId());
public WriteResult update(Installation installation) {
return installations().update(installation.getId()).with(installation);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/utils/InstallationUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Optional<String> getConfiguredListId(Installation installation) {
Optional<String> value = Optional.empty();
for (Preference preference : installation.getPreferences()) {
if (preference.getKey().equals(CONFIG_MC_LIST_ID)) {
String label = (String) preference.getValues().get(0).getLabel();
String label = preference.getValues().get(0).getId();
value = Optional.ofNullable(label);
}
}
Expand Down
12 changes: 4 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ name := """xola-mailchimp"""

version := (version in ThisBuild).value

lazy val root = (project in file(".")).enablePlugins(PlayJava, DebianPlugin)
lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
cache,
javaWs,
"uk.co.panaxiom" %% "play-jongo" % "2.0.0-jongo1.3",
"com.github.rishabh9" %% "play-jongo" % "2.0.1-jongo1.3",
"org.mockito" % "mockito-core" % "1.10.19" % "test",
"org.powermock" % "powermock-module-junit4" % "1.6.6" % "test",
"org.powermock" % "powermock-api-mockito" % "1.6.6" % "test",
Expand All @@ -20,8 +20,6 @@ libraryDependencies ++= Seq(

PlayKeys.externalizeResources := false

javacOptions ++= Seq("-source", "1.8", "-target", "1.8")

initialize := {
val _ = initialize.value
if (sys.props("java.specification.version") != "1.8")
Expand All @@ -34,12 +32,10 @@ sources in (Compile, doc) := Seq.empty

publishArtifact in (Compile, packageDoc) := false

maintainer in Linux := "Rishabh Joshi <[email protected]>"

packageSummary in Linux := "The Mailchimp Integration for Xola"

packageDescription := "The Mailchimp Integration for Xola"

maintainer := "Rishabh Joshi"

javaOptions in Test += "-Dconfig.file=conf/application-test.conf"

javaOptions in Test += "-Dlogger.file=conf/logback-test.xml"
Expand Down

0 comments on commit f9c5dcf

Please sign in to comment.