Skip to content

Commit

Permalink
[feat] user notification settings: add marketing column (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Oct 1, 2024
1 parent 009ae30 commit 45ecb12
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions fixbackend/auth/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ class UserNotificationSettingsRead(BaseModel):
weekly_report: bool = Field(description="Whether to receive a weekly report")
inactivity_reminder: bool = Field(description="Whether to receive a reminder for open incidents")
tutorial: bool = Field(description="Whether to receive tutorial emails")
marketing: bool = Field(description="Whether to receive marketing emails")

@staticmethod
def from_model(model: UserNotificationSettings) -> "UserNotificationSettingsRead":
return UserNotificationSettingsRead(
weekly_report=model.weekly_report,
inactivity_reminder=model.inactivity_reminder,
tutorial=model.tutorial,
marketing=model.marketing,
)

def to_model(self, user_id: UserId) -> UserNotificationSettings:
Expand All @@ -95,6 +97,7 @@ def to_model(self, user_id: UserId) -> UserNotificationSettings:
weekly_report=self.weekly_report,
inactivity_reminder=self.inactivity_reminder,
tutorial=self.tutorial,
marketing=self.marketing,
)


Expand All @@ -104,6 +107,7 @@ class UserNotificationSettingsWrite(BaseModel):
default=None, description="Whether to receive a reminder for open incidents"
)
tutorial: Optional[bool] = Field(default=None, description="Whether to receive tutorial emails")
marketing: Optional[bool] = Field(default=None, description="Whether to receive marketing emails")


class OTPConfig(BaseModel):
Expand Down
23 changes: 16 additions & 7 deletions fixbackend/notification/user_notification_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
@frozen
class UserNotificationSettings:
user_id: UserId
weekly_report: bool
inactivity_reminder: bool
tutorial: bool
weekly_report: bool = True
inactivity_reminder: bool = True
tutorial: bool = True
marketing: bool = True


class UserNotificationSettingsEntity(CreatedUpdatedMixin, Base):
Expand All @@ -43,13 +44,15 @@ class UserNotificationSettingsEntity(CreatedUpdatedMixin, Base):
weekly_report: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
inactivity_reminder: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
tutorial: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
marketing: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)

def to_model(self) -> UserNotificationSettings:
return UserNotificationSettings(
user_id=self.user_id,
weekly_report=self.weekly_report,
inactivity_reminder=self.inactivity_reminder,
tutorial=self.tutorial,
marketing=self.marketing,
)

@staticmethod
Expand All @@ -59,6 +62,7 @@ def from_model(settings: UserNotificationSettings) -> "UserNotificationSettingsE
weekly_report=settings.weekly_report,
inactivity_reminder=settings.inactivity_reminder,
tutorial=settings.tutorial,
marketing=settings.marketing,
)


Expand All @@ -72,9 +76,7 @@ async def get_notification_settings(self, user_id: UserId) -> UserNotificationSe
if result := await session.get(UserNotificationSettingsEntity, user_id):
return result.to_model()
else:
return UserNotificationSettings(
user_id=user_id, weekly_report=True, inactivity_reminder=True, tutorial=True
)
return UserNotificationSettings(user_id=user_id)

async def update_notification_settings(
self,
Expand All @@ -83,6 +85,7 @@ async def update_notification_settings(
weekly_report: Optional[bool] = None,
inactivity_reminder: Optional[bool] = None,
tutorial: Optional[bool] = None,
marketing: Optional[bool] = None,
) -> UserNotificationSettings:
async with self.session_maker() as session:
if isinstance(user_id_or_email, str):
Expand All @@ -99,7 +102,11 @@ async def update_notification_settings(
value = await session.get(UserNotificationSettingsEntity, user_id)
if value is None:
value = UserNotificationSettingsEntity(
user_id=user_id, weekly_report=True, inactivity_reminder=True, tutorial=True
user_id=user_id,
weekly_report=True,
inactivity_reminder=True,
tutorial=True,
marketing=True,
)
session.add(value)
if weekly_report is not None:
Expand All @@ -108,6 +115,8 @@ async def update_notification_settings(
value.inactivity_reminder = inactivity_reminder
if tutorial is not None:
value.tutorial = tutorial
if marketing is not None:
value.marketing = marketing
settings = value.to_model()
await session.commit()
return settings
Expand Down
23 changes: 23 additions & 0 deletions migrations/versions/2024-10-01T14:23:11Z_email_marketing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
user_notification_settings: add marketing column
"""

from typing import Union

import sqlalchemy as sa
from alembic import op

revision: str = "2c3086217445"
down_revision: Union[str, None] = "1e4ccaf4e087"


def upgrade() -> None:
op.add_column(
"user_notification_settings",
sa.Column(
"marketing",
sa.Boolean(),
nullable=False,
server_default="true",
),
)
4 changes: 2 additions & 2 deletions tests/fixbackend/auth/users_router_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ async def client(
async def test_user_notification_settings(client: AsyncClient) -> None:
response = await client.get("/api/users/me/settings/notifications")
assert response.status_code == 200
assert response.json() == {"inactivity_reminder": True, "weekly_report": True, "tutorial": True}
assert response.json() == {"inactivity_reminder": True, "weekly_report": True, "tutorial": True, "marketing": True}

response = await client.put("/api/users/me/settings/notifications", json={"weekly_report": False})
assert response.status_code == 200
assert response.json() == {"inactivity_reminder": True, "weekly_report": False, "tutorial": True}
assert response.json() == {"inactivity_reminder": True, "weekly_report": False, "tutorial": True, "marketing": True}
3 changes: 3 additions & 0 deletions tests/fixbackend/notification/user_notification_repo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ async def test_user_notification_settings_repo(async_session_maker: AsyncSession
assert updated.weekly_report is False
assert updated.inactivity_reminder is True
assert updated.tutorial is False
assert updated.marketing is True

# get updated settings
settings = await repo.get_notification_settings(user.id)
assert settings.user_id == user.id
assert settings.weekly_report is False
assert settings.inactivity_reminder is True
assert settings.tutorial is False
assert settings.marketing is True

# update via email settings
updated = await repo.update_notification_settings(
Expand All @@ -56,3 +58,4 @@ async def test_user_notification_settings_repo(async_session_maker: AsyncSession
assert updated.weekly_report is True
assert updated.inactivity_reminder is False
assert updated.tutorial is True
assert updated.marketing is True

0 comments on commit 45ecb12

Please sign in to comment.