2019-10-18 16:09:39 +01:00
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
2021-01-06 12:12:01 +00:00
|
|
|
|
from app.formatters import format_thousands
|
2019-10-18 16:09:39 +01:00
|
|
|
|
from app.models import ModelList
|
|
|
|
|
|
from app.notify_client.service_api_client import service_api_client
|
2024-05-16 10:37:37 -04:00
|
|
|
|
from notifications_utils.formatters import formatted_list
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Event(ABC):
|
|
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
item,
|
|
|
|
|
|
key=None,
|
|
|
|
|
|
value_from=None,
|
|
|
|
|
|
value_to=None,
|
|
|
|
|
|
):
|
|
|
|
|
|
self.item = item
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.time = item["updated_at"] or item["created_at"]
|
|
|
|
|
|
self.user_id = item["created_by_id"]
|
2019-10-18 16:09:39 +01:00
|
|
|
|
self.key = key
|
|
|
|
|
|
self.value_from = value_from
|
|
|
|
|
|
self.value_to = value_to
|
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def relevant(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceCreationEvent(Event):
|
|
|
|
|
|
relevant = True
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Created this service and called it ‘{}’".format(self.item["name"])
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceEvent(Event):
|
|
|
|
|
|
@property
|
|
|
|
|
|
def relevant(self):
|
|
|
|
|
|
return self.value_from != self.value_to and bool(self._formatter)
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
|
return self._formatter()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def _formatter(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return getattr(self, "format_{}".format(self.key), None)
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_restricted(self):
|
|
|
|
|
|
if self.value_to is False:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Made this service live"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
if self.value_to is True:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Put this service back into trial mode"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_active(self):
|
|
|
|
|
|
if self.value_to is False:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Deleted this service"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
if self.value_to is True:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Unsuspended this service"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_contact_link(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Set the contact details for this service to ‘{}’".format(self.value_to)
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_inbound_api(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Updated the callback for received text messages"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_message_limit(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return ("{} this service’s daily message limit from {} to {}").format(
|
|
|
|
|
|
"Reduced" if self.value_from > self.value_to else "Increased",
|
2019-10-18 16:09:39 +01:00
|
|
|
|
format_thousands(self.value_from),
|
|
|
|
|
|
format_thousands(self.value_to),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def format_name(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return ("Renamed this service from ‘{}’ to ‘{}’").format(
|
2019-10-18 16:09:39 +01:00
|
|
|
|
self.value_from, self.value_to
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def format_permissions(self):
|
|
|
|
|
|
added = list(sorted(set(self.value_to) - set(self.value_from)))
|
|
|
|
|
|
removed = list(sorted(set(self.value_from) - set(self.value_to)))
|
|
|
|
|
|
if removed and added:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Removed {} from this service’s permissions, added {}".format(
|
2019-10-18 16:09:39 +01:00
|
|
|
|
formatted_list(removed),
|
|
|
|
|
|
formatted_list(added),
|
|
|
|
|
|
)
|
|
|
|
|
|
if added:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Added {} to this service’s permissions".format(
|
2019-10-18 16:09:39 +01:00
|
|
|
|
formatted_list(added)
|
|
|
|
|
|
)
|
|
|
|
|
|
if removed:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Removed {} from this service’s permissions".format(
|
2019-10-18 16:09:39 +01:00
|
|
|
|
formatted_list(removed)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def format_prefix_sms(self):
|
|
|
|
|
|
if self.value_to is True:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Set text messages to start with the name of this service"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Set text messages to not start with the name of this service"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_research_mode(self):
|
|
|
|
|
|
if self.value_to is True:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Put this service into research mode"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Took this service out of research mode"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
def format_service_callback_api(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return "Updated the callback for delivery receipts"
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class APIKeyEvent(Event):
|
|
|
|
|
|
relevant = True
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if self.item["updated_at"]:
|
|
|
|
|
|
return ("Revoked the ‘{}’ API key").format(self.item["name"])
|
2019-10-18 16:09:39 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return ("Created an API key called ‘{}’").format(self.item["name"])
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class APIKeyEvents(ModelList):
|
|
|
|
|
|
model = APIKeyEvent
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = service_api_client.get_service_api_key_history
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceEvents(ModelList):
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = service_api_client.get_service_service_history
|
2019-10-18 16:09:39 +01:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def model(self):
|
|
|
|
|
|
return lambda x: x
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def splat(events):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
for index, item in enumerate(
|
|
|
|
|
|
sorted(events, key=lambda event: event["updated_at"] or event["created_at"])
|
|
|
|
|
|
):
|
2019-10-18 16:09:39 +01:00
|
|
|
|
if index == 0:
|
|
|
|
|
|
yield ServiceCreationEvent(item)
|
|
|
|
|
|
else:
|
|
|
|
|
|
for key in sorted(item.keys()):
|
|
|
|
|
|
yield ServiceEvent(
|
|
|
|
|
|
item,
|
|
|
|
|
|
key,
|
|
|
|
|
|
events[index - 1][key],
|
|
|
|
|
|
events[index][key],
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, service_id):
|
|
|
|
|
|
self.items = [
|
2023-08-25 09:12:23 -07:00
|
|
|
|
event
|
|
|
|
|
|
for event in self.splat(self.client_method(service_id))
|
|
|
|
|
|
if event.relevant
|
2019-10-18 16:09:39 +01:00
|
|
|
|
]
|