Skip to content
Snippets Groups Projects
Commit 2e5d1c69 authored by Christof Bauer's avatar Christof Bauer
Browse files

Merge branch 'main' into production

parents b04d3da7 8764f5c5
No related branches found
Tags release/1.48.0
No related merge requests found
......@@ -169,8 +169,11 @@ class Mutation:
)
insights = insight_model.objects.order_by("-date_published")[:3]
subscription_token = str(uuid.uuid4())
NotificationsService.send_welcome_email_series_notification(
user=current_user, spaces=spaces, insights=insights
user=current_user, spaces=spaces, insights=insights, token=subscription_token
)
updated_fields = _get_updated_fields(current_user_fields_before, current_user_fields_after)
......@@ -296,3 +299,9 @@ class Mutation:
current_user.remove_insight_bookmark(insight_id) # To maintain compatibility with older versions
UserFavorite.unfavorite(user=current_user, favoritable_id=insight_id, favoritable_type="insight")
return Result(success=True)
@strawberry.mutation()
@sync_to_async
def unsubscribe_from_newsletter(self, info, token: str) -> Result:
NotificationApi.cancel_workflow(transaction_id=token)
return Result(success=True)
......@@ -1534,3 +1534,22 @@ class TestUsers:
response = get_connections_by_user_id_query(user_id=user1.id)
assert_is_unauthorized(response)
@mock.patch("openbook_notifications.api.NotificationApi.cancel_workflow")
def test_can_unsubscribe_from_newsletter(self, mock_cancel_workflow):
token = str(uuid.uuid4())
executed = async_to_sync(schema.execute)(
"""
mutation test($token: String!) {
unsubscribeFromNewsletter(token: $token) {
success
}
}
""",
context_value=benedict({"request": {"user": AnonymousUser}}),
variable_values={"token": token},
)
assert executed.errors is None
mock_cancel_workflow.assert_called_once_with(transaction_id=token)
assert executed.data["unsubscribeFromNewsletter"]["success"] is True
......@@ -364,7 +364,3 @@ class Query:
offset,
limit,
)
@strawberry_django.field()
def get_stuff(self) -> List[SpaceUserConnectionType]:
return [SpaceUserConnectionType.MEMBER, SpaceUserConnectionType.CREATOR]
......@@ -391,10 +391,11 @@ class WelcomeEmailSeriesNotification(Notification):
self,
user: str,
payload: WelcomeEmailSeriesPayload,
token: str,
):
super().__init__(
workflow=Workflow.WELCOME_EMAIL_SERIES.value,
recipients=user,
payload=payload.to_dict(),
transaction_id=user, # user id is used as transaction id so that we can easily unsubscribe from the email series
transaction_id=token, # token is used as transaction_id to cancel the workflow and unsubscribe the user from newsletter
)
......@@ -613,12 +613,13 @@ class CommentReactionPayload(Payload):
class WelcomeEmailSeriesPayload(Payload):
NOTIFICATION_VERSION = "1.0.0"
def __init__(self, user: User, spaces, insights):
def __init__(self, user: User, spaces, insights, token):
super().__init__(
data={
"user": UserData.from_user(user).to_dict(),
"recommendedSpaces": [SpaceData.from_community(space).to_dict() for space in spaces],
"recommendedInsights": [InsightData.from_insight(insight).to_dict() for insight in insights],
"token": token,
},
notification_type=NotificationType.WELCOME_EMAIL_SERIES.value,
notification_version=WelcomeEmailSeriesPayload.NOTIFICATION_VERSION,
......
......@@ -362,9 +362,10 @@ class NotificationsService:
notification.send()
@staticmethod
def send_welcome_email_series_notification(user: User, spaces, insights):
def send_welcome_email_series_notification(user: User, spaces, insights, token):
notification = WelcomeEmailSeriesNotification(
user=str(user.username),
payload=WelcomeEmailSeriesPayload(user=user, spaces=spaces, insights=insights),
payload=WelcomeEmailSeriesPayload(user=user, spaces=spaces, insights=insights, token=token),
token=token,
)
notification.send()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment