Skip to content

Commit

Permalink
Metodos de serviços para envio de notificação
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldatabox committed Aug 7, 2020
1 parent 861ace8 commit 2812aed
Show file tree
Hide file tree
Showing 28 changed files with 445 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package br.com.muttley.hermes.api;

import br.com.muttley.feign.autoconfig.FeignTimeoutConfig;
import br.com.muttley.model.hermes.notification.onesignal.Notification;
import br.com.muttley.security.infra.server.FeignClientConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
* @author Joel Rodrigues Moreira on 06/08/2020.
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
@FeignClient(value = "${muttley.hermes.server.name}", path = "/api/v1/tokens-notification", configuration = {FeignClientConfig.class, FeignTimeoutConfig.class})
public interface NotificationClient {
@RequestMapping(method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotification(@RequestBody final Notification notification);

@RequestMapping(value = "/{playerId}", method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotification(@PathVariable("playerId") final String playerId, @RequestBody final Notification notification);

@RequestMapping(value = "/send-by-user/{userId}", method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotificationByUserId(@PathVariable("userId") final String userId, @RequestBody final Notification notification);

@RequestMapping(value = "/send-by-mobile-user/{userId}", method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotificationMobileByUser(@PathVariable("userId") final String userId, @RequestBody final Notification notification);

@RequestMapping(value = "/simple-send-by-user/{userId}", method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotificationByUser(@PathVariable("userId") final String userId, @PathVariable(value = "heading", required = false) final String heading, @PathVariable(value = "subtitle", required = false) final String subtitle, @PathVariable(value = "content", required = false) final String content);

@RequestMapping(value = "/simple-send-by-mobile-user/{userId}", method = POST, consumes = APPLICATION_JSON_VALUE)
public void sendNotificationMobileByUserId(@PathVariable("userId") final String userId, @PathVariable(value = "heading", required = false) final String heading, @PathVariable(value = "subtitle", required = false) final String subtitle, @PathVariable(value = "content", required = false) final String content);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.com.muttley.hermes.api;

import br.com.muttley.feign.autoconfig.FeignTimeoutConfig;
import br.com.muttley.model.hermes.notification.TokenId;
import br.com.muttley.security.infra.server.FeignClientConfig;
import org.springframework.cloud.openfeign.FeignClient;
Expand All @@ -14,7 +15,7 @@
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
@FeignClient(value = "${muttley.hermes.server.name}", path = "/api/v1/tokens-notification", configuration = FeignClientConfig.class)
@FeignClient(value = "${muttley.hermes.server.name}", path = "/api/v1/tokens-notification", configuration = {FeignClientConfig.class, FeignTimeoutConfig.class})
public interface UserTokenNotificationClient {

@RequestMapping(method = POST, consumes = APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package br.com.muttley.hermes.server.autoconfig;
package br.com.muttley.hermes.server.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

Expand All @@ -11,7 +10,7 @@
*/
@Configuration
@ComponentScan(basePackages = {
"br.com.muttley.hermes.server.autoconfig.mongo",
"br.com.muttley.hermes.server.config.mongo",
"br.com.muttley.hermes.server.controller",
"br.com.muttley.hermes.server.service"
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.muttley.hermes.server.autoconfig.mongo;
package br.com.muttley.hermes.server.config.mongo;

import br.com.muttley.mongo.repository.impl.SimpleTenancyMongoRepositoryImpl;
import org.springframework.boot.autoconfigure.domain.EntityScan;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package br.com.muttley.hermes.server.controller;

import br.com.muttley.hermes.server.service.NotificationService;
import br.com.muttley.model.hermes.notification.onesignal.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
* @author Joel Rodrigues Moreira on 06/08/2020.
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
@RestController
@RequestMapping(value = "/api/v1/notifications", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
public class NotificationController {

private final NotificationService notificationService;

@Autowired
public NotificationController(final NotificationService notificationService) {
this.notificationService = notificationService;
}

@RequestMapping(method = POST)
public void sendNotification(@RequestBody final Notification notification) {
this.notificationService.sendNotification(notification);
}

@RequestMapping(value = "/{playerId}", method = POST)
public void sendNotification(@PathVariable("playerId") final String playerId, @RequestBody final Notification notification) {
this.notificationService.sendNotification(notification.addPlayers(playerId));
}

@RequestMapping(value = "/send-by-user/{userId}", method = POST)
public void sendNotificationByUserId(@PathVariable("userId") final String userId, @RequestBody final Notification notification) {
this.notificationService.sendNotificationByUserId(userId, notification);
}

@RequestMapping(value = "/send-by-mobile-user/{userId}", method = POST)
public void sendNotificationMobileByUser(@PathVariable("userId") final String userId, @RequestBody final Notification notification) {
this.notificationService.sendNotificationMobileByUserId(userId, notification);
}

@RequestMapping(value = "/simple-send-by-user/{userId}", method = POST)
public void sendNotificationByUser(@PathVariable("userId") final String userId, @PathVariable(value = "heading", required = false) final String heading, @PathVariable(value = "subtitle", required = false) final String subtitle, @PathVariable(value = "content", required = false) final String content) {
this.notificationService.sendNotificationByUserId(userId, heading, subtitle, content);
}

@RequestMapping(value = "/simple-send-by-mobile-user/{userId}", method = POST)
public void sendNotificationMobileByUserId(@PathVariable("userId") final String userId, @PathVariable(value = "heading", required = false) final String heading, @PathVariable(value = "subtitle", required = false) final String subtitle, @PathVariable(value = "content", required = false) final String content) {
this.notificationService.sendNotificationMobileByUserId(userId, heading, subtitle, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @project muttley-cloud
*/
@RestController
@RequestMapping(value = "/api/v1/tokens-notification", produces = APPLICATION_JSON_VALUE)
@RequestMapping(value = "/api/v1/tokens-notification", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
public class UserTokenNotificationController implements RestResource {
private final AuthService authService;
private final UserTokensNotificationService service;
Expand All @@ -32,7 +32,7 @@ public UserTokenNotificationController(final AuthService authService, final User
this.service = service;
}

@RequestMapping(method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@RequestMapping(method = POST)
@ResponseStatus(CREATED)
public void save(@RequestBody TokenId tokenId) {
this.service.addTokenNotification(this.authService.getCurrentUser(), tokenId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
package br.com.muttley.hermes.server.listeners;

import br.com.muttley.notification.onesignal.model.Content;
import br.com.muttley.notification.onesignal.model.Notification;
import br.com.muttley.notification.onesignal.model.NotificationData;
import br.com.muttley.notification.onesignal.model.events.NotificationEvent;
import br.com.muttley.model.hermes.notification.onesignal.events.NotificationEvent;
import br.com.muttley.notification.onesignal.service.OneSignalNotificationService;
import br.com.muttley.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import static br.com.muttley.notification.onesignal.model.MuttleyLanguage.English;

/**
* @author Joel Rodrigues Moreira on 05/08/2020.
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
@Component
public class NotificationEventListener implements ApplicationListener<NotificationEvent> {
private static final String KEY_REDIS = "muttley-notification-cache";
private final RedisService redisService;
private final OneSignalNotificationService oneSignalService;

@Autowired
public NotificationEventListener(final RedisService redisService, final OneSignalNotificationService oneSignalService) {
this.redisService = redisService;
public NotificationEventListener(final OneSignalNotificationService oneSignalService) {
this.oneSignalService = oneSignalService;
this.oneSignalService.sendNotification(new Notification().setAppId("f9f00ebb-1d73-4e6b-a127-b6a086f48111")
.addPlayers("6c9e98ff-2542-4278-848b-1310abd57b04")
.setData(new NotificationData().setType("asdfasd").setPayload("asdfasdf"))
.addContent(new Content(English, "English"))
.addHeadings(new Content(English, "headings"))
.addSubtitles(new Content(English, "subtitles"))
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import br.com.muttley.model.hermes.notification.UserTokensNotification;
import br.com.muttley.model.security.User;
import br.com.muttley.model.security.UserView;
import br.com.muttley.mongo.repository.SimpleTenancyMongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

/**
Expand All @@ -13,4 +15,10 @@
@Repository
public interface UserTokensNotificationRepository extends SimpleTenancyMongoRepository<UserTokensNotification> {
UserTokensNotification findByUser(final User user);

@Query("{'user': {'$ref' : ?#{@documentNameConfig.getNameCollectionUser()}, '$id': ?#{[0].getId()}}")
UserTokensNotification findByUser(final UserView user);

@Query("{'user': {'$ref' : ?#{@documentNameConfig.getNameCollectionUser()}, '$id': ?1}}")
UserTokensNotification findByUser(final String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package br.com.muttley.hermes.server.service;

import br.com.muttley.model.hermes.notification.onesignal.Content;
import br.com.muttley.model.hermes.notification.onesignal.Notification;
import br.com.muttley.model.security.UserView;

/**
* @author Joel Rodrigues Moreira on 06/08/2020.
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
public interface NotificationService {
void sendNotification(final Notification notification);

void sendNotification(final String playerId, final Notification notification);

void sendNotification(final UserView user, final Notification notification);

void sendNotificationByUserId(final String userId, final Notification notification);

void sendNotificationMobile(final UserView user, final Notification notification);

void sendNotificationMobileByUserId(final String userId, final Notification notification);

void sendNotification(final UserView user, final Content headings, final Content subtitles, final Content contents);

void sendNotificationByUserId(final String userId, final Content headings, final Content subtitles, final Content contents);

void sendNotificationMobile(final UserView user, final Content headings, final Content subtitles, final Content contents);

void sendNotificationMobileByUserId(final String userId, final Content headings, final Content subtitles, final Content contents);

void sendNotification(final UserView user, final String heading, final String subtitle, final String content);

void sendNotificationByUserId(final String userId, final String heading, final String subtitle, final String content);

void sendNotificationMobile(final UserView user, final String heading, final String subtitle, final String content);

void sendNotificationMobileByUserId(final String userId, final String heading, final String subtitle, final String content);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package br.com.muttley.hermes.server.service;

import br.com.muttley.domain.Service;
import br.com.muttley.exception.throwables.MuttleyNotFoundException;
import br.com.muttley.model.hermes.notification.TokenId;
import br.com.muttley.model.hermes.notification.UserTokensNotification;
import br.com.muttley.model.security.User;
import br.com.muttley.model.security.UserView;

/**
* @author Joel Rodrigues Moreira on 04/08/2020.
* e-mail: <a href="mailto:[email protected]">[email protected]</a>
* @project muttley-cloud
*/
public interface UserTokensNotificationService extends Service<UserTokensNotification> {
UserTokensNotification findByUser(final User user);
UserTokensNotification findByUser(final User user) throws MuttleyNotFoundException;

UserTokensNotification findByUser(final UserView user) throws MuttleyNotFoundException;

UserTokensNotification findByUser(final String userId) throws MuttleyNotFoundException;

void addTokenNotification(final User user, final TokenId tokenId);
}
Loading

0 comments on commit 2812aed

Please sign in to comment.