Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/962 notification model update #1040

Merged
merged 10 commits into from
Jun 19, 2024
8,143 changes: 1 addition & 8,142 deletions docs/api/traceability-foss-backend.json

Large diffs are not rendered by default.

8,143 changes: 1 addition & 8,142 deletions tx-backend/openapi/traceability-foss-backend.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ public class NotificationMessageMapper {
* @param edcNotification the EDCNotification received by the receiver
* @return a Notification object representing the notification received by the receiver
*/
public NotificationMessage toNotification(EDCNotification edcNotification, NotificationType type) {
public NotificationMessage toNotificationMessage(EDCNotification edcNotification, NotificationType type) {
String notificationId = UUID.randomUUID().toString();
return NotificationMessage.builder()
.id(notificationId)
.created(LocalDateTime.now())
.notificationReferenceId(edcNotification.getNotificationId())
.createdBy(edcNotification.getSenderBPN())
.createdByName(getManufacturerName(edcNotification.getSenderBPN()))
.sentBy(edcNotification.getSenderBPN())
.sentByName(getManufacturerName(edcNotification.getSenderBPN()))
.type(type)
.sendTo(edcNotification.getRecipientBPN())
.sentTo(edcNotification.getRecipientBPN())
.sendToName(getManufacturerName(edcNotification.getRecipientBPN()))
.description(edcNotification.getInformation())
.message(edcNotification.getInformation())
.notificationStatus(edcNotification.convertNotificationStatus())
.affectedParts(emptyIfNull(edcNotification.getListOfAffectedItems()))
.targetDate(edcNotification.getTargetDate())
.severity(NotificationSeverity.fromString(edcNotification.getSeverity()))
.edcNotificationId(edcNotification.getNotificationId())
.messageId(edcNotification.getMessageId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,16 @@ public static NotificationMessageResponse fromNotification(NotificationMessage n
return NotificationMessageResponse
.builder()
.id(notificationMessage.getId())
.severity(notificationMessage.getSeverity() != null ? NotificationSeverityResponse.fromString(notificationMessage.getSeverity().getRealName()) : null)
.notificationReferenceId(notificationMessage.getNotificationReferenceId())
.edcNotificationId(notificationMessage.getEdcNotificationId())
.contractAgreementId(notificationMessage.getContractAgreementId())
.notificationReferenceId(notificationMessage.getNotificationReferenceId())
.messageId(notificationMessage.getMessageId())
.updated(notificationMessage.getUpdated())
.sendToName(notificationMessage.getSendToName())
.status(fromStatus(notificationMessage.getNotificationStatus()))
.targetDate(notificationMessage.getTargetDate())
.created(notificationMessage.getCreated())
.createdBy(notificationMessage.getCreatedBy())
.createdByName(notificationMessage.getCreatedByName())
.sendTo(notificationMessage.getSendTo())
.sentBy(notificationMessage.getSentBy())
.sentByName(notificationMessage.getSentByName())
.sendTo(notificationMessage.getSentTo())
.errorMessage(notificationMessage.getErrorMessage())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
package org.eclipse.tractusx.traceability.notification.application.notification.mapper;

import lombok.experimental.UtilityClass;
import notification.response.NotificationResponse;
import notification.response.NotificationSeverityResponse;
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.notification.domain.base.model.Notification;
import org.eclipse.tractusx.traceability.notification.domain.base.model.NotificationMessage;
import org.eclipse.tractusx.traceability.notification.domain.base.model.NotificationSeverity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import notification.response.NotificationReasonResponse;
import notification.response.NotificationResponse;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -54,16 +53,11 @@ public static NotificationResponse from(Notification notification) {
.channel(NotificationMessageMapper.from(notification.getNotificationSide()))
.type(NotificationMessageMapper.from(notification.getNotificationType()))
.title(notification.getTitle())
.reason(new NotificationReasonResponse(
notification.getCloseReason(),
notification.getAcceptReason(),
notification.getDeclineReason()
))
.updatedDate(OffsetDateTime.now().toString())
.sendTo(getReceiverBPN(notification.getNotifications()))
.sendToName(getReceiverName(notification.getNotifications()))
// TODO severity should not be inside the notification it should be in the message
.severity(NotificationMessageMapper.from(notification.getNotifications().stream().findFirst().map(NotificationMessage::getSeverity).orElse(NotificationSeverity.MINOR)))
.targetDate(notification.getNotifications().stream().findFirst().map(NotificationMessage::getTargetDate).map(Instant::toString).orElse(null))
.severity(NotificationSeverityResponse.fromString(notification.getNotificationSeverity().getRealName()))
.targetDate(notification.getTargetDate())
.messages(fromNotifications(notification.getNotifications()))
.build();
}
Expand All @@ -81,21 +75,21 @@ public static PageResult<NotificationResponse> fromAsPageResult(PageResult<Notif
private static String getSenderBPN(Collection<NotificationMessage> notifications) {
return notifications.stream()
.findFirst()
.map(NotificationMessage::getCreatedBy)
.map(NotificationMessage::getSentBy)
.orElse(null);
}

private static String getReceiverBPN(Collection<NotificationMessage> notifications) {
return notifications.stream()
.findFirst()
.map(NotificationMessage::getSendTo)
.map(NotificationMessage::getSentTo)
.orElse(null);
}

private static String getSenderName(Collection<NotificationMessage> notifications) {
return notifications.stream()
.findFirst()
.map(NotificationMessage::getCreatedByName)
.map(NotificationMessage::getSentByName)
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ public class Notification {
private NotificationType notificationType;
@Builder.Default
private List<String> affectedPartIds = new ArrayList<>();
private String closeReason;
private String acceptReason;
private String declineReason;
private NotificationSeverity notificationSeverity;
private String targetDate;

@Getter
@Builder.Default
private List<NotificationMessage> notifications = List.of();


public static Notification startNotification(String title, Instant createDate, BPN bpn, String description, NotificationType notificationType) {
public static Notification startNotification(String title, Instant createDate, BPN bpn, String description, NotificationType notificationType, NotificationSeverity severity) {
return Notification.builder()
.title(title)
.bpn(bpn)
.notificationStatus(NotificationStatus.CREATED)
.notificationSide(NotificationSide.SENDER)
.notificationType(notificationType)
.notificationSeverity(severity)
.description(description)
.createdAt(createDate)
.affectedPartIds(Collections.emptyList())
Expand Down Expand Up @@ -135,33 +136,32 @@ public String getBpn() {
public void cancel(BPN applicationBpn) {
validateBPN(applicationBpn);
changeStatusTo(NotificationStatus.CANCELED);
this.closeReason = "canceled";
}

public void close(BPN applicationBpn, String reason) {
public void close(BPN applicationBpn, String reason, NotificationMessage notificationMessage) {
validateBPN(applicationBpn);
changeStatusTo(NotificationStatus.CLOSED);
this.closeReason = reason;
this.notifications.forEach(notification -> notification.setDescription(reason));
notificationMessage.setMessage(reason);
this.notifications.forEach(notification -> notification.setMessage(reason));
}

public void acknowledge() {
changeStatusTo(NotificationStatus.ACKNOWLEDGED);
}

public void accept(String reason) {
public void accept(String reason, NotificationMessage message) {
changeStatusTo(NotificationStatus.ACCEPTED);
this.acceptReason = reason;
message.setMessage(reason);
}

public void decline(String reason) {
public void decline(String reason, NotificationMessage message) {
changeStatusTo(NotificationStatus.DECLINED);
this.declineReason = reason;
message.setMessage(reason);
}

public void close(String reason) {
public void close(String reason, NotificationMessage notificationMessage) {
changeStatusTo(NotificationStatus.CLOSED);
this.closeReason = reason;
notificationMessage.setMessage(reason);
}

public void send(BPN applicationBpn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,19 @@
@Data
public class NotificationMessage {
private String id;
private final String createdByName;
private final String sentByName;
private final String sendToName;
@Builder.Default
private List<NotificationAffectedPart> affectedParts = new ArrayList<>();
private String notificationReferenceId;
private String createdBy;
private String sendTo;
private String sentBy;
private String sentTo;
private String contractAgreementId;
private String description;
private String message;
private NotificationStatus notificationStatus;
private String edcNotificationId;
private LocalDateTime created;
private LocalDateTime updated;
private Instant targetDate;
private NotificationSeverity severity;
private String messageId;
private NotificationType type;
private String errorMessage;
Expand All @@ -74,15 +72,13 @@ public static NotificationMessage create(BPN applicationBpn, String receiverBpn,
return NotificationMessage.builder()
.id(notificationId)
.created(LocalDateTime.now())
.createdBy(applicationBpn.value())
.createdByName(creator)
.sendTo(StringUtils.isBlank(receiverBpn) ? asset.getKey() : receiverBpn)
.sentBy(applicationBpn.value())
.sentByName(creator)
.sentTo(StringUtils.isBlank(receiverBpn) ? asset.getKey() : receiverBpn)
.sendToName(sendToName)
.description(description)
.message(description)
.notificationStatus(NotificationStatus.CREATED)
.affectedParts(asset.getValue().stream().map(AssetBase::getId).map(NotificationAffectedPart::new).toList())
.targetDate(targetDate)
.severity(severity)
.edcNotificationId(notificationId)
.type(notificationType)
.messageId(messageId)
Expand All @@ -95,36 +91,34 @@ public static NotificationMessage create(BPN applicationBpn, String receiverBpn,
public NotificationMessage copyAndSwitchSenderAndReceiver(BPN applicationBpn) {
final String notificationId = UUID.randomUUID().toString();
final String messageUUID = UUID.randomUUID().toString();
String receiverBPN = sendTo;
String senderBPN = createdBy;
String receiverBPN = sentTo;
String senderBPN = sentBy;
String receiverName;
String senderName;

// This is needed to make sure that the app can send a message to the receiver and not addresses itself
if (applicationBpn.value().equals(sendTo)) {
receiverBPN = createdBy;
senderBPN = sendTo;
receiverName = createdByName;
if (applicationBpn.value().equals(sentTo)) {
receiverBPN = sentBy;
senderBPN = sentTo;
receiverName = sentByName;
senderName = sendToName;
} else {
receiverName = sendToName;
senderName = createdByName;
senderName = sentByName;
}
return NotificationMessage.builder()
.created(LocalDateTime.now())
.id(notificationId)
.createdBy(senderBPN)
.createdByName(senderName)
.sendTo(receiverBPN)
.sentBy(senderBPN)
.sentByName(senderName)
.sentTo(receiverBPN)
.sendToName(receiverName)
.contractAgreementId(contractAgreementId)
.description(description)
.message(message)
.notificationStatus(notificationStatus)
.affectedParts(affectedParts)
.edcNotificationId(edcNotificationId)
.messageId(messageUUID)
.severity(severity)
.targetDate(targetDate)
.type(type)
.errorMessage(errorMessage)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class AbstractNotificationReceiverService implements Notificatio
@Override
public void handleReceive(EDCNotification edcNotification, NotificationType notificationType) {
BPN investigationCreatorBPN = BPN.of(edcNotification.getSenderBPN());
NotificationMessage notification = getNotificationMessageMapper().toNotification(edcNotification, notificationType);
NotificationMessage notification = getNotificationMessageMapper().toNotificationMessage(edcNotification, notificationType);
Notification investigation = getNotificationMapper().toNotification(investigationCreatorBPN, edcNotification.getInformation(), notification, notificationType);
NotificationId investigationId = getRepository().saveNotification(investigation);
log.info("Stored received edcNotification in investigation with id {}", investigationId);
Expand All @@ -58,15 +58,15 @@ public void handleUpdate(EDCNotification edcNotification, NotificationType notif

Notification notification = getRepository().findByEdcNotificationId(edcNotification.getNotificationId())
.orElseThrow(() -> getNotFoundException(edcNotification.getNotificationId()));
NotificationMessage notificationMessage = getNotificationMessageMapper().toNotification(edcNotification, notificationType);
NotificationMessage notificationMessage = getNotificationMessageMapper().toNotificationMessage(edcNotification, notificationType);
emptyIfNull(notification.getNotifications()).stream().findFirst().ifPresent(notificationMessage1 -> notificationMessage.setAffectedParts(notificationMessage1.getAffectedParts()));

switch (edcNotification.convertNotificationStatus()) {
case ACKNOWLEDGED -> notification.acknowledge();
case ACCEPTED -> notification.accept(edcNotification.getInformation());
case DECLINED -> notification.decline(edcNotification.getInformation());
case ACCEPTED -> notification.accept(edcNotification.getInformation(), notificationMessage);
case DECLINED -> notification.decline(edcNotification.getInformation(), notificationMessage);
case CLOSED ->
notification.close(BPN.of(notification.getBpn()), edcNotification.getInformation());
notification.close(BPN.of(notification.getBpn()), edcNotification.getInformation(), notificationMessage);
default ->
throw getIllegalUpdateException("Failed to handle notification due to unhandled %s status".formatted(edcNotification.convertNotificationStatus()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void updateStatusTransition(Long notificationId, NotificationStatus notif
NotificationMessage notificationMessageSwitchedSenderAndReceiver = notificationMessage.copyAndSwitchSenderAndReceiver(traceabilityProperties.getBpn());
notificationMessageSwitchedSenderAndReceiver.setId(UUID.randomUUID().toString());
notificationMessageSwitchedSenderAndReceiver.changeStatusTo(notificationStatus);
notificationMessageSwitchedSenderAndReceiver.setDescription(reason);
notificationMessageSwitchedSenderAndReceiver.setMessage(reason);
notification.addNotificationMessage(notificationMessageSwitchedSenderAndReceiver);
});

Expand Down Expand Up @@ -135,9 +135,11 @@ public void editNotification(EditNotification editNotification) {
if (editNotification.getAffectedPartIds() != null) {
notification.setAffectedPartIds(editNotification.getAffectedPartIds());
}
if (editNotification.getSeverity() != null){
notification.setNotificationSeverity(editNotification.getSeverity());
}


getNotificationRepository().updateNotificationAndMessage(notification, editNotification.getSeverity());
getNotificationRepository().updateNotificationAndMessage(notification);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class EdcNotificationServiceImpl implements EdcNotificationService {
public CompletableFuture<NotificationMessage> asyncNotificationMessageExecutor(NotificationMessage message) {
log.info("::asyncNotificationExecutor::message {}", message);
try {
Discovery discovery = discoveryService.getDiscoveryByBPN(message.getSendTo());
Discovery discovery = discoveryService.getDiscoveryByBPN(message.getSentTo());

String senderEdcUrl = discovery.getSenderUrl();
List<String> receiverUrls = emptyIfNull(discovery.getReceiverUrls());
Expand Down
Loading
Loading