From 8e43fb48eea9c0ebbbf2f23431f2ad8eea39f34b Mon Sep 17 00:00:00 2001
From: Michal Badin <xbadin@fi.muni.cz>
Date: Mon, 8 May 2023 20:42:26 +0200
Subject: [PATCH] fix(new-application-email): Added email template

---
 .../pa165/service/NotificationService.java    | 22 +++---
 .../templates/new-application-email.html      | 69 +++++++++++++++++++
 2 files changed, 80 insertions(+), 11 deletions(-)
 create mode 100644 notification/src/main/resources/templates/new-application-email.html

diff --git a/notification/src/main/java/cz/muni/pa165/service/NotificationService.java b/notification/src/main/java/cz/muni/pa165/service/NotificationService.java
index d9742fd..5ae703c 100644
--- a/notification/src/main/java/cz/muni/pa165/service/NotificationService.java
+++ b/notification/src/main/java/cz/muni/pa165/service/NotificationService.java
@@ -14,7 +14,6 @@ import org.thymeleaf.TemplateEngine;
 import org.thymeleaf.context.Context;
 
 import java.nio.charset.StandardCharsets;
-import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
@@ -22,11 +21,10 @@ import java.util.List;
 @Service
 public class NotificationService {
     private static final String EMAIL = "formula.team.management@gmail.com";
+    private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
     private final JavaMailSender emailSender;
     private final TemplateEngine templateEngine;
 
-    private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
-
     @Autowired
     public NotificationService(JavaMailSender emailSender, TemplateEngine templateEngine) {
         this.emailSender = emailSender;
@@ -51,16 +49,18 @@ public class NotificationService {
     public ConfirmationDto notifyNewApplication(ApplicationNotificationDto applicationNotificationDto) {
         var a = applicationNotificationDto.getApplication();
         var subject = "New application: " + getFullName(a);
-        var text = "New application was received.\n"
-                + "\nId: " + a.getId()
-                + "\nName: " + a.getName()
-                + "\nSurname: " + a.getSurname()
-                + "\nBirthday: " + a.getBirthday()
-                + "\nEmail: " + a.getEmail()
-                + "\nFilling out date: " + a.getFillingOutDate();
 
+        Context ctx = new Context();
+        ctx.setVariable("id", a.getId());
+        ctx.setVariable("name", a.getName());
+        ctx.setVariable("surname", a.getSurname());
+        ctx.setVariable("birthday", a.getBirthday().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
+        ctx.setVariable("email", a.getEmail());
+        ctx.setVariable("fillingOutDate", a.getFillingOutDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
+
+        var htmlContent = templateEngine.process("new-application-email", ctx);
 
-        return sendEmail(subject, text, applicationNotificationDto.getReceivers());
+        return sendEmail(subject, htmlContent, applicationNotificationDto.getReceivers());
     }
 
     public ConfirmationDto notifyApplicationStatusChange(ApplicationNotificationDto applicationNotificationDto) {
diff --git a/notification/src/main/resources/templates/new-application-email.html b/notification/src/main/resources/templates/new-application-email.html
new file mode 100644
index 0000000..9f13d21
--- /dev/null
+++ b/notification/src/main/resources/templates/new-application-email.html
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org" lang="en">
+<head>
+    <meta charset="utf-8">
+    <title>Application for Formula Driver Position</title>
+    <style>
+        body {
+            font-family: Arial, sans-serif;
+            margin: 0;
+            padding: 0;
+            background-color: #f6f6f6;
+        }
+
+        .container {
+            max-width: 625px;
+            margin: 0 auto;
+            background-color: #fff;
+            padding: 20px;
+            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+            position: relative;
+        }
+
+        h1 {
+            font-size: 24px;
+            font-weight: bold;
+            margin: 0;
+            text-align: left;
+        }
+
+        p {
+            font-size: 16px;
+            line-height: 1.5;
+            margin: 20px 0;
+            text-align: justify;
+        }
+    </style>
+</head>
+<body>
+<div class="container">
+    <h1>New Application Received</h1>
+    <table>
+        <tr>
+            <td>Id:</td>
+            <td th:text="${id}"></td>
+        </tr>
+        <tr>
+            <td>Name:</td>
+            <td th:text="${name}"></td>
+        </tr>
+        <tr>
+            <td>Surname:</td>
+            <td th:text="${surname}"></td>
+        </tr>
+        <tr>
+            <td>Birthday:</td>
+            <td th:text="${birthday}"></td>
+        </tr>
+        <tr>
+            <td>Email:</td>
+            <td th:text="${email}"></td>
+        </tr>
+        <tr>
+            <td>Filling out date:</td>
+            <td th:text="${fillingOutDate}"></td>
+        </tr>
+    </table>
+</div>
+</body>
+</html>
\ No newline at end of file
-- 
GitLab