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 @@ -21,6 +21,7 @@
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import static org.eclipse.tractusx.traceability.common.config.TimeConfig.SYSTEM_TIMEZONE;

Expand All @@ -41,4 +42,13 @@ public static OffsetDateTime toOffsetDateTime(Instant instant) {
return null;
}
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;

public static String convertInstantToString(Instant instant) {
if (instant == null) {
return null;
}
return formatter.format(instant);
}

}
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 @@ -38,12 +38,12 @@ public class NotificationFieldMapper extends BaseRequestFieldMapper {
Map.entry("close", "closeReason"),
Map.entry("accept", "acceptReason"),
Map.entry("decline", "declineReason"),
Map.entry("severity", "messages_severity"),
Map.entry("severity", "severity"),
Map.entry("createdBy", "messages_createdBy"),
Map.entry("createdByName", "messages_createdByName"),
Map.entry("sendTo", "messages_sendTo"),
Map.entry("sendToName", "messages_sendToName"),
Map.entry("targetDate", "messages_targetDate"),
Map.entry("targetDate", "targetDate"),
Map.entry("assetId", "assets_id"),
Map.entry("title", "title"),
Map.entry("type", "type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
@UtilityClass
public class NotificationMessageMapper {

public static NotificationSeverityResponse from(NotificationSeverity notificationSeverity) {
return NotificationSeverityResponse.fromString(notificationSeverity.getRealName());
public static NotificationSeverityResponse from(NotificationSeverity severity) {
return NotificationSeverityResponse.fromString(severity.getRealName());
}

public static NotificationSideResponse from(NotificationSide side) {
Expand All @@ -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,13 @@ 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(notification.getSeverity() != null ?
NotificationSeverityResponse.fromString(notification.getSeverity().getRealName()) :
null)
.targetDate(notification.getTargetDate())
.messages(fromNotifications(notification.getNotifications()))
.build();
}
Expand All @@ -81,21 +77,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 @@ -40,6 +40,7 @@

import static java.util.stream.Collectors.groupingBy;
import static org.apache.commons.collections4.ListUtils.emptyIfNull;
import static org.eclipse.tractusx.traceability.common.date.DateUtil.convertInstantToString;

@Data
@Builder(toBuilder = true)
Expand All @@ -55,21 +56,24 @@ 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 severity;
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, Instant targetDate) {

return Notification.builder()
.title(title)
.bpn(bpn)
.notificationStatus(NotificationStatus.CREATED)
.notificationSide(NotificationSide.SENDER)
.notificationType(notificationType)
.targetDate(convertInstantToString(targetDate))
.severity(severity)
.description(description)
.createdAt(createDate)
.affectedPartIds(Collections.emptyList())
Expand All @@ -89,8 +93,6 @@ public void createInitialNotifications(List<AssetBase> affectedParts, BPN applic
applicationBPN,
editNotification.getReceiverBpn(),
editNotification.getDescription(),
editNotification.getTargetDate(),
editNotification.getSeverity(),
this.notificationType,
receiverAssetsMap,
applicationBPN.value(),
Expand All @@ -111,8 +113,6 @@ public void createInitialNotifications(List<AssetBase> affectedParts, BPN applic
applicationBPN,
sentToBPN,
editNotification.getDescription(),
editNotification.getTargetDate(),
editNotification.getSeverity(),
this.notificationType,
receiverAssetsMapEntry,
applicationBPN.value(),
Expand All @@ -135,33 +135,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 @@ -68,21 +66,19 @@ public void changeStatusTo(NotificationStatus to) {
this.notificationStatus = to;
}

public static NotificationMessage create(BPN applicationBpn, String receiverBpn, String description, Instant targetDate, NotificationSeverity severity, NotificationType notificationType, Map.Entry<String, List<AssetBase>> asset, String creator, String sendToName) {
public static NotificationMessage create(BPN applicationBpn, String receiverBpn, String description, NotificationType notificationType, Map.Entry<String, List<AssetBase>> asset, String creator, String sendToName) {
final String notificationId = UUID.randomUUID().toString();
final String messageId = UUID.randomUUID().toString();
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 static NotificationSeverity fromString(String str) {
return s;
}
}
throw new IllegalArgumentException("No enum constant " + NotificationSeverity.class.getCanonicalName() + "." + str);
return null;
}

public static NotificationSeverity from(NotificationSeverityRequest notificationSeverityRequest) {
Expand Down
Loading
Loading