diff --git a/build.gradle b/build.gradle index 3494521c0..a96effc2f 100644 --- a/build.gradle +++ b/build.gradle @@ -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") diff --git a/config/application.properties.sample b/config/application.properties.sample index 7f8275719..05de3516a 100644 --- a/config/application.properties.sample +++ b/config/application.properties.sample @@ -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] \ No newline at end of file +a5l.googleMapsAPIkey=[CHANGE ME] +slack.slackWebHook.url=[CHANGE ME] \ No newline at end of file diff --git a/lombok.config b/lombok.config new file mode 100644 index 000000000..189c0bef9 --- /dev/null +++ b/lombok.config @@ -0,0 +1,3 @@ +# This file is generated by the 'io.freefair.lombok' Gradle plugin +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true diff --git a/src/main/java/ch/wisv/areafiftylan/exception/SlackNotificationException.java b/src/main/java/ch/wisv/areafiftylan/exception/SlackNotificationException.java new file mode 100644 index 000000000..dc6588706 --- /dev/null +++ b/src/main/java/ch/wisv/areafiftylan/exception/SlackNotificationException.java @@ -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 . + */ + +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()); + } +} diff --git a/src/main/java/ch/wisv/areafiftylan/products/service/TicketServiceImpl.java b/src/main/java/ch/wisv/areafiftylan/products/service/TicketServiceImpl.java index 85d3967e5..52ba3cb63 100644 --- a/src/main/java/ch/wisv/areafiftylan/products/service/TicketServiceImpl.java +++ b/src/main/java/ch/wisv/areafiftylan/products/service/TicketServiceImpl.java @@ -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 diff --git a/src/main/java/ch/wisv/areafiftylan/utils/SlackNotificationAspect.java b/src/main/java/ch/wisv/areafiftylan/utils/SlackNotificationAspect.java new file mode 100644 index 000000000..d8d658698 --- /dev/null +++ b/src/main/java/ch/wisv/areafiftylan/utils/SlackNotificationAspect.java @@ -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()); + } +}