Skip to content

Commit 68adc06

Browse files
committed
add last ordering date of user
1 parent 726cf5b commit 68adc06

File tree

5 files changed

+87
-8
lines changed

5 files changed

+87
-8
lines changed

app/modules/amap/cruds_amap.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
from collections.abc import Sequence
5+
from datetime import datetime
56

67
from sqlalchemy import delete, select, update
78
from sqlalchemy.ext.asyncio import AsyncSession
@@ -349,7 +350,7 @@ async def add_cash(db: AsyncSession, user_id: str, amount: float):
349350
await db.execute(
350351
update(models_amap.Cash)
351352
.where(models_amap.Cash.user_id == user_id)
352-
.values(user_id=balance.user_id, balance=balance.balance + amount),
353+
.values(balance=balance.balance + amount),
353354
)
354355
await db.flush()
355356

@@ -363,7 +364,21 @@ async def remove_cash(db: AsyncSession, user_id: str, amount: float):
363364
await db.execute(
364365
update(models_amap.Cash)
365366
.where(models_amap.Cash.user_id == user_id)
366-
.values(user_id=balance.user_id, balance=balance.balance - amount),
367+
.values(balance=balance.balance - amount),
368+
)
369+
await db.flush()
370+
371+
372+
async def update_last_ordering_date(db: AsyncSession, user_id: str, date: datetime):
373+
result = await db.execute(
374+
select(models_amap.Cash).where(models_amap.Cash.user_id == user_id),
375+
)
376+
balance = result.scalars().first()
377+
if balance is not None:
378+
await db.execute(
379+
update(models_amap.Cash)
380+
.where(models_amap.Cash.user_id == user_id)
381+
.values(last_ordering_date=date),
367382
)
368383
await db.flush()
369384

app/modules/amap/endpoints_amap.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,10 @@ async def add_order_to_delievery(
456456

457457
# If the balance does not exist, we create a new one with a balance of 0
458458
if not balance:
459-
new_cash_db = schemas_amap.CashBase(
459+
balance = models_amap.Cash(
460460
balance=0,
461461
user_id=order.user_id,
462-
)
463-
balance = models_amap.Cash(
464-
**new_cash_db.model_dump(),
462+
last_ordering_date=ordering_date,
465463
)
466464
await cruds_amap.create_cash_of_user(
467465
cash=balance,
@@ -492,6 +490,12 @@ async def add_order_to_delievery(
492490
amount=amount,
493491
)
494492

493+
await cruds_amap.update_last_ordering_date(
494+
db=db,
495+
user_id=order.user_id,
496+
date=ordering_date,
497+
)
498+
495499
orderret = await cruds_amap.get_order_by_id(order_id=db_order.order_id, db=db)
496500
productsret = await cruds_amap.get_products_of_order(db=db, order_id=order_id)
497501

@@ -619,6 +623,12 @@ async def edit_order_from_delivery(
619623
user_id=previous_order.user_id,
620624
amount=previous_amount,
621625
)
626+
date = datetime.now(UTC)
627+
await cruds_amap.update_last_ordering_date(
628+
db=db,
629+
user_id=previous_order.user_id,
630+
date=date,
631+
)
622632
hyperion_amap_logger.info(
623633
f"Edit_order: Order {order_id} has been edited for user {db_order.user_id}. Amount was {previous_amount}€, is now {amount}€. ({request_id})",
624634
)
@@ -847,6 +857,7 @@ async def get_cash_by_id(
847857
balance=0,
848858
user_id=user_id,
849859
user=schemas_users.CoreUserSimple(**user_db.__dict__),
860+
last_ordering_date=datetime.now(UTC),
850861
)
851862

852863
return cash
@@ -882,7 +893,9 @@ async def create_cash_of_user(
882893
detail="This user already has a cash.",
883894
)
884895

885-
cash_db = models_amap.Cash(user_id=user_id, balance=cash.balance)
896+
cash_db = models_amap.Cash(
897+
user_id=user_id, balance=cash.balance, last_ordering_date=datetime.now(UTC)
898+
)
886899

887900
await cruds_amap.create_cash_of_user(
888901
cash=cash_db,
@@ -987,7 +1000,7 @@ async def get_orders_of_user(
9871000
delivery_date=order.delivery.delivery_date,
9881001
delivery_name=order.delivery.name,
9891002
**order.__dict__,
990-
)
1003+
),
9911004
)
9921005
return res
9931006

app/modules/amap/models_amap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class Cash(Base):
107107
primary_key=True,
108108
)
109109
balance: Mapped[float]
110+
last_ordering_date: Mapped[datetime]
110111
user: Mapped[CoreUser] = relationship("CoreUser", init=False)
111112

112113

app/modules/amap/schemas_amap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class CashBase(BaseModel):
124124

125125
class CashComplete(CashBase):
126126
user: CoreUserSimple
127+
last_ordering_date: datetime
127128

128129

129130
class CashEdit(BaseModel):
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""empty message
2+
3+
Create Date: 2025-09-25 13:10:51.977068
4+
"""
5+
6+
from collections.abc import Sequence
7+
from datetime import datetime
8+
from typing import TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from pytest_alembic import MigrationContext
12+
13+
import sqlalchemy as sa
14+
from alembic import op
15+
16+
# revision identifiers, used by Alembic.
17+
revision: str = "5351256b1bfc"
18+
down_revision: str | None = "b72df3765853"
19+
branch_labels: str | Sequence[str] | None = None
20+
depends_on: str | Sequence[str] | None = None
21+
22+
23+
def upgrade() -> None:
24+
# ### commands auto generated by Alembic - please adjust! ###
25+
op.add_column(
26+
"amap_cash", sa.Column("last_ordering_date", TZDateTime(), nullable=True)
27+
)
28+
default_time = datetime(2025, 1, 1)
29+
op.execute("UPDATE amap_cash SET last_ordering_date = (?)", (default_time,))
30+
op.alter_column("amap_cash", "last_ordering_date", nullable=False)
31+
32+
33+
def downgrade() -> None:
34+
# ### commands auto generated by Alembic - please adjust! ###
35+
op.drop_column("amap_cash", "last_ordering_date")
36+
37+
38+
def pre_test_upgrade(
39+
alembic_runner: "MigrationContext",
40+
alembic_connection: sa.Connection,
41+
) -> None:
42+
pass
43+
44+
45+
def test_upgrade(
46+
alembic_runner: "MigrationContext",
47+
alembic_connection: sa.Connection,
48+
) -> None:
49+
pass

0 commit comments

Comments
 (0)