Skip to content
Snippets Groups Projects
Commit 938e3f63 authored by Otto Visser's avatar Otto Visser
Browse files

Merge branch '33-push-notifications-for-tas-on-new-requests' into 'development'

Push notification to all assistants upon new request

See merge request !26
parents 1062ba70 93e118f9
No related branches found
No related tags found
1 merge request!26Push notification to all assistants upon new request
package nl.tudelft.ewi.queue.service;
import nl.tudelft.ewi.queue.repository.NotificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
......@@ -10,6 +11,8 @@ import nl.tudelft.ewi.queue.model.Request.Type;
import nl.tudelft.ewi.queue.repository.RequestRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class LabService {
......@@ -17,6 +20,12 @@ public class LabService {
@Autowired
private RequestRepository requestRepository;
@Autowired
private NotificationRepository notificationRepository;
@Autowired
private NotificationService notificationService;
/**
* Enqueue student for lab. An exception is thrown if the lab is closed,
* the student is not enrolled, or if the student is already enqueued.
......@@ -40,6 +49,17 @@ public class LabService {
Request request = new Request(student, assignment, room, type, comment, lab);
requestRepository.save(request);
createAndSendNotifications(request.getLab());
}
private void createAndSendNotifications(Lab lab) {
for(User assistant : lab.getCourse().getAssistants()) {
Notification notification = new Notification(assistant, lab.getCourse(), "New request",
"New request for " + lab + " in course " + lab.getCourse(),
100);
notificationService.sendPushNotification(assistant, notification);
}
}
}
package nl.tudelft.ewi.queue.service;
import nl.martijndwars.webpush.PushService;
import nl.tudelft.ewi.queue.factory.NotificationFactory;
import nl.tudelft.ewi.queue.model.Notification;
import nl.tudelft.ewi.queue.model.Subscription;
import nl.tudelft.ewi.queue.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
@Service
public class NotificationService {
@Autowired
private SimpMessageSendingOperations messagingTemplate;
@Autowired
private PushService pushService;
@Autowired
private NotificationFactory notificationFactory;
public void sendPushNotification(User receiver, Notification notification) {
// Update UI through WebSocket
messagingTemplate.convertAndSendToUser(receiver.getUsername(), "/queue/notifications", notification);
// Send push notification
Subscription subscription = receiver.getSubscription();
if (null != subscription) {
try {
pushService.send(notificationFactory.fromSubscription(subscription, notification.toJSON().getBytes()));
} catch (Exception e ) {
e.printStackTrace();
}
}
}
}
......@@ -30,13 +30,7 @@ public class RequestService {
private NotificationRepository notificationRepository;
@Autowired
private NotificationFactory notificationFactory;
@Autowired
private PushService pushService;
@Autowired
private SimpMessageSendingOperations messagingTemplate;
private NotificationService notificationService;
/**
* Get the next request for this assistant/teacher from a lab.
......@@ -80,20 +74,7 @@ public class RequestService {
Notification notification = createNotification(request.getLab(), user, student);
notificationRepository.save(notification);
// Update UI through WebSocket
messagingTemplate.convertAndSendToUser(student.getUsername(), "/queue/notifications", notification);
// Send push notification
Subscription subscription = request.getStudent().getSubscription();
if (null != subscription) {
try {
pushService.send(notificationFactory.fromSubscription(subscription, notification.toJSON().getBytes()));
} catch (Exception e ) {
e.printStackTrace();
}
}
notificationService.sendPushNotification(student, notification);
return Optional.of(request);
}
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment