From 270e9c14ef09c79cc85ce9d2b20fd96d8126be00 Mon Sep 17 00:00:00 2001
From: Radheshhathwar <radheshhathwar.bh@gmail.com>
Date: Tue, 21 Nov 2023 14:38:14 +0530
Subject: [PATCH] Junk ticket email issue

---
 .../upsmf/grievance/service/EmailService.java |   2 +-
 .../service/impl/EmailServiceImpl.java        |  46 ++++-
 .../service/impl/TicketServiceImpl.java       |  38 ++--
 .../resources/templates/raiser_junk_ticket.vm | 173 ++++++++++++++++++
 4 files changed, 237 insertions(+), 22 deletions(-)
 create mode 100644 src/main/resources/templates/raiser_junk_ticket.vm

diff --git a/src/main/java/org/upsmf/grievance/service/EmailService.java b/src/main/java/org/upsmf/grievance/service/EmailService.java
index cebfe09..31e0a44 100644
--- a/src/main/java/org/upsmf/grievance/service/EmailService.java
+++ b/src/main/java/org/upsmf/grievance/service/EmailService.java
@@ -16,7 +16,7 @@ public interface EmailService {
     void sendUpdateTicketMail(EmailDetails details, Ticket ticket);
 
     void sendClosedTicketMail(EmailDetails details, Ticket ticket, String comment, List<AssigneeTicketAttachment> attachments, String feedbackURL);
-
+    void sendJunkMail(EmailDetails details, Ticket ticket, String comment, List<AssigneeTicketAttachment> attachments, String feedbackURL);
     void sendSimpleMail(EmailDetails details);
 
     // Method
diff --git a/src/main/java/org/upsmf/grievance/service/impl/EmailServiceImpl.java b/src/main/java/org/upsmf/grievance/service/impl/EmailServiceImpl.java
index 9d615f8..c83d019 100644
--- a/src/main/java/org/upsmf/grievance/service/impl/EmailServiceImpl.java
+++ b/src/main/java/org/upsmf/grievance/service/impl/EmailServiceImpl.java
@@ -89,8 +89,15 @@ public class EmailServiceImpl implements EmailService {
         new Thread(mailThread).start();
 
     }
+    @Override
+    public void sendJunkMail(EmailDetails details, Ticket ticket, String comment, List<AssigneeTicketAttachment> attachments, String feedbackURL) {
+        // Try block to check for exceptions
+        Runnable mailThread = () -> {   // lambda expression
+            sendJunkResponseToRaiser(details, ticket, comment, attachments, feedbackURL);
+        };
+        new Thread(mailThread).start();
 
-
+    }
     private void sendFeedbackMailToRaiser(EmailDetails details, Ticket ticket,
                                          String comment, List<AssigneeTicketAttachment> attachments,
                                          String feedbackUrl) {
@@ -128,6 +135,43 @@ public class EmailServiceImpl implements EmailService {
             log.error("Error while Sending Mail", e);
         }
     }
+    private void sendJunkResponseToRaiser(EmailDetails details, Ticket ticket,
+                                          String comment, List<AssigneeTicketAttachment> attachments,
+                                          String feedbackUrl) {
+        try {
+            MimeMessagePreparator preparator = new MimeMessagePreparator() {
+                public void prepare(MimeMessage mimeMessage) throws Exception {
+                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
+                    message.setTo(details.getRecipient());
+                    message.setSubject(details.getSubject());
+
+                    List<Department> departmentList = Department.getById(Integer.parseInt(ticket.getAssignedToId()));
+                    VelocityContext velocityContext = new VelocityContext();
+                    velocityContext.put("first_name", ticket.getFirstName());
+                    velocityContext.put("id", ticket.getId());
+                    velocityContext.put("created_date", DateUtil.getFormattedDateInString(ticket.getCreatedDate()));
+                    velocityContext.put("department", departmentList!=null&&!departmentList.isEmpty()?departmentList.get(0).getCode():"Others");
+                    velocityContext.put("comment", comment);
+                    velocityContext.put("url", feedbackUrl);
+
+                    // signature
+                    createCommonMailSignature(velocityContext);
+                    // merge mail body
+                    StringWriter stringWriter = new StringWriter();
+                    velocityEngine.mergeTemplate("templates/raiser_junk_ticket.vm", "UTF-8", velocityContext, stringWriter);
+
+                    message.setText(stringWriter.toString(), true);
+                }
+            };
+            // Sending the mail
+            javaMailSender.send(preparator);
+            log.info("create ticket mail Sent Successfully...");
+        }
+        // Catch block to handle the exceptions
+        catch (Exception e) {
+            log.error("Error while Sending Mail", e);
+        }
+    }
 
     private void sendUpdateMailToRaiser(EmailDetails details, Ticket ticket) {
         try {
diff --git a/src/main/java/org/upsmf/grievance/service/impl/TicketServiceImpl.java b/src/main/java/org/upsmf/grievance/service/impl/TicketServiceImpl.java
index 245e224..021ca96 100644
--- a/src/main/java/org/upsmf/grievance/service/impl/TicketServiceImpl.java
+++ b/src/main/java/org/upsmf/grievance/service/impl/TicketServiceImpl.java
@@ -203,8 +203,7 @@ public class TicketServiceImpl implements TicketService {
             generateFeedbackLinkAndEmail(ticket);
             return ticket;
         } else if (curentUpdatedTicket.getStatus().name().equalsIgnoreCase(TicketStatus.INVALID.name())) {
-            EmailDetails resolutionOfYourGrievance = EmailDetails.builder().subject("Update on Ticket Status - " +curentUpdatedTicket.getTicketId()).recipient(curentUpdatedTicket.getEmail()).build();
-            emailService.sendUpdateTicketMail(resolutionOfYourGrievance, ticket);
+            generateFeedbackLinkAndEmailForJunkTicket(ticket);
             return ticket;
         }else {
             EmailDetails resolutionOfYourGrievance = EmailDetails.builder().subject("Resolution of Your Grievance - " + curentUpdatedTicket.getTicketId()).recipient(curentUpdatedTicket.getEmail()).build();
@@ -230,24 +229,23 @@ public class TicketServiceImpl implements TicketService {
         EmailDetails resolutionOfYourGrievance = EmailDetails.builder().subject("Resolution of Your Grievance").recipient(curentUpdatedTicket.getEmail()).build();
         emailService.sendClosedTicketMail(resolutionOfYourGrievance, curentUpdatedTicket, comment, Collections.EMPTY_LIST, link);
     }
-//    private void generateFeedbackLinkAndEmail(Ticket curentUpdatedTicket) {
-//        List<Comments> comments = commentRepository.findAllByTicketId(curentUpdatedTicket.getId());
-//        Comments latestComment =null;
-//        if(comments!=null && comments.size() > 0) {
-//            latestComment = comments.get(comments.size()-1);
-//        }
-//        String comment = latestComment!=null?latestComment.getComment():"";
-//        String link = feedbackBaseUrl.concat("?").concat("guestName=")
-//                .concat(curentUpdatedTicket.getFirstName().concat("%20").concat(curentUpdatedTicket.getLastName()))
-//                .concat("&ticketId=").concat(String.valueOf(curentUpdatedTicket.getId()))
-//                .concat("&resolutionComment=").concat(comment)
-//                .concat("&email=").concat(curentUpdatedTicket.getEmail())
-//                .concat("&phone=").concat(curentUpdatedTicket.getPhone())
-//                .concat("&ticketTitle=").concat(curentUpdatedTicket.getDescription());
-//        EmailDetails resolutionOfYourGrievance = EmailDetails.builder().subject("Resolution of Your Grievance").recipient(curentUpdatedTicket.getEmail()).build();
-//        emailService.sendClosedTicketMail(resolutionOfYourGrievance, curentUpdatedTicket, comment, Collections.EMPTY_LIST, link);
-//    }
-
+    private void generateFeedbackLinkAndEmailForJunkTicket(Ticket curentUpdatedTicket) {
+        List<Comments> comments = commentRepository.findAllByTicketId(curentUpdatedTicket.getId());
+        Comments latestComment =null;
+        if(comments!=null && comments.size() > 0) {
+            latestComment = comments.get(comments.size()-1);
+        }
+        String comment = latestComment!=null?latestComment.getComment():"";
+        String link = feedbackBaseUrl.concat("?").concat("guestName=")
+                .concat(curentUpdatedTicket.getFirstName().concat("%20").concat(curentUpdatedTicket.getLastName()))
+                .concat("&ticketId=").concat(String.valueOf(curentUpdatedTicket.getId()))
+                .concat("&resolutionComment=").concat(comment)
+                .concat("&email=").concat(curentUpdatedTicket.getEmail())
+                .concat("&phone=").concat(curentUpdatedTicket.getPhone())
+                .concat("&ticketTitle=").concat(curentUpdatedTicket.getDescription());
+        EmailDetails resolutionOfYourGrievance = EmailDetails.builder().subject("Resolution of Your Grievance").recipient(curentUpdatedTicket.getEmail()).build();
+        emailService.sendJunkMail(resolutionOfYourGrievance, curentUpdatedTicket, comment, Collections.EMPTY_LIST, link);
+    }
     @Override
     public Ticket getTicketById(long id) {
         if(id <= 0) {
diff --git a/src/main/resources/templates/raiser_junk_ticket.vm b/src/main/resources/templates/raiser_junk_ticket.vm
new file mode 100644
index 0000000..f12531c
--- /dev/null
+++ b/src/main/resources/templates/raiser_junk_ticket.vm
@@ -0,0 +1,173 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
+
+<head>
+    <meta charset="UTF-8">
+    <meta content="width=device-width, initial-scale=1" name="viewport">
+    <meta name="x-apple-disable-message-reformatting">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta content="telephone=no" name="format-detection">
+    <title></title>
+</head>
+
+<body style="background: #fdebe8; padding: 50px">
+    <div class="es-wrapper-color">
+        <table class="es-wrapper" width="100%" cellspacing="0" cellpadding="0">
+            <tbody>
+                <tr>
+                    <td class="esd-email-paddings" valign="top">
+                        <table class="esd-header-popover es-header" cellspacing="0" cellpadding="0" align="center">
+                            <tbody>
+                                <tr>
+                                    <td class="esd-stripe" align="center">
+                                        <table class="es-header-body" width="600" cellspacing="0" cellpadding="0" bgcolor="#ffffff" align="center">
+                                            <tbody>
+                                                <tr>
+                                                    <td class="es-p20t es-p20r es-p20l esd-structure" align="left">
+                                                        <table class="es-left" cellspacing="0" cellpadding="0" align="left">
+                                                            <tbody>
+                                                                <tr>
+                                                                    <td class="es-m-p0r es-m-p20b esd-container-frame" width="180" valign="top" align="center">
+                                                                        <table width="100%" cellspacing="0" cellpadding="0">
+                                                                            <tbody>
+                                                                                <tr>
+                                                                                    <td align="center" class="esd-block-image" style="font-size: 0px;"><a target="_blank" href="https:// sitename.org"><img class="adapt-img" src="https://uphrh.in/assets/images/sunbird_logo.png" alt="sitename" style="display: block; width: 350px; padding: 50px 25px;" width="180" title="sitename"></a></td>
+                                                                                </tr>
+                                                                            </tbody>
+                                                                        </table>
+                                                                    </td>
+                                                                </tr>
+                                                            </tbody>
+                                                        </table>
+                                                        <!--[if mso]></td><td width="20"></td><td width="360" valign="top"><![endif]-->
+                                                        <table class="es-right" cellspacing="0" cellpadding="0" align="right">
+                                                            <tbody>
+                                                                <tr>
+                                                                    <td class="esd-container-frame" width="360" align="left">
+                                                                        <table width="100%" cellspacing="0" cellpadding="0">
+                                                                            <tbody>
+                                                                                <tr>
+                                                                                    <td align="center" class="esd-empty-container" style="display: none;"></td>
+                                                                                </tr>
+                                                                            </tbody>
+                                                                        </table>
+                                                                    </td>
+                                                                </tr>
+                                                            </tbody>
+                                                        </table>
+                                                    </td>
+                                                </tr>
+                                            </tbody>
+                                        </table>
+                                    </td>
+                                </tr>
+                            </tbody>
+                        </table>
+                    </td>
+                </tr>
+                <tr>
+                    <td>
+                        <table class="es-content" cellspacing="0" cellpadding="0" align="center">
+                            <tbody>
+                                <tr>
+                                    <td class="esd-stripe" align="center">
+                                        <table class="es-content-body" width="600" cellspacing="0" cellpadding="0" bgcolor="#ffffff" align="center">
+                                            <tbody>
+                                                <tr>
+                                                    <td class="es-p20t es-p20r es-p20l esd-structure" align="left">
+                                                        <table width="100%" cellspacing="0" cellpadding="0">
+                                                            <tbody>
+                                                                <tr>
+                                                                    <td class="esd-container-frame" width="560" valign="top" align="center" style="padding: 0 50px;">
+                                                                        <table width="100%" cellspacing="0" cellpadding="0">
+                                                                            <tbody>
+                                                                                <tr>
+                                                                                    <td align="justify" class="esd-block-text es-m-txt-l">
+                                                                                        <p></br></p>
+                                                                                        <p>Dear ${first_name},</p>
+                                                                                        <p>We hope this email finds you well. We are writing to provide you with an update regarding your recent ticket with us, ${id}.</p>
+                                                                                        <p>We are pleased to inform you that significant progress has been made in resolving the issue you reported. Our dedicated team has been diligently working on your case, and we are confident that we are moving towards a resolution.</p>
+                                                                                        <p>
+                                                                                            <div>Here is a brief overview of the status:</div>
+                                                                                            <div>Grievance ID: <strong>${id}</strong></div>
+                                                                                            <div>Date of Registration: <strong>${created_date}</strong></div>
+                                                                                            <div>Current Status: <strong>${status}</strong></div>
+                                                                                            <div>Date of Last Update: <strong>${updated_date}</strong></div>
+                                                                                        </p>
+                                                                                        <p>Our support team is continuously monitoring the situation to ensure that all necessary steps are taken to address your concerns effectively and efficiently. Rest assured; we are committed to providing you with the best possible resolution.</p>
+                                                                                        <p>If you have any additional information or updates that could be helpful for our team to better understand your issue, please feel free to share it with us. Your input is valuable and can contribute to a swifter resolution.</p>
+                                                                                        <p>We will keep you updated on any further developments regarding your ticket.</p>
+                                                                                    </td>
+                                                                                </tr>
+                                                                                <tr></tr>
+                                                                                <tr>
+                                                                                    <td align="left" class="esd-block-text">
+                                                                                        <p>Sincerely,</p>
+                                                                                        <p>
+                                                                                            <div>${signature_name}</div>
+                                                                                            <div>${address}</div>
+                                                                                            <div>${phone}</div>
+                                                                                            <div>${mobile}</div>
+                                                                                            <div>${fax}</div>
+                                                                                            <div>${email}</div>
+                                                                                        </p>
+                                                                                    </td>
+                                                                                </tr>
+                                                                            </tbody>
+                                                                        </table>
+                                                                    </td>
+                                                                </tr>
+                                                            </tbody>
+                                                        </table>
+                                                    </td>
+                                                </tr>
+                                            </tbody>
+                                        </table>
+                                    </td>
+                                </tr>
+                            </tbody>
+                        </table>
+                    </td>
+                </tr>
+            </tbody>
+        </table>
+        <table class="esd-footer-popover es-footer" cellspacing="0" cellpadding="0" align="center" bgcolor="#fdebe8">
+                <tbody>
+                    <tr>
+                        <td class="esd-stripe" align="center">
+                            <table class="es-footer-body" width="600" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff" style="padding: 100px 50px;">
+                                <tbody>
+                                    <tr>
+                                        <td class="esd-structure es-p20t es-p20b es-p20r es-p20l" align="left">
+                                            <table class="es-left" cellspacing="0" cellpadding="0" align="center">
+                                                <tbody>
+                                                    <tr>
+                                                        <td class="es-m-p20b esd-container-frame" width="400" align="left">
+                                                            <table width="100%" cellspacing="0" cellpadding="0">
+                                                                <tbody>
+                                                                    <tr>
+                                                                        <td align="center" class="esd-block-text">
+                                                                           <label _ngcontent-wjh-c12="" class="d-flex flex-w-wrap mb-16 flex-jc-center"><a _ngcontent-wjh-c12="" apptelemetryinteract="" href="/term-of-use.html"> Terms and Privacy </a><span _ngcontent-wjh-c12="">| © 2023, NIRAMAYA</span></label>
+                                                                        </td>
+                                                                    </tr>
+                                                                </tbody>
+                                                            </table>
+                                                        </td>
+                                                    </tr>
+                                                </tbody>
+                                            </table>
+                                        </td>
+                                    </tr>
+                                </tbody>
+                            </table>
+                        </td>
+                    </tr>
+                </tbody>
+            </table>
+        </td>
+    </tr>
+</tbody>
+</table>
+</div>
+</body>
+</html>
\ No newline at end of file
-- 
GitLab