From b8a80d716d956a178616f9cbb82ac3696c94f89f Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 22 Nov 2022 10:17:38 +0100
Subject: [PATCH] OZG-2966 OZG-3187 OZG-3188 adjust absender for
 outgoing/incoming nachrichten at pdf export

---
 .../common/user/UserManagerUrlProvider.java   |  9 +++
 .../goofy/postfach/PostfachMailService.java   | 16 +++--
 .../postfach/PostfachNachrichtPdfData.java    |  2 +
 .../postfach/PostfachNachrichtPdfService.java | 49 ++++++++++++---
 .../common/user/UserProfileTestFactory.java   |  2 +
 .../postfach/PostfachMailServiceTest.java     | 50 ++++++++++++++-
 .../PostfachNachrichtPdfServiceTest.java      | 61 ++++++++++++++++++-
 7 files changed, 175 insertions(+), 14 deletions(-)

diff --git a/goofy-server/src/main/java/de/itvsh/goofy/common/user/UserManagerUrlProvider.java b/goofy-server/src/main/java/de/itvsh/goofy/common/user/UserManagerUrlProvider.java
index 1b4dbecdf0..e6902afed4 100644
--- a/goofy-server/src/main/java/de/itvsh/goofy/common/user/UserManagerUrlProvider.java
+++ b/goofy-server/src/main/java/de/itvsh/goofy/common/user/UserManagerUrlProvider.java
@@ -1,14 +1,22 @@
 package de.itvsh.goofy.common.user;
 
 import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Predicate;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import de.itvsh.goofy.postfach.PostfachMail;
+
 @Service
 public class UserManagerUrlProvider {
 
+	public static final String SYSTEM_USER_IDENTIFIER = "system";
+	public static final Predicate<PostfachMail> SENT_BY_CLIENT_USER = postfachNachricht -> Optional.ofNullable(postfachNachricht.getCreatedBy())
+			.map(createdBy -> !createdBy.toString().startsWith(SYSTEM_USER_IDENTIFIER)).orElse(false);
+
 	@Autowired
 	private UserManagerProperties userManagerProperties;
 
@@ -37,4 +45,5 @@ public class UserManagerUrlProvider {
 	public boolean isConfiguredForInternalUserId() {
 		return Objects.nonNull(StringUtils.trimToNull(userManagerProperties.getInternalurl()));
 	}
+
 }
diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
index af17ed4d8f..16fea2fbb6 100644
--- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
+++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
@@ -28,6 +28,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -38,7 +39,7 @@ import de.itvsh.goofy.common.binaryfile.BinaryFileService;
 import de.itvsh.goofy.common.binaryfile.FileId;
 import de.itvsh.goofy.common.errorhandling.ResourceNotFoundException;
 import de.itvsh.goofy.common.file.OzgFile;
-import de.itvsh.goofy.common.user.UserId;
+import de.itvsh.goofy.common.user.UserManagerUrlProvider;
 import de.itvsh.goofy.common.user.UserProfile;
 import de.itvsh.goofy.common.user.UserService;
 import de.itvsh.goofy.vorgang.VorgangWithEingang;
@@ -117,14 +118,21 @@ class PostfachMailService {
 			Map<FileId, String> ozgFileIdOzgFileMap) {
 		return PostfachNachrichtPdfData.builder()
 				.createdAt(postfachNachricht.getCreatedAt())
-				.user(getUser(postfachNachricht.getCreatedBy()))
+				.user(getUser(postfachNachricht))
+				.direction(postfachNachricht.getDirection())
 				.mailBody(postfachNachricht.getMailBody())
 				.subject(postfachNachricht.getSubject())
 				.attachmentNames(postfachNachricht.getAttachments().stream().map(ozgFileIdOzgFileMap::get).toList())
 				.build();
 	}
 
-	private UserProfile getUser(UserId createdBy) {
-		return Objects.nonNull(createdBy) ? userService.getById(createdBy) : null;
+	UserProfile getUser(PostfachMail postfachNachricht) {
+		var createdBy = postfachNachricht.getCreatedBy();
+		if (UserManagerUrlProvider.SENT_BY_CLIENT_USER.test(postfachNachricht)) {
+			return Optional.ofNullable(userService.getById(createdBy)).orElseGet(() -> null);
+		}
+		return Optional.ofNullable(createdBy)
+				.map(created -> UserProfile.builder().id(created).build())
+				.orElseGet(() -> null);
 	}
 }
\ No newline at end of file
diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfData.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfData.java
index 89a98c4c85..fe208292b2 100644
--- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfData.java
+++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfData.java
@@ -27,6 +27,7 @@ import java.time.ZonedDateTime;
 import java.util.List;
 
 import de.itvsh.goofy.common.user.UserProfile;
+import de.itvsh.goofy.postfach.PostfachMail.Direction;
 import lombok.Builder;
 import lombok.Getter;
 import lombok.Setter;
@@ -41,6 +42,7 @@ class PostfachNachrichtPdfData {
 
 	private String subject;
 	private String mailBody;
+	private Direction direction;
 
 	private List<String> attachmentNames;
 
diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfService.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfService.java
index 59eb71fe00..1e0366f18d 100644
--- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfService.java
+++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfService.java
@@ -27,14 +27,17 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.time.format.DateTimeFormatter;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Stream;
 
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
 import org.springframework.stereotype.Service;
 
+import de.itvsh.goofy.common.user.UserManagerUrlProvider;
 import de.itvsh.goofy.common.user.UserProfile;
 import de.itvsh.goofy.postfach.PostfachMail.Direction;
 import de.itvsh.goofy.vorgang.Antragsteller;
@@ -47,10 +50,13 @@ class PostfachNachrichtPdfService {
 
 	static final String PDF_TEMPLATE_PATH = "classpath:fop/postfach-nachrichten.xsl";
 
+	static final String SYSTEM_NACHRICHT_NAME = StringUtils.EMPTY;
 	static final String FALLBACK_USER_NAME = "Unbekannter Benutzer";
 
+	static final String FALLBACK_ANTRAGSTELLER_NAME = "Antragsteller";
+
 	// TODO Auf Konstante mit Locale umstellen
-	private static final DateTimeFormatter CREATED_AT_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");
+	private static final DateTimeFormatter CREATED_AT_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm:ss");
 
 	@Autowired
 	private PdfService pdfService;
@@ -77,8 +83,7 @@ class PostfachNachrichtPdfService {
 
 		Optional.ofNullable(vorgang.getEingang().getAntragsteller()).ifPresent(antragsteller -> mapAntragsteller(pdfModelBuilder, antragsteller));
 
-		isFirstNachricht = true;
-		pdfModelBuilder.nachrichten(postfachNachrichten.map(this::mapPostfachNachricht).toList());
+		mapNachrichten(pdfModelBuilder, postfachNachrichten, vorgang.getEingang().getAntragsteller());
 
 		return pdfModelBuilder.build();
 	}
@@ -93,13 +98,21 @@ class PostfachNachrichtPdfService {
 				.antragstellerOrt(antragsteller.getOrt());
 	}
 
-	PostfachNachrichtPdfModel.Nachricht mapPostfachNachricht(PostfachNachrichtPdfData postfachMail) {
+	private void mapNachrichten(PostfachNachrichtPdfModel.PostfachNachrichtPdfModelBuilder pdfModelBuilder,
+			Stream<PostfachNachrichtPdfData> postfachNachrichten, Antragsteller antragsteller) {
+		isFirstNachricht = true;
+
+		pdfModelBuilder.nachrichten(postfachNachrichten.map(nachricht -> mapPostfachNachricht(nachricht, antragsteller)).toList());
+
+	}
+
+	PostfachNachrichtPdfModel.Nachricht mapPostfachNachricht(PostfachNachrichtPdfData postfachMail, Antragsteller antragsteller) {
 		return PostfachNachrichtPdfModel.Nachricht.builder()
 				.isFirst(isFirstNachricht())
 				.subject(postfachMail.getSubject())
 				.mailBody(postfachMail.getMailBody())
 				.createdAt(CREATED_AT_FORMATTER.format(postfachMail.getCreatedAt()))
-				.createdBy(buildUserName(postfachMail.getUser(), postfachMail.getDirection()))
+				.createdBy(buildAbsenderName(postfachMail, antragsteller))
 				.attachments(postfachMail.getAttachmentNames())
 				.build();
 	}
@@ -112,11 +125,33 @@ class PostfachNachrichtPdfService {
 		return false;
 	}
 
-	String buildUserName(UserProfile userProfile, Direction direction) {
-		return Optional.ofNullable(userProfile).map(this::formatUserName).orElseGet(() -> FALLBACK_USER_NAME);
+	String buildAbsenderName(PostfachNachrichtPdfData postfachNachrichtPdfData, Antragsteller antragsteller) {
+		if (postfachNachrichtPdfData.getDirection() == Direction.IN) {
+			return buildAbsenderForOutgoingNachricht(antragsteller);
+		}
+		return buildAbsenderForIncomingNachricht(postfachNachrichtPdfData);
+	}
+
+	String buildAbsenderForOutgoingNachricht(Antragsteller antragsteller) {
+		return Optional.ofNullable(antragsteller)
+				.map(this::formatAntragstellerName)
+				.orElseGet(() -> FALLBACK_ANTRAGSTELLER_NAME);
+	}
+
+	String buildAbsenderForIncomingNachricht(PostfachNachrichtPdfData postfachNachrichtPdfData) {
+		return Optional.ofNullable(postfachNachrichtPdfData.getUser()).map(user -> {
+			if (Objects.nonNull(user.getId()) && user.toString().startsWith(UserManagerUrlProvider.SYSTEM_USER_IDENTIFIER)) {
+				return SYSTEM_NACHRICHT_NAME;
+			}
+			return formatUserName(postfachNachrichtPdfData.getUser());
+		}).orElseGet(() -> FALLBACK_USER_NAME);
 	}
 
 	private String formatUserName(UserProfile user) {
 		return String.format("%s %s", user.getFirstName(), user.getLastName());
 	}
+
+	private String formatAntragstellerName(Antragsteller antragsteller) {
+		return String.format("%s %s", antragsteller.getVorname(), antragsteller.getNachname());
+	}
 }
\ No newline at end of file
diff --git a/goofy-server/src/test/java/de/itvsh/goofy/common/user/UserProfileTestFactory.java b/goofy-server/src/test/java/de/itvsh/goofy/common/user/UserProfileTestFactory.java
index 226531aa45..0996dce79b 100644
--- a/goofy-server/src/test/java/de/itvsh/goofy/common/user/UserProfileTestFactory.java
+++ b/goofy-server/src/test/java/de/itvsh/goofy/common/user/UserProfileTestFactory.java
@@ -40,6 +40,8 @@ public class UserProfileTestFactory {
 	public static final String ROLE = "TEST_USER";
 	public static final GrantedAuthority AUTHORITY = new SimpleGrantedAuthority(ROLE);
 
+	public static final String SYSTEM_USER = "system_user_example";
+
 	public static UserProfile create() {
 		return createBuilder().build();
 	}
diff --git a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
index 055882237d..5fee86fc5c 100644
--- a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
+++ b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
@@ -332,7 +332,7 @@ class PostfachMailServiceTest {
 					}
 
 					@Test
-					void shouldHaveSetAsEmptyStringOnNull() {
+					void shouldHaveSetNullAsUser() {
 						var postfachNachricht = buildPostfachNachrichtPdfDataWithoutUser();
 
 						assertThat(postfachNachricht.getUser()).isNull();
@@ -368,5 +368,53 @@ class PostfachMailServiceTest {
 				assertThat(fileIds).isEmpty();
 			}
 		}
+
+		@DisplayName("get User")
+		@Nested
+		class TestGetUser {
+
+			@DisplayName("on existing client user")
+			@Nested
+			class TestOnExistingClientUser {
+
+				@Test
+				void shouldCallUserServiceOnExistingUser() {
+					service.getUser(PostfachMailTestFactory.create());
+
+					verify(userService).getById(UserProfileTestFactory.ID);
+				}
+
+				@Test
+				void shouldReturnUserProfileWithId() {
+					when(userService.getById(any())).thenReturn(UserProfileTestFactory.create());
+
+					var user = service.getUser(PostfachMailTestFactory.create());
+
+					assertThat(user.getId()).isEqualTo(UserProfileTestFactory.ID);
+				}
+			}
+
+			@DisplayName("on existing system user")
+			@Nested
+			class TestOnSystemUser {
+
+				private final PostfachMail postfachMail = PostfachMailTestFactory.createBuilder()
+						.createdBy(UserId.from(UserProfileTestFactory.SYSTEM_USER)).build();
+
+				@Test
+				void shouldNotCallUserService() {
+					service.getUser(postfachMail);
+
+					verify(userService, never()).getById(UserProfileTestFactory.ID);
+				}
+
+				@Test
+				void shouldReturnUserProfileWithId() {
+					var user = service.getUser(postfachMail);
+
+					assertThat(user.getId()).hasToString(UserProfileTestFactory.SYSTEM_USER);
+				}
+			}
+		}
 	}
 }
\ No newline at end of file
diff --git a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfServiceTest.java b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfServiceTest.java
index 4ed6014afa..24f10a6e03 100644
--- a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfServiceTest.java
+++ b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachNachrichtPdfServiceTest.java
@@ -44,6 +44,7 @@ import org.springframework.util.ReflectionUtils;
 
 import de.itvsh.goofy.common.binaryfile.BinaryFileTestFactory;
 import de.itvsh.goofy.common.user.UserProfileTestFactory;
+import de.itvsh.goofy.postfach.PostfachMail.Direction;
 import de.itvsh.goofy.postfach.PostfachNachrichtPdfModel.Nachricht;
 import de.itvsh.goofy.vorgang.AntragstellerTestFactory;
 import de.itvsh.goofy.vorgang.VorgangHeaderTestFactory;
@@ -240,7 +241,7 @@ class PostfachNachrichtPdfServiceTest {
 				void shouldMapNachrichtCreatedAt() {
 					var nachricht = mapNachricht();
 
-					assertThat(nachricht.getCreatedAt()).isEqualTo("01.01.2000");
+					assertThat(nachricht.getCreatedAt()).isEqualTo("01.01.2000 01:00:00");
 				}
 
 				@Test
@@ -258,7 +259,63 @@ class PostfachNachrichtPdfServiceTest {
 				}
 
 				private Nachricht mapNachricht() {
-					return service.mapPostfachNachricht(PostfachNachrichtPdfDataTestFactory.create());
+					return service.mapPostfachNachricht(PostfachNachrichtPdfDataTestFactory.create(), AntragstellerTestFactory.create());
+				}
+
+				@DisplayName("for outgoing nachricht")
+				@Nested
+				class TestDirectionOut {
+
+					@DisplayName("build absender name")
+					@Nested
+					class TestBuildUserName {
+
+						private final PostfachNachrichtPdfData data = PostfachNachrichtPdfDataTestFactory.createBuilder().direction(Direction.IN)
+								.build();
+
+						@Test
+						void shouldReturnAntragstellerIfExists() {
+							var name = service.buildAbsenderName(data, AntragstellerTestFactory.create());
+
+							assertThat(name).isEqualTo(AntragstellerTestFactory.VORNAME + " " + AntragstellerTestFactory.NACHNAME);
+						}
+
+						@Test
+						void shouldReturnFallbackNameForAntragsteller() {
+							var name = service.buildAbsenderName(data, null);
+
+							assertThat(name).isEqualTo(PostfachNachrichtPdfService.FALLBACK_ANTRAGSTELLER_NAME);
+						}
+					}
+				}
+
+				@DisplayName("for incoming nachricht")
+				@Nested
+				class TestDirectionIn {
+
+					@DisplayName("build absender name")
+					@Nested
+					class TestBuildUserName {
+
+						private final PostfachNachrichtPdfData data = PostfachNachrichtPdfDataTestFactory.createBuilder().direction(Direction.OUT)
+								.build();
+
+						@Test
+						void shouldReturnUserProfileNameIfExists() {
+							var userName = service.buildAbsenderName(data, AntragstellerTestFactory.create());
+
+							assertThat(userName).isEqualTo(UserProfileTestFactory.FIRSTNAME + " " + UserProfileTestFactory.LASTNAME);
+						}
+
+						@Test
+						void shouldReturnFallbackNameForUserProfile() {
+							var name = service.buildAbsenderName(
+									PostfachNachrichtPdfDataTestFactory.createBuilder().direction(Direction.OUT).user(null).build(),
+									AntragstellerTestFactory.create());
+
+							assertThat(name).isEqualTo(PostfachNachrichtPdfService.FALLBACK_USER_NAME);
+						}
+					}
 				}
 			}
 		}
-- 
GitLab