diff --git a/alembic/versions/20240619_1536_49a828f10b05_add_proof_currency_field.py b/alembic/versions/20240619_1536_49a828f10b05_add_proof_currency_field.py new file mode 100644 index 00000000..b848bff1 --- /dev/null +++ b/alembic/versions/20240619_1536_49a828f10b05_add_proof_currency_field.py @@ -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 ### diff --git a/app/models.py b/app/models.py index a3919724..65f4013d 100644 --- a/app/models.py +++ b/app/models.py @@ -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) diff --git a/app/schemas.py b/app/schemas.py index d029b354..90886897 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -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"] ) @@ -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: @@ -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 diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index a99bcf10..6d59787e 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -912,6 +912,7 @@ def test_get_proofs(db_session, user_session: SessionModel, clean_proofs): "mimetype", "type", "price_count", + "currency", "date", "owner", "created", @@ -1042,8 +1043,9 @@ 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}"}, @@ -1051,6 +1053,7 @@ def test_update_proof( ) 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