diff --git a/pom.xml b/pom.xml index 394ea12..6a39c0b 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ 0.8.2 3.1.0 1.7 - 0.6.2 + 0.6.12 20150319 diff --git a/src/main/java/com/hello/suripu/admin/SuripuAdmin.java b/src/main/java/com/hello/suripu/admin/SuripuAdmin.java index d5cf796..a747049 100644 --- a/src/main/java/com/hello/suripu/admin/SuripuAdmin.java +++ b/src/main/java/com/hello/suripu/admin/SuripuAdmin.java @@ -47,6 +47,7 @@ import com.hello.suripu.admin.resources.v1.TimelineResources; import com.hello.suripu.admin.resources.v1.TokenResources; import com.hello.suripu.admin.resources.v1.WifiResources; +import com.hello.suripu.admin.resources.v2.AccountAdminResources; import com.hello.suripu.core.configuration.DynamoDBTableName; import com.hello.suripu.core.configuration.QueueName; import com.hello.suripu.core.db.AccountDAO; @@ -457,6 +458,7 @@ public void run(SuripuAdminConfiguration configuration, Environment environment) environment.jersey().register(new WifiResources(wifiInfoDAO)); environment.jersey().register(new KeyStoreResources(senseKeyStore, pillKeyStore)); environment.jersey().register(new DBResource(sensorsTableDAO)); + environment.jersey().register(new AccountAdminResources(accountDAO)); environment.jersey().register(new FeedbackResources(feedbackDAO, accountDAO)); } diff --git a/src/main/java/com/hello/suripu/admin/models/AccountAdminView.java b/src/main/java/com/hello/suripu/admin/models/AccountAdminView.java new file mode 100644 index 0000000..5edd34f --- /dev/null +++ b/src/main/java/com/hello/suripu/admin/models/AccountAdminView.java @@ -0,0 +1,25 @@ +package com.hello.suripu.admin.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.hello.suripu.core.models.Account; +import org.jetbrains.annotations.NotNull; + +public class AccountAdminView { + + @JsonProperty("account") + public final Account account; + + private AccountAdminView(final Account account) { + this.account = account; + } + + @JsonProperty("disabled") + public final Boolean disabled() { + return account.email.startsWith("ADMIN-DISABLED"); + } + + public static AccountAdminView fromAccount(@NotNull final Account account) { + return new AccountAdminView(account); + } + +} diff --git a/src/main/java/com/hello/suripu/admin/resources/v1/NotificationResources.java b/src/main/java/com/hello/suripu/admin/resources/v1/NotificationResources.java new file mode 100644 index 0000000..30cf7db --- /dev/null +++ b/src/main/java/com/hello/suripu/admin/resources/v1/NotificationResources.java @@ -0,0 +1,61 @@ +package com.hello.suripu.admin.resources.v1; + +import com.codahale.metrics.annotation.Timed; +import com.google.common.base.Optional; +import com.hello.suripu.core.db.AccountDAO; +import com.hello.suripu.core.models.Account; +import com.hello.suripu.core.notifications.HelloPushMessage; +import com.hello.suripu.core.notifications.MobilePushNotificationProcessor; +import com.hello.suripu.core.oauth.AccessToken; +import com.hello.suripu.core.oauth.OAuthScope; +import com.hello.suripu.coredw8.oauth.Auth; +import com.hello.suripu.coredw8.oauth.ScopesAllowed; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +public class NotificationResources { + + + private static final Logger LOGGER = LoggerFactory.getLogger(NotificationResources.class); + + private final AccountDAO accountDAO; + private final MobilePushNotificationProcessor pushNotificationProcessor; + + public NotificationResources(final AccountDAO accountDAO, MobilePushNotificationProcessor pushNotificationProcessor) { + this.accountDAO = accountDAO; + this.pushNotificationProcessor = pushNotificationProcessor; + } + + + @ScopesAllowed({OAuthScope.ADMINISTRATION_READ}) + @POST + @Path("/send/{email}") + @Timed + @Consumes(MediaType.APPLICATION_JSON) + public Response send( + @Auth final AccessToken accessToken, + @PathParam("email") final String email, + @Valid final HelloPushMessage message) { + + LOGGER.debug("email: {}", email); + LOGGER.debug("push target: {}", message.target); + LOGGER.debug("push details: {}", message.details); + LOGGER.debug("push body: {}", message.body); + + final Optional accountOptional = accountDAO.getByEmail(email); + if(!accountOptional.isPresent()) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + + pushNotificationProcessor.push(accountOptional.get().id.get(), message); + return Response.ok().build(); + } +} diff --git a/src/main/java/com/hello/suripu/admin/resources/v2/AccountAdminResources.java b/src/main/java/com/hello/suripu/admin/resources/v2/AccountAdminResources.java new file mode 100644 index 0000000..072cd2e --- /dev/null +++ b/src/main/java/com/hello/suripu/admin/resources/v2/AccountAdminResources.java @@ -0,0 +1,56 @@ +package com.hello.suripu.admin.resources.v2; + + +import com.google.common.base.Optional; +import com.hello.suripu.admin.models.AccountAdminView; +import com.hello.suripu.core.db.AccountDAO; +import com.hello.suripu.core.models.Account; +import com.hello.suripu.core.oauth.OAuthScope; +import com.hello.suripu.coredw8.oauth.AccessToken; +import com.hello.suripu.coredw8.oauth.Auth; +import com.hello.suripu.coredw8.oauth.ScopesAllowed; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/v2/account") +public class AccountAdminResources { + + private static final Logger LOGGER = LoggerFactory.getLogger(AccountAdminResources.class); + + public final AccountDAO accountDAO; + + public AccountAdminResources(final AccountDAO accountDAO) { + this.accountDAO = accountDAO; + } + + @ScopesAllowed({OAuthScope.ADMINISTRATION_READ}) + @GET + @Produces(MediaType.APPLICATION_JSON) + public AccountAdminView retrieveAccountByEmailOrId(@Auth final AccessToken accessToken, + @QueryParam("email") final String email, + @QueryParam("id") final Long id) { + if (email == null && id == null) { + throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND) + .entity("Missing query params, please specify email or id").build()); + } + + final Optional account = (email !=null) + ? accountDAO.getByEmail(email.toLowerCase()) + : accountDAO.getById(id); + + if (!account.isPresent()) { + LOGGER.warn("email={} id={} result=missing", email, id); + throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity("Account not found").build()); + } + + return AccountAdminView.fromAccount(account.get()); + } +}