Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pyjade = "===4.0.0"
Jinja2 = "===2.10.1"
MarkupSafe = "===0.23"
pycrypto = "===2.6.1"
sanic = "==22.12.0"
sanic-ext = "23.12.0"
sanic-openapi = "21.12.0"

[dev-packages]

Expand Down
4 changes: 3 additions & 1 deletion app/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .constants import SyncDispatcher, EventType
from .constants import SyncDispatcher
from .error_messages import ErrorMessages
from .notification_channels import NotificationChannels
from .event_priority import EventPriority
from .event import EventType
from .database_tables import DatabaseTables
from .notification_request_log_status import NotificationRequestLogStatus
from .email import Email
from .whatsapp import VariableMappingKeys
from .providers import Providers, ProvidersStatus
from .providers_default_priority import ProvidersDefaultPriorityStatus
from .providers_dynamic_priority import ProvidersDynamicPriorityStatus
25 changes: 18 additions & 7 deletions app/constants/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from enum import Enum
from commonutils.utils import CustomEnum


URL_SHORTENER_START_PATTERN = "__URL_SHORTNER_START__"
URL_SHORTENER_END_PATTERN = "__URL_SHORTNER_END__"
MAX_DEVICES_FOR_PUSH = 10
TRUNCATE_MAX_LEN = 5000


class Action(Enum):
Expand All @@ -22,8 +26,11 @@ class Event:
"priority",
"event_type",
]
TRIGGER_LIMIT = 'trigger_limit'
ADD_ACTION_REQUIRED_PARAMS = ["app_name", "event_name", "user_email"]
EVENT_UPDATE_REQUIRED_PARAMS = ['app_name', 'event_name', 'event_type', 'priority', 'callback_enabled', 'payload']
EVENT_PRIORITY = "priority"
DYNAMIC_CHANNELS = "dynamic_channels"
EVENT_TYPE = "event_type"
NEW_EVENT_CREATED_MESSAGE = "New event created with event_name = {event_name},app_name = {app_name} by {user_email}."
NEW_ACTION_ADDED_MESSAGE = "New action added in event_name = {event_name},app_name = {app_name} by {user_email}."
Expand All @@ -37,14 +44,18 @@ class Event:
SOFT_DELETE_DEFAULT_VALUE = False
DEFAULT_LIMIT = 1000
DEFAULT_OFFSET = 0


class EventType(Enum):
PROMOTIONAL = "promotional"
TRANSACTIONAL = "transactional"
OTHER = "other"

ID = "id"

class SyncDispatcher:
ENDPOINT = "/notify"
METHOD = "POST"


class Redis:
REDIS_NAMESPACE = 'notification_core_redis'
REDIS_EXPIRY_TIME_MED = 60 * 60
KEY_DELIMITER = '_'


class RedisKeyPrefixes:
EVENT_BODY_LATEST_VERSION = 'event_body_latest_version'
1 change: 1 addition & 0 deletions app/constants/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Email:
SUBJECT = 'subject'
DESCRIPTION = 'description'
CONTENT = 'content'
ID = 'id'
NAME = 'name'
CONTENT_COLUMNS=['id', 'event_id', 'name', 'description', 'subject', 'content','updated_by']
REDIS_EXPIRY_TIME_LONG = 24 * 60 * 60
Expand Down
19 changes: 19 additions & 0 deletions app/constants/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from enum import Enum
from commonutils.utils import CustomEnum


class EventPriority(CustomEnum):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilise the existing one

CRITICAL = "critical"
HIGH = "high"
MEDIUM = "medium"
LOW = "low"

@classmethod
def _missing_(cls, _value):
return cls.LOW


class EventType(Enum):
PROMOTIONAL = "promotional"
TRANSACTIONAL = "transactional"
OTP = "otp"
15 changes: 11 additions & 4 deletions app/constants/notification_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ def get_sent_to_for_channel(cls, channel, request_body) -> list:
send_address = list()
if channel in [cls.EMAIL.value]:
send_address = request_body.get('to', {}).get('email') or list()
send_address = [send_address[0]] if send_address else list()
elif channel in [cls.SMS.value, cls.WHATSAPP.value]:
send_address = request_body.get('to', {}).get('mobile') or list()
send_address = [send_address[0]] if send_address else list()
elif channel in [cls.PUSH.value]:
send_address = request_body.get('to', {}).get('device') or list()
return send_address

@classmethod
def get_source_identifier_for_event(cls, request_body):
return request_body["source_identifier"]
def get_source_identifier_for_event(cls, event_name, request_body):
# TODO add more logic for other apps of needed
source_identifier = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discuss?

if 'body' in request_body:
body = request_body['body'] or dict()
if 'order' in body:
order = body['order'] or dict()
if 'order_id' in order or 'appointment_id' in order:
source_identifier = order.get('order_id') or order.get('appointment_id')
return source_identifier

65 changes: 57 additions & 8 deletions app/constants/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class Providers(CustomEnum):
NotificationChannels.EMAIL.value
],
"configuration": {
"API_KEY": "",
"AWS_ACCESS_KEY_ID": "",
"AWS_ACCESS_KEY_SECRET": "",
"API_KEY": ""
}
}

Expand Down Expand Up @@ -63,10 +61,11 @@ class Providers(CustomEnum):
NotificationChannels.SMS.value
],
"configuration": {
"PLIVO_SMS_URL": "",
"PLIVO_AUTH_ID": "",
"PLIVO_AUTH_TOKEN": "",
"PLIVO_SENDER_ID": "",
"PLIVO_CALLBACK_URL": ""
"PLIVO_CALLBACK_URL": "",
}
}

Expand All @@ -81,7 +80,13 @@ class Providers(CustomEnum):
"SMS_COUNTRY_USERNAME": "",
"SMS_COUNTRY_PASSWORD": "",
"SMS_COUNTRY_SENDER_ID": "",
"SMS_COUNTRY_URL": ""
"SMS_COUNTRY_OTP_USERNAME": "",
"SMS_COUNTRY_OTP_PASSWORD": "",
"SMS_COUNTRY_OTP_SENDER_ID": "",
"SMS_COUNTRY_DROPLET_USERNAME": "",
"SMS_COUNTRY_DROPLET_PASSWORD": "",
"SMS_COUNTRY_DROPLET_SENDER_ID": "",
"SMS_COUNTRY_URL": "",
}
}

Expand All @@ -93,7 +98,36 @@ class Providers(CustomEnum):
NotificationChannels.PUSH.value
],
"configuration": {
"AUTH_KEY": ""
"PROJECT_ID": "",
"TYPE": "",
"PRIVATE_KEY_ID": "",
"PRIVATE_KEY": "",
"CLIENT_EMAIL": "",
"CLIENT_ID": "",
"TOKEN_URI": "",
"AUTH_URI": "",
"AUTH_PROVIDER_X509_CERT_URL": "",
"CLIENT_X509_CERT_URL": "",
"UNIVERSE_DOMAIN": ""
}
}

APNS = {
"name": "Apple Push Notification Service",
"code": "APNS",
"logo": "https://raw.githubusercontent.com/tata1mg/notifyone-core/refs/heads/notifyone/issues/14/media/logo/apns.png",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

"channels": [
NotificationChannels.PUSH.value
],
"configuration": {
"HOST": "",
"ENDPOINT": "",
"BUNDLE_IDENTIFIER": "",
"REFRESH_TOKEN_DELAY": 1800,
"ALGORITHM": "",
"TEAM_ID": "",
"KEY_ID": "",
"PRIVATE_KEY": ""
}
}

Expand All @@ -105,8 +139,23 @@ class Providers(CustomEnum):
NotificationChannels.WHATSAPP.value
],
"configuration": {
"CAPACITY": 0,
"AUTHORIZATION_TOKEN": ""
"APP_AUTHORIZATIONS": {
"corporate-service": "CORPORATE",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be in config and not part of code.

"diagnostics": "DIAGNOSTICS",
"health_records": "DIAGNOSTICS",
"off": "PHARMACY",
"ppmc_api": "CORPORATE",
"validation_service": "PHARMACY",
"verification": "PHARMACY"
},
"AUTHORIZATION": {
"CORPORATE": "",
"DEFAULT": "",
"DIAGNOSTICS": "",
"PHARMACY": ""
},
"HOST": "https://api.interakt.ai",
"PATH": "/v1/public/message/"
}
}

Expand Down
11 changes: 8 additions & 3 deletions app/constants/push.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from commonutils.utils import CustomEnum
from enum import Enum


class PushTarget:
Expand Down Expand Up @@ -87,11 +87,16 @@ class PushTarget:
}


class DeviceType(CustomEnum):
class DeviceType(Enum):
ALL = "ALL"
iOS = "IOS"
iOS = "IPHONE OS"
ANDROID = "ANDROID"

class NotificationType(Enum):
BANNER = "BANNER"
CALL = "CALL"
LIVE_ACTIVITY = "LIVE_ACTIVITY"
BACKGROUND = "BACKGROUND"

class Push:
TITLE = "title"
Expand Down
13 changes: 13 additions & 0 deletions app/constants/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum


class ExecutionDetailsSource(str, Enum):
INTERNAL = "INTERNAL"
WEBHOOK = "WEBHOOK"


class ExecutionDetailsEventStatus(str, Enum):
QUEUED = "QUEUED"
PENDING = "PENDING"
SUCCESS = "SUCCESS"
FAILED = "FAILED"
7 changes: 6 additions & 1 deletion app/constants/whatsapp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from enum import Enum
class Whatsapp:
CONTENT_COLUMNS=['id','event_id','name']
NAME = 'name'


class VariableMappingKeys(Enum):
BODY = "body"
HEADER = "header"
BUTTON = "button"
25 changes: 24 additions & 1 deletion app/listeners/listener.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from torpedo import CONFIG
from torpedo.constants import ListenerEventTypes

from asyncio import BaseEventLoop
from app.repositories.content_log import ContentLogRepository
from app.services import SubscribeNotificationRequest, SubscribeStatusUpdate
from app.services.email import EmailHandler
Expand Down Expand Up @@ -37,11 +37,34 @@ async def setup_repositories(app, loop):
)


def exc_handler(self: BaseEventLoop, context: dict) -> None:
"""
Custom exception handler to filter out specific messages.
:param self: The event loop instance
:param context: The context dictionary containing exception details
:return: None
"""
message = context.get("message")
if message not in ["Unclosed connector", "Unclosed client session"]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check if this for excluding these only.

BaseEventLoop.default_exception_handler(self, context)


async def setup_custom_exc_handler(app, loop):
"""
Set up the custom exception handler for the event loop.
:param app: The current app instance
:param loop: The running event loop
:return: None
"""
loop.set_exception_handler(exc_handler)


listeners = [
(subscribe_for_notification_requests, ListenerEventTypes.AFTER_SERVER_START.value),
(subscribe_for_status_updates, ListenerEventTypes.AFTER_SERVER_START.value),
(setup_notification_channel_handlers, ListenerEventTypes.AFTER_SERVER_START.value),
(initialize_jinja_environment, ListenerEventTypes.AFTER_SERVER_START.value),
(setup_repositories, ListenerEventTypes.AFTER_SERVER_START.value),
(setup_options, ListenerEventTypes.BEFORE_SERVER_START.value),
(setup_custom_exc_handler, ListenerEventTypes.BEFORE_SERVER_START.value),
]
21 changes: 15 additions & 6 deletions app/manager/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@

class BaseManager:
@classmethod
async def update_trigger_limit(cls, event_type: str, agent_id: str, **kwargs):
async def update_trigger_limit_or_actions(cls, event_type: str, agent_id: str, **kwargs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be two different methods

app_name = kwargs["payload"].get("app_name")
event_name = kwargs["payload"].get("event_name")
event_id = kwargs["payload"].get("event_id")
data = await cls.common_validation_checks(app_name, event_name, event_id)
event_limit = kwargs["payload"].get("triggers_limit")

event_action = kwargs["payload"].get("actions")
values = {
"updated_by": agent_id,
}

if event_limit:
triggers_limit = data.triggers_limit
# as in some cases trigger_limit is instance of str.
triggers_limit[event_type] = int(event_limit)
updated_trigger_limit = triggers_limit
values = {
"updated_by": agent_id,
"triggers_limit": json.dumps(updated_trigger_limit),
}
values["triggers_limit"] = triggers_limit

if 'actions' in kwargs["payload"] and event_action is not None:
actions = data.actions
# as in some cases actions is instance of str.
actions[event_type] = int(event_action)
values["actions"] = actions

if event_limit or 'actions' in kwargs["payload"] and event_action is not None:
await EventRepository.update_event(app_name, event_name, values=values)

@staticmethod
Expand Down
Loading