From 1032e38fb6a414e96b7f018e1c7a888a9bee51fa Mon Sep 17 00:00:00 2001
From: vnglnk <128087718+holotsvan@users.noreply.github.com>
Date: Thu, 5 Dec 2024 16:48:22 +0200
Subject: [PATCH] add functions to replace {times} to actual amount of times
 (#7871)

* add functions to replace {times} to actual amount of times

* tests

* issue
---
 .../service/NotificationServiceImpl.java      |  9 +++-
 .../service/UserNotificationServiceImpl.java  | 26 +---------
 .../greencity/utils/NotificationUtils.java    | 35 +++++++++++++
 .../utils/NotificationUtilsTest.java          | 51 +++++++++++++++++++
 4 files changed, 96 insertions(+), 25 deletions(-)
 create mode 100644 service/src/main/java/greencity/utils/NotificationUtils.java
 create mode 100644 service/src/test/java/greencity/utils/NotificationUtilsTest.java

diff --git a/service/src/main/java/greencity/service/NotificationServiceImpl.java b/service/src/main/java/greencity/service/NotificationServiceImpl.java
index 5f66702dd..8b9031c1c 100644
--- a/service/src/main/java/greencity/service/NotificationServiceImpl.java
+++ b/service/src/main/java/greencity/service/NotificationServiceImpl.java
@@ -37,6 +37,8 @@
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import static greencity.utils.NotificationUtils.resolveTimesInEnglish;
+import static greencity.utils.NotificationUtils.resolveTimesInUkrainian;
 
 @Slf4j
 @Service
@@ -360,10 +362,15 @@ private ScheduledEmailMessage createScheduledEmailMessage(Notification notificat
         }
         String customMessage = notification.getCustomMessage() != null ? notification.getCustomMessage() : "";
         String secondMessage = notification.getSecondMessage() != null ? notification.getSecondMessage() : "";
+        int messagesCount = notification.getActionUsers().size();
+        String times = language.equals("ua")
+            ? resolveTimesInUkrainian(messagesCount)
+            : resolveTimesInEnglish(messagesCount);
         String body = bodyTemplate
             .replace("{user}", actionUserText)
             .replace("{message}", customMessage)
-            .replace("{secondMessage}", secondMessage);
+            .replace("{secondMessage}", secondMessage)
+            .replace("{times}", times);
 
         return ScheduledEmailMessage.builder()
             .email(notification.getTargetUser().getEmail())
diff --git a/service/src/main/java/greencity/service/UserNotificationServiceImpl.java b/service/src/main/java/greencity/service/UserNotificationServiceImpl.java
index 79eaf66bd..7306fbac4 100644
--- a/service/src/main/java/greencity/service/UserNotificationServiceImpl.java
+++ b/service/src/main/java/greencity/service/UserNotificationServiceImpl.java
@@ -32,6 +32,8 @@
 import java.util.Locale;
 import java.util.Optional;
 import java.util.ResourceBundle;
+import static greencity.utils.NotificationUtils.resolveTimesInEnglish;
+import static greencity.utils.NotificationUtils.resolveTimesInUkrainian;
 
 /**
  * Implementation of {@link UserNotificationService}.
@@ -379,30 +381,6 @@ private NotificationDto createNotificationDto(Notification notification, String
         return dto;
     }
 
-    private String resolveTimesInEnglish(final int number) {
-        return switch (number) {
-            case 1 -> "";
-            case 2 -> "twice";
-            default -> number + " times";
-        };
-    }
-
-    private String resolveTimesInUkrainian(int number) {
-        number = Math.abs(number);
-        final int lastTwoDigits = number % 100;
-        final int lastDigit = number % 10;
-
-        if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
-            return number + " разів";
-        }
-
-        return switch (lastDigit) {
-            case 1 -> "";
-            case 2, 3, 4 -> number + " рази";
-            default -> number + " разів";
-        };
-    }
-
     /**
      * Sends a new notification to a specified user.
      *
diff --git a/service/src/main/java/greencity/utils/NotificationUtils.java b/service/src/main/java/greencity/utils/NotificationUtils.java
new file mode 100644
index 000000000..2c8854cb0
--- /dev/null
+++ b/service/src/main/java/greencity/utils/NotificationUtils.java
@@ -0,0 +1,35 @@
+package greencity.utils;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class NotificationUtils {
+    public static String resolveTimesInEnglish(final int number) {
+        return switch (number) {
+            case 1 -> "";
+            case 2 -> "twice";
+            default -> number + " times";
+        };
+    }
+
+    public static String resolveTimesInUkrainian(int number) {
+        number = Math.abs(number);
+        final int lastTwoDigits = number % 100;
+        final int lastDigit = number % 10;
+
+        if (number == 1) {
+            return "";
+        }
+
+        if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
+            return number + " разів";
+        }
+
+        return switch (lastDigit) {
+            case 1 -> number + " раз";
+            case 2, 3, 4 -> number + " рази";
+            default -> number + " разів";
+        };
+    }
+}
diff --git a/service/src/test/java/greencity/utils/NotificationUtilsTest.java b/service/src/test/java/greencity/utils/NotificationUtilsTest.java
new file mode 100644
index 000000000..5aaebfe48
--- /dev/null
+++ b/service/src/test/java/greencity/utils/NotificationUtilsTest.java
@@ -0,0 +1,51 @@
+package greencity.utils;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import java.util.stream.Stream;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class NotificationUtilsTest {
+    @ParameterizedTest(name = "Test resolveTimeInUkrainian with input {0}")
+    @MethodSource("provideUkrainianTestCases")
+    void testResolveTimeInUkrainian(int input, String expected) {
+        String result = NotificationUtils.resolveTimesInUkrainian(input);
+        assertEquals(expected, result);
+    }
+
+    private static Stream<Arguments> provideUkrainianTestCases() {
+        return Stream.of(
+            Arguments.of(1, ""),
+            Arguments.of(2, "2 рази"),
+            Arguments.of(4, "4 рази"),
+            Arguments.of(5, "5 разів"),
+            Arguments.of(11, "11 разів"),
+            Arguments.of(19, "19 разів"),
+            Arguments.of(21, "21 раз"),
+            Arguments.of(22, "22 рази"),
+            Arguments.of(25, "25 разів"),
+            Arguments.of(100, "100 разів"),
+            Arguments.of(101, "101 раз"),
+            Arguments.of(102, "102 рази"),
+            Arguments.of(112, "112 разів"),
+            Arguments.of(123, "123 рази"));
+    }
+
+    @ParameterizedTest(name = "Test resolveTimesInEnglish with input {0}")
+    @MethodSource("provideEnglishTestCases")
+    void testResolveTimesInEnglish(int input, String expected) {
+        String result = NotificationUtils.resolveTimesInEnglish(input);
+        assertEquals(expected, result);
+    }
+
+    private static Stream<Arguments> provideEnglishTestCases() {
+        return Stream.of(
+            Arguments.of(1, ""),
+            Arguments.of(2, "twice"),
+            Arguments.of(3, "3 times"),
+            Arguments.of(5, "5 times"),
+            Arguments.of(11, "11 times"),
+            Arguments.of(21, "21 times"));
+    }
+}
\ No newline at end of file