Skip to content

Commit

Permalink
Slack notifications (#515)
Browse files Browse the repository at this point in the history
* Add AOP dependency and initial Notification Service trial

* Add AOP dependency and initial Notification Service trial

* add slacknotification service that notifies the lanciedev channel of ticket payments and transfers

* change slack notification service to slack notification Aspect

* Update src/main/java/ch/wisv/areafiftylan/exception/SlackNotificationException.java

Co-Authored-By: Martijn Janssen <[email protected]>

* remove slacknotificationexception from aspect execution

* disable slack notifications during testing

* change slacknotification profile to production

* Add conditional annotation for environment value used with slack

* remove whitespace

* remove unnecessary properties in condition on property

Co-authored-by: Martijn Janssen <[email protected]>
  • Loading branch information
julian9499 and martijnjanssen authored Feb 15, 2020
1 parent 461d821 commit 1b95bb3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-json")
compile("org.flywaydb:flyway-core")

compile("org.springframework.boot:spring-boot-starter-aop")
compile("com.google.guava:guava:28.1-jre")
compile("nl.stil4m:mollie-api:2.7.0")
compile("net.logstash.logback:logstash-logback-encoder:4.11")

compile("in.ashwanthkumar:slack-java-webhook:0.0.9")

runtime("org.hsqldb:hsqldb")
runtime("org.postgresql:postgresql")

Expand Down
3 changes: 2 additions & 1 deletion config/application.properties.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/[DATABASENAME]
spring.datasource.username=
spring.datasource.password=
a5l.molliekey=[CHANGE ME]
a5l.googleMapsAPIkey=[CHANGE ME]
a5l.googleMapsAPIkey=[CHANGE ME]
slack.slackWebHook.url=[CHANGE ME]
3 changes: 3 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018 W.I.S.V. 'Christiaan Huygens'
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ch.wisv.areafiftylan.exception;


import java.io.IOException;

public class SlackNotificationException extends AreaFiftyLANException {
public SlackNotificationException(IOException e) {
super("Unable to send a notification to slack:" + e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ch.wisv.areafiftylan.utils;

import ch.wisv.areafiftylan.exception.SlackNotificationException;
import ch.wisv.areafiftylan.products.model.order.Order;
import ch.wisv.areafiftylan.products.model.order.OrderStatus;
import in.ashwanthkumar.slack.webhook.Slack;
import in.ashwanthkumar.slack.webhook.SlackMessage;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Aspect
@Component
@ConditionalOnProperty(
value = "slack")
@Profile("production")
public class SlackNotificationAspect {

private Slack slackApi;

public SlackNotificationAspect(@Value("${slack}") String webHookUrl) {
slackApi = new Slack(webHookUrl);
}

private void sendSlackMessage(String message) {
try {
slackApi.sendToChannel("#lanciedev").push(new SlackMessage(message));
} catch (IOException e) {
throw new SlackNotificationException(e);
}
}

@AfterReturning(pointcut = "execution(* ch.wisv.areafiftylan.products.service.MolliePaymentService.updateStatus(..))", returning = "order")
public void notifyTicketSale(Order order) {
if (order.getStatus() == OrderStatus.PAID) {
sendSlackMessage(order.getTickets().size() + " tickets bought! :tada:");
}
}

@After("execution(* ch.wisv.areafiftylan.products.service.TicketServiceImpl.transferTicket(..))")
public void notifyTicketTransfer() {
sendSlackMessage("A ticket transfer has been performed");
}

@AfterThrowing(pointcut = "execution(* ch.wisv.areafiftylan.exception.*.*(..)) && !execution(* ch.wisv.areafiftylan.exception.SlackNotificationException.*(..))", throwing = "ex")
public void forwardException(Throwable ex) {
sendSlackMessage("An exception has been thrown: " + ex.getMessage());
}
}

0 comments on commit 1b95bb3

Please sign in to comment.