From 864be1eeda42bc16729c918ae58b5c359b866fc1 Mon Sep 17 00:00:00 2001
From: dzak27567 <fadhlurohmandzaki@gmail.com>
Date: Thu, 27 Mar 2025 14:02:27 +0700
Subject: [PATCH] Implement unsubscribe function in Notification service.

---
 src/service/notification.rs | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/src/service/notification.rs b/src/service/notification.rs
index 8f0cbde..f5e5494 100644
--- a/src/service/notification.rs
+++ b/src/service/notification.rs
@@ -54,5 +54,42 @@ impl NotificationService {
         };
     }
 
+    pub fn unsubscribe(product_type: &str) -> Result<SubscriberRequest> {
+        let product_type_clone = String::from(product_type);
+        return thread::spawn(move || Self::unsubscribe_request(product_type_clone))
+            .join().unwrap();
+    }
+    
+    #[tokio::main]
+    async fn unsubscribe_request(product_type: String) -> Result<SubscriberRequest> {
+        let product_type_upper: String = product_type.to_uppercase();
+        let product_type_str: &str = product_type_upper.as_str();
+        let notification_receiver_url: String = format!("{}/receive", 
+            APP_CONFIG.get_instance_root_url());
+        
+        let request_url: String = format!("{}/notification/unsubscribe/{}/{}",
+            APP_CONFIG.get_publisher_root_url(), product_type_str, notification_receiver_url);
+        let request = REQWEST_CLIENT
+            .post(request_url.clone())
+            .header("Content-Type", "application/json")
+            .header("Accept", "application/json")
+            .send().await;
+        log::warn_!("Sent unsubscribe request to: {}", request_url);
+        
+        return match request {
+            Ok(r) => match r.json::<SubscriberRequest>().await {
+                Ok(x) => Ok(x),
+                Err(_) => Err(compose_error_response(
+                    Status::NotFound,
+                    String::from("Already unsubscribed to the topic.")
+                ))
+            },
+            Err(e) => Err(compose_error_response(
+                Status::NotFound,
+                e.to_string()
+            ))
+        }
+    }
+
     
 }
\ No newline at end of file
-- 
GitLab