From a9af79212fddee12ffae9fa62f0c5a9dcbd1fde6 Mon Sep 17 00:00:00 2001 From: Kenneth Ezekiel <88850771+KenEzekiel@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:59:49 +0700 Subject: [PATCH] add: integrated with admin endpoint from mailer --- .../soap/service/UserPremiumServiceImpl.java | 17 +++++++++-------- src/main/java/com/letterpaw/soap/util/Auth.java | 8 ++++---- .../java/com/letterpaw/soap/util/Mailer.java | 16 ++++++++++++---- .../java/com/letterpaw/soap/util/Request.java | 9 +++++++-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java b/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java index 126740f..6f0ba97 100644 --- a/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java +++ b/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java @@ -6,6 +6,7 @@ import com.letterpaw.soap.enums.ServiceType; import com.letterpaw.soap.util.Auth; import com.letterpaw.soap.util.Logger; import com.letterpaw.soap.util.Mailer; +import com.letterpaw.soap.util.Request; import javax.annotation.Resource; import javax.jws.WebMethod; @@ -34,14 +35,14 @@ public class UserPremiumServiceImpl implements UserPremiumService { s.premiumStatus = UserPremium.Status.PENDING; s.save(); // Notify admin -// Request.GET(ServiceType.REST, "/users/admins", (String adminJson) -> { -// Mailer.notifyAdminNewSub( -// "There is a new premium request from user id " + userId, -// adminJson -// ); -// }); - String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; - Mailer.notifyAdminNewSub("There is a new premium request from user ID " + userId, adminJson); + Request.GET(ServiceType.PHP, "/admins", (String adminJson) -> { + Mailer.notifyAdminNewSub( + "There is a new premium request from user id " + userId, + adminJson + ); + }); +// String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; +// Mailer.notifyAdminNewSub("There is a new premium request from user ID " + userId, adminJson); return ResponseType.SUCCESS.toString(); // Success adding new Successfully registered for premium } return ResponseType.FAILED.toString(); // User Premium already exist User Premium already exists diff --git a/src/main/java/com/letterpaw/soap/util/Auth.java b/src/main/java/com/letterpaw/soap/util/Auth.java index a2f2648..50ab18b 100644 --- a/src/main/java/com/letterpaw/soap/util/Auth.java +++ b/src/main/java/com/letterpaw/soap/util/Auth.java @@ -30,11 +30,11 @@ public class Auth { } else if (type == ServiceType.PHP) { key = PHPAPIKey; } -// System.out.println(auth.get(0)); -// System.out.println(key); + System.out.println(auth.get(0)); + System.out.println(key); System.out.println(!auth.isEmpty() && auth.get(0).equals("Bearer " + key)); - return true; +// return true; // Check the key validity -// return !auth.isEmpty() && auth.get(0).equals("Bearer " + key); + return !auth.isEmpty() && auth.get(0).equals("Bearer " + key); } } diff --git a/src/main/java/com/letterpaw/soap/util/Mailer.java b/src/main/java/com/letterpaw/soap/util/Mailer.java index d17365b..3cdef20 100644 --- a/src/main/java/com/letterpaw/soap/util/Mailer.java +++ b/src/main/java/com/letterpaw/soap/util/Mailer.java @@ -5,9 +5,11 @@ import jakarta.mail.*; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; +import java.util.List; import java.util.Properties; import org.json.JSONArray; +import org.json.JSONObject; public class Mailer { static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); @@ -62,13 +64,19 @@ public class Mailer { } public static void notifyAdminNewSub(String textMsg, String adminJson) { - JSONArray resp = new JSONArray(adminJson); + System.out.println(adminJson); + JSONObject resp = new JSONObject(adminJson); if (resp.isEmpty()) { System.out.println("No admin to notify: " + textMsg); return; } - String to = resp.getJSONObject(0).getString("email"); - sendMail(to, "New subscription request to Letterpaw SOAP", textMsg); - System.out.println("Sent email to admin <" + to + "> with message: " + textMsg); +// String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; +// [{"status":200,"data":["admin@example.com","adm@email.com"]}] + JSONArray to = resp.getJSONArray("data"); + for (int i = 0; i < to.length(); i++) { + sendMail(to.getString(i), "New subscription request to Letterpaw SOAP", textMsg); + System.out.println("Sent email to admin <" + to.getString(i) + "> with message: " + textMsg); + } + } } diff --git a/src/main/java/com/letterpaw/soap/util/Request.java b/src/main/java/com/letterpaw/soap/util/Request.java index a5f4b42..760d5ae 100644 --- a/src/main/java/com/letterpaw/soap/util/Request.java +++ b/src/main/java/com/letterpaw/soap/util/Request.java @@ -1,6 +1,7 @@ package com.letterpaw.soap.util; import com.letterpaw.soap.enums.ServiceType; +import io.github.cdimascio.dotenv.Dotenv; // Make sure to import the correct HttpClient, HttpRequest, and HttpResponse import java.io.DataOutputStream; import java.net.HttpURLConnection; @@ -10,6 +11,8 @@ import java.io.InputStreamReader; import java.util.function.Consumer; public class Request { + static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); + public enum RequestType { GET, POST @@ -30,9 +33,11 @@ public class Request { try { // Create connection // Get URL - URL url = new URL("http://" + - System.getenv().getOrDefault((type == ServiceType.PHP ? "PHP_HOST" : "REST_HOST"), "localhost") + + URL url = new URL("http", + dotenv.get((type == ServiceType.PHP ? "PHP_HOST" : "REST_HOST"), "localhost"), + Integer.parseInt(dotenv.get((type == ServiceType.PHP ? "PHP_PORT" : "REST_PORT"), "8008")), endpoint); + System.out.println(url.toString()); // Open connection connection = (HttpURLConnection) url.openConnection(); // Set request type -- GitLab