Skip to content

Commit

Permalink
[feat] user notification settings: add marketing column
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Oct 1, 2024
1 parent 009ae30 commit 7afc01a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
17 changes: 11 additions & 6 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 @@ -108,6 +111,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",
),
)

0 comments on commit 7afc01a

Please sign in to comment.