From fdb2aadd1fbdac5a79fbad0493b51e4cfc071db7 Mon Sep 17 00:00:00 2001
From: Jose Semedo <jose.semedo@cern.ch>
Date: Tue, 10 Oct 2023 14:42:37 +0200
Subject: [PATCH] [#82] Add support for device type phone subtype mail2sms

---
 .gitlab-ci.yml                       |  4 ++--
 docker-compose.local.yml             |  4 +++-
 notifications_routing/preferences.py | 17 +++++++++++++++++
 notifications_routing/utils.py       | 14 ++++++++++++++
 4 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e936749..aa18263 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -65,7 +65,7 @@ lint:
     - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
     - apk add make
   tags:
-    - docker-privileged
+    - docker-privileged-xl
   rules:
     - if: $CI_MERGE_REQUEST_ID
     - when: never
@@ -91,7 +91,7 @@ unit_tests:
     - docker-compose --version
     - apk add make
   tags:
-    - docker-privileged
+    - docker-privileged-xl
   rules:
     - if: $CI_MERGE_REQUEST_ID
     - when: never
diff --git a/docker-compose.local.yml b/docker-compose.local.yml
index 9e4f554..46fbb80 100644
--- a/docker-compose.local.yml
+++ b/docker-compose.local.yml
@@ -21,4 +21,6 @@ services:
       - "pg_db:127.0.0.1"
       - "etcd:127.0.0.1"
 networks:
-  default:
+  shared:
+    external:
+      name: shared
diff --git a/notifications_routing/preferences.py b/notifications_routing/preferences.py
index 981a19d..ca5759f 100644
--- a/notifications_routing/preferences.py
+++ b/notifications_routing/preferences.py
@@ -16,6 +16,7 @@ from notifications_routing.utils import (
     convert_notification_email_to_json_string,
     convert_notification_push_to_json_string,
     is_created_time_between_allowed_range,
+    convert_notifications_sms_to_json_string,
 )
 
 
@@ -165,6 +166,10 @@ def send_to_device(publisher: megabus.Publisher, message: Dict, device: Device,
         send_live_email(publisher, message, device.token)
         return
 
+    if device.type == "PHONE" and device.subType == "MAIL2SMS":
+        send_live_sms(publisher, message, device.token)
+        return
+
     if device.type == "BROWSER":
         if device.subType == "OTHER":
             logging.info(device)
@@ -248,6 +253,18 @@ def apply_default_preferences(publisher: megabus.Publisher, message: Dict, email
     )
 
 
+def send_live_sms(publisher: Publisher, message: Dict, email: str):
+    """Send live e-mail that targets phone.
+
+    :param publisher: Publisher object used to publish messages
+    :param message: message object
+    :param email: e-mail
+    """
+    logging.debug("Sending to mail2sms:\n\t summary:%s \n\t email:%s", message[OutputMessageKeys.SUMMARY], email)
+    message = convert_notifications_sms_to_json_string(message, email)
+    publisher.send(message, extension="mail2sms", ttl=Config.TTL, headers={"persistent": "true"})
+
+
 def send_live_email(publisher: Publisher, message: Dict, email: str):
     """Send live e-mail.
 
diff --git a/notifications_routing/utils.py b/notifications_routing/utils.py
index 980cb49..7db30e5 100644
--- a/notifications_routing/utils.py
+++ b/notifications_routing/utils.py
@@ -50,6 +50,7 @@ class OutputMessageKeys(StrEnum):
     ID = "id"
     PRIVATE = "private"
     INTERSECTION = "intersection"
+    PHONE_EMAIL = "phone_email"
 
 
 class FeedFrequency(StrEnum):
@@ -92,6 +93,19 @@ def is_time_between(time, start_range, end_range):
     return start_range <= time <= end_range
 
 
+def convert_notifications_sms_to_json_string(message, email):
+    """Convert notification sms to json string."""
+    notif = {
+        "channel_id": message[OutputMessageKeys.CHANNEL_ID],
+        "notification_id": message[OutputMessageKeys.ID],
+        "message_body": message[OutputMessageKeys.MESSAGE_BODY],
+        "short_url": message[OutputMessageKeys.CHANNEL_SHORT_URL],
+        "summary": message[OutputMessageKeys.SUMMpreferences.pyARY],
+        "phone_email": email,
+    }
+    return json.dumps(notif)
+
+
 def convert_notification_email_to_json_string(message, email):
     """Convert notification e-mail to json string."""
     notif = {
-- 
GitLab