Skip to content

Commit

Permalink
feat(proofs): New proof.currency field (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 19, 2024
1 parent bc8cd49 commit b9d2c90
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Add Proof currency field
Revision ID: 49a828f10b05
Revises: 5f5835d81917
Create Date: 2024-06-19 15:36:55.542647
"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "49a828f10b05"
down_revision: Union[str, None] = "5f5835d81917"
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("proofs", sa.Column("currency", sa.String(length=3), nullable=True))
# Set the currency to the currency of the first price for each proof
op.execute(
"""
UPDATE proofs
SET currency = (
SELECT currency
FROM prices
WHERE prices.proof_id = proofs.id
LIMIT 1
)
WHERE type IN ('PRICE_TAG', 'RECEIPT')
"""
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("proofs", "currency")
# ### end Alembic commands ###
5 changes: 4 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ class Proof(Base):
Integer, nullable=False, server_default="0", index=True
)

date = mapped_column(Date)
currency: Mapped[CurrencyEnum] = mapped_column(
ChoiceType(CurrencyEnum), nullable=True
)
date = mapped_column(Date, nullable=True)

owner = mapped_column(String, index=True)

Expand Down
13 changes: 13 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ class ProofFull(BaseModel):
price_count: int = Field(
description="number of prices for this proof.", examples=[15], default=0
)
currency: CurrencyEnum | None = Field(
description="currency of the price, as a string. "
"The currency must be a valid currency code. "
"See https://en.wikipedia.org/wiki/ISO_4217 for a list of valid currency codes.",
examples=["EUR", "USD"],
)
date: datetime.date | None = Field(
description="date of the proof.", examples=["2024-01-01"]
)
Expand All @@ -202,6 +208,7 @@ class ProofFull(BaseModel):

class ProofBasicUpdatableFields(BaseModel):
type: ProofTypeEnum | None = None
currency: CurrencyEnum | None = None
date: datetime.date | None = None

class Config:
Expand Down Expand Up @@ -491,6 +498,12 @@ class ProofFilter(Filter):
price_count: Optional[int] | None = None
price_count__gte: Optional[int] | None = None
price_count__lte: Optional[int] | None = None
currency: Optional[str] | None = None
date: Optional[str] | None = None
date__gt: Optional[str] | None = None
date__gte: Optional[str] | None = None
date__lt: Optional[str] | None = None
date__lte: Optional[str] | None = None

order_by: Optional[list[str]] | None = None

Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ def test_get_proofs(db_session, user_session: SessionModel, clean_proofs):
"mimetype",
"type",
"price_count",
"currency",
"date",
"owner",
"created",
Expand Down Expand Up @@ -1042,15 +1043,17 @@ def test_update_proof(
)
assert response.status_code == 200
assert response.json()["type"] == "RECEIPT"
assert response.json()["currency"] is None
assert response.json()["date"] is None
PROOF_UPDATE_PARTIAL = {"date": "2024-01-01"}
PROOF_UPDATE_PARTIAL = {"currency": "EUR", "date": "2024-01-01"}
response = client.patch(
f"/api/v1/proofs/{proof.id}",
headers={"Authorization": f"Bearer {user_session.token}"},
json=jsonable_encoder(PROOF_UPDATE_PARTIAL),
)
assert response.status_code == 200
assert response.json()["type"] == "RECEIPT"
assert response.json()["currency"] == "EUR"
assert response.json()["date"] == "2024-01-01"

# with authentication and proof owner but extra fields
Expand Down

0 comments on commit b9d2c90

Please sign in to comment.