Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/karl/feature/opt-out'
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhigley committed Jul 29, 2024
2 parents e1b9686 + 871cace commit 003d97d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ repos:
- id: ruff
args: [--fix]
name: auto-fix Python lint errors
exclude: ^poprox-db/migrations/versions
# Run the formatter.
- id: ruff-format
name: format Python source
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""add web_login table
Revision ID: 98ff1c3ebe33
Revises: 211c6b3ac5de
Create Date: 2024-07-26 13:52:10.169410
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB

# revision identifiers, used by Alembic.
revision: str = "98ff1c3ebe33"
down_revision: Union[str, None] = "211c6b3ac5de"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_table(
"web_logins",
sa.Column(
"web_login_id",
sa.UUID,
primary_key=True,
server_default=sa.text("gen_random_uuid()"),
),
sa.Column("account_id", sa.UUID, nullable=False),
sa.Column("newsletter_id", sa.UUID, nullable=True),
sa.Column("endpoint", sa.String, nullable=False),
sa.Column("data", JSONB, nullable=False), # not raw_data -- only contains extra url parameters.
sa.Column("created_at", sa.DateTime, nullable=False, server_default=sa.text("NOW()")),
)

op.create_foreign_key(
"fk_web_logins_accounts_account_id",
"web_logins",
"accounts",
["account_id"],
["account_id"],
)

op.create_foreign_key(
"fk_web_logins_newsletters_newsletter_id",
"web_logins",
"newsletters",
["newsletter_id"],
["newsletter_id"],
)


def downgrade() -> None:
op.drop_constraint(
"fk_web_logins_accounts_account_id",
"web_logins",
type_="foreignkey",
)
op.drop_constraint(
"fk_web_logins_newsletters_newsletter_id",
"web_logins",
type_="foreignkey",
)

op.drop_table("web_logins")
14 changes: 13 additions & 1 deletion src/poprox_storage/repositories/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

from poprox_concepts import Account
from poprox_concepts.api.tracking import LoginLinkData
from poprox_storage.repositories.data_stores.db import DatabaseRepository

logger = logging.getLogger(__name__)
Expand All @@ -23,11 +24,12 @@ def __init__(self, connection: Connection):
super().__init__(connection)
self.tables = self._load_tables(
"accounts",
"expt_allocations",
"expt_assignments",
"expt_phases",
"expt_treatments",
"subscriptions",
"account_consent_log",
"web_logins",
)

def fetch_accounts(self, account_ids: list[UUID] | None = None) -> list[Account]:
Expand Down Expand Up @@ -147,6 +149,16 @@ def update_status(self, account_id: UUID, new_status: str):
query = sqlalchemy.update(account_tbl).values(status=new_status).where(account_tbl.c.account_id == account_id)
self.conn.execute(query)

def store_login(self, link_data: LoginLinkData):
web_login_tbl = self.tables["web_logins"]
query = sqlalchemy.insert(web_login_tbl).values(
account_id=link_data.account_id,
newsletter_id=link_data.newsletter_id,
endpoint=link_data.endpoint,
data=link_data.data,
)
self.conn.execute(query)

create_new_account = store_new_account
create_subscription_for_account = store_subscription_for_account
end_subscription_for_account = remove_subscription_for_account
Expand Down

0 comments on commit 003d97d

Please sign in to comment.