Skip to content

Commit

Permalink
feat(prices): new source field to store the app name (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 4, 2024
1 parent 4eda331 commit 9efd923
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Add Price source field
Revision ID: 8a6892a20b5d
Revises: 2737db183adb
Create Date: 2024-05-25 20:06:18.409941
"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "8a6892a20b5d"
down_revision: Union[str, None] = "2737db183adb"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("prices", sa.Column("source", sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("prices", "source")
# ### end Alembic commands ###
6 changes: 4 additions & 2 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ def get_price_by_id(db: Session, id: int) -> Price | None:
return db.query(Price).filter(Price.id == id).first()


def create_price(db: Session, price: PriceCreate, user: UserCreate) -> Price:
db_price = Price(**price.model_dump(), owner=user.user_id)
def create_price(
db: Session, price: PriceCreate, user: UserCreate, source: str = None
) -> Price:
db_price = Price(**price.model_dump(), owner=user.user_id, source=source)
db.add(db_price)
db.commit()
db.refresh(db_price)
Expand Down
2 changes: 2 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class Price(Base):

owner: Mapped[str] = mapped_column(String)

source = mapped_column(String, nullable=True)

created = mapped_column(DateTime(timezone=True), server_default=func.now())

__tablename__ = "prices"
3 changes: 2 additions & 1 deletion app/routers/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_price(
price: schemas.PriceCreateWithValidation,
background_tasks: BackgroundTasks,
current_user: schemas.UserCreate = Depends(get_current_user),
app_name: str | None = None,
db: Session = Depends(get_db),
) -> Price:
"""
Expand All @@ -58,7 +59,7 @@ def create_price(
detail="Proof does not belong to current user",
)
# create price
db_price = crud.create_price(db, price=price, user=current_user)
db_price = crud.create_price(db, price=price, user=current_user, source=app_name)
# update counts
background_tasks.add_task(tasks.create_price_product, db, price=db_price)
background_tasks.add_task(tasks.create_price_location, db, price=db_price)
Expand Down
5 changes: 5 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ class PriceFull(PriceCreate):
product_id: int | None
location_id: int | None
owner: str
# source: str | None = Field(
# description="Source (App name)",
# examples=["web app", "mobile app"],
# default=None,
# )
created: datetime.datetime


Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ def test_create_price(db_session, user_session: SessionModel, clean_prices):
assert response.json()["product_code"] == PRICE_2.product_code
assert len(crud.get_prices(db_session)) == 1 + 1
# assert db_prices[0]["owner"] == user.user_id
# with app_name
response = client.post(
"/api/v1/prices?app_name=test",
json=jsonable_encoder(PRICE_1),
headers={"Authorization": f"Bearer {user_session.token}"},
)
assert response.status_code == 201
assert response.json()["product_code"] == PRICE_1.product_code
assert len(crud.get_prices(db_session)) == 2 + 1
assert crud.get_prices(db_session)[0][0].source == "test"


def test_create_price_moderator(db_session, user_session, user_session_1, clean_prices):
Expand Down

0 comments on commit 9efd923

Please sign in to comment.