diff --git a/app/controllers/IncomingDataController.java b/app/controllers/IncomingDataController.java index 8495705..d4ee3f2 100644 --- a/app/controllers/IncomingDataController.java +++ b/app/controllers/IncomingDataController.java @@ -123,7 +123,7 @@ private CompletionStage 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) { diff --git a/app/controllers/helpers/MailchimpKeyVerifier.java b/app/controllers/helpers/MailchimpKeyVerifier.java index 749b811..c094423 100644 --- a/app/controllers/helpers/MailchimpKeyVerifier.java +++ b/app/controllers/helpers/MailchimpKeyVerifier.java @@ -45,9 +45,6 @@ public MailchimpKeyVerifier(WSClient ws, ConfigurationProvider configProvider) { /** * Verify we have all the data and complete installation. - * - * @param installation - * @return */ Optional verifyAndCompleteInstallation(Installation installation, Messages messages) { Map> prefsMap = new HashMap<>(); @@ -71,7 +68,7 @@ Optional 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) { @@ -85,7 +82,7 @@ Optional 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); } } diff --git a/app/controllers/helpers/NewInstallationHelper.java b/app/controllers/helpers/NewInstallationHelper.java index 5bf3c7a..01254f1 100644 --- a/app/controllers/helpers/NewInstallationHelper.java +++ b/app/controllers/helpers/NewInstallationHelper.java @@ -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; @@ -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); @@ -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 getValidationErrors(Data data, Messages messages) { Map errors = new HashMap<>(); if (!StringUtils.hasText(data.getId())) { diff --git a/app/daos/GenericDao.java b/app/daos/GenericDao.java index c46338a..06dda5e 100644 --- a/app/daos/GenericDao.java +++ b/app/daos/GenericDao.java @@ -42,12 +42,12 @@ interface GenericDao { 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. diff --git a/app/daos/impl/InstallationDaoImpl.java b/app/daos/impl/InstallationDaoImpl.java index 882ef7b..c0c8e05 100644 --- a/app/daos/impl/InstallationDaoImpl.java +++ b/app/daos/impl/InstallationDaoImpl.java @@ -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 diff --git a/app/utils/InstallationUtility.java b/app/utils/InstallationUtility.java index cba6e4e..a932dc8 100644 --- a/app/utils/InstallationUtility.java +++ b/app/utils/InstallationUtility.java @@ -20,7 +20,7 @@ public Optional getConfiguredListId(Installation installation) { Optional 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); } } diff --git a/build.sbt b/build.sbt index 1860834..0a05a1c 100644 --- a/build.sbt +++ b/build.sbt @@ -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", @@ -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") @@ -34,12 +32,10 @@ sources in (Compile, doc) := Seq.empty publishArtifact in (Compile, packageDoc) := false -maintainer in Linux := "Rishabh Joshi " - -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"