Skip to content

Commit

Permalink
feat: add cancel order endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasoli committed Oct 16, 2024
1 parent 62ee9ef commit 8b08623
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class OrderInDB(BaseModel):
model_config = ConfigDict(from_attributes=True)


class CancelOrder(BaseModel):
cancel_reason: str


class OrderItemResponse(BaseModel):
order_items: list[OrderItemInDB]
page: int
Expand Down
12 changes: 12 additions & 0 deletions app/infra/endpoints/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from app.infra.deps import get_db
from app.infra.models import PaymentDB, OrderDB
from app.entities.order import (
CancelOrder,
OrderSchema,
TrackingFullResponse,
OrderUserListResponse,
Expand Down Expand Up @@ -115,6 +116,17 @@ async def complete_order(
raise


@order.delete('/{order_id}', status_code=status.HTTP_204_NO_CONTENT)
async def delete_order(
*,
db: Session = Depends(get_db),
cancel_reason: CancelOrder,
order_id: int,
) -> None:
"""Delete order."""
services.delete_order(order_id, cancel=cancel_reason, db=db)


@order.patch(
'/{order_id}/tracking_number',
status_code=status.HTTP_204_NO_CONTENT,
Expand Down
24 changes: 22 additions & 2 deletions app/order/services.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import UTC, datetime
import json
import math
from typing import Any

from app.infra.constants import OrderStatus
from app.infra.constants import OrderStatus, PaymentStatus
from fastapi import HTTPException, status
from loguru import logger
from pydantic import TypeAdapter
Expand All @@ -11,7 +12,7 @@
from app.product import repository
from faststream.rabbit import RabbitQueue

from app.entities.order import OrderInDB, OrderResponse
from app.entities.order import CancelOrder, OrderInDB, OrderNotFoundError, OrderResponse
from app.entities.product import (
ProductCategoryInDB,
ProductCreate,
Expand Down Expand Up @@ -318,6 +319,25 @@ def get_order(db: Session, order_id: int) -> OrderInDB:
return OrderInDB.model_validate(order)


def delete_order(order_id: int, *, cancel: CancelOrder, db) -> None:
"""Soft delete order."""
with db:
order_query = (
select(OrderDB)
.options(selectinload(OrderDB.items))
.where(OrderDB.order_id == order_id)
)
order = db.scalar(order_query)
if not order:
raise OrderNotFoundError
order.order_status = PaymentStatus.CANCELLED
order.checked = False
order.cancelled_at = datetime.now(tz=UTC)
order.cancelled_reason=cancel.cancel_reason
db.add(order)
db.commit()


def get_order_users(db: Session, id):
...

Expand Down

0 comments on commit 8b08623

Please sign in to comment.