From 539f5ca86049eeac3bfa8ac8cc3cbd9683eec2c1 Mon Sep 17 00:00:00 2001
From: vissutagunawan <vglim3653@gmail.com>
Date: Thu, 27 Mar 2025 10:12:37 +0700
Subject: [PATCH] Implement unsubscribe function in Notification service.

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

diff --git a/src/service/notification.rs b/src/service/notification.rs
index aa0e757..2dbbdb4 100644
--- a/src/service/notification.rs
+++ b/src/service/notification.rs
@@ -48,4 +48,42 @@ use rocket::http::Status;
             .join()
             .unwrap();
     }
+
+    async fn unsubscribe_request(product_type: String) -> Result<SubscriberRequest> {
+        let product_type_upper = product_type.to_uppercase();
+        let product_type_str = product_type_upper.as_str();
+        let notification_reciever_url = format!("{}/recieve", APP_CONFIG.get_instance_root_url());
+
+        let request_url = format!(
+            "{}/notification/unsubscribe/{}?url={}",
+            APP_CONFIG.get_instance_root_url(),
+            product_type_str,
+            notification_reciever_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(f) => match f.json::<SubscriberRequest>().await {
+                Ok(x) => Ok(x),
+                Err(_) => Err(compose_error_response(
+                    Status::NotAcceptable,
+                    String::from("Already unsubscribe from topic"),
+                )),
+            },
+            Err(e) => Err(compose_error_response(Status::NotFound, e.to_string())),
+        };
+    }
+
+    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();
+    }
  }
\ No newline at end of file
-- 
GitLab