Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename order status column #3570

Merged
merged 25 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Rename order status
Revision ID: 7770dcad8bde
Revises: bb4c6dbad991
Create Date: 2024-08-09 09:47:47.814700
"""

import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

from alembic import op

# revision identifiers, used by Alembic.
revision = "7770dcad8bde"
down_revision = "bb4c6dbad991"
islean marked this conversation as resolved.
Show resolved Hide resolved
branch_labels = None
depends_on = None


class Base(DeclarativeBase):
pass


class Order(Base):
__tablename__ = "order"

id: Mapped[int] = mapped_column(primary_key=True)
is_open: Mapped[str]


def upgrade():
op.alter_column(
table_name="order", column_name="is_delivered", type_=sa.Boolean, new_column_name="is_open"
)
bind = op.get_bind()
session = orm.Session(bind=bind)
for order in session.query(Order).all():
order.is_open = not order.is_open
session.commit()


def downgrade():
bind = op.get_bind()
session = orm.Session(bind=bind)
for order in session.query(Order).all():
order.is_open = not order.is_open
session.commit()
op.alter_column(
table_name="order", new_column_name="is_delivered", type_=sa.Boolean, column_name="is_open"
)
2 changes: 1 addition & 1 deletion cg/server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class OrderView(BaseView):
"""Admin view for Model.Order"""

column_default_sort = ("order_date", True)
column_editable_list = ["is_delivered"]
column_editable_list = ["is_open"]
column_searchable_list = ["id", "ticket_id"]
column_display_pk = True
create_modal = True
Expand Down
2 changes: 1 addition & 1 deletion cg/server/dto/orders/order_delivery_update_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel, Field


class OrderDeliveredUpdateRequest(BaseModel):
class OrderOpenUpdateRequest(BaseModel):
delivered_analyses_count: int = Field(..., alias="deliveredAnalysesCount")
4 changes: 2 additions & 2 deletions cg/server/dto/orders/order_patch_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel


class OrderDeliveredPatch(BaseModel):
delivered: bool
class OrderOpenPatch(BaseModel):
is_open: bool
2 changes: 1 addition & 1 deletion cg/server/dto/orders/orders_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class OrdersRequest(BaseModel):
sort_order: SortOrder | None = Field(alias="sortOrder", default=SortOrder.DESC)
search: str | None = None
workflow: str | None = None
delivered: bool | None = None
is_open: bool | None = None
2 changes: 1 addition & 1 deletion cg/server/dto/orders/orders_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Order(BaseModel):
ticket_id: int
order_date: str
id: int
is_delivered: bool
is_open: bool
workflow: Workflow
summary: OrderSummary | None = None

Expand Down
17 changes: 8 additions & 9 deletions cg/server/endpoints/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
from cg.models.orders.order import OrderIn, OrderType
from cg.models.orders.orderform_schema import Orderform
from cg.server.dto.delivery_message.delivery_message_response import DeliveryMessageResponse
from cg.server.dto.orders.order_delivery_update_request import OrderDeliveredUpdateRequest
from cg.server.dto.orders.order_patch_request import OrderDeliveredPatch
from cg.server.dto.orders.order_patch_request import OrderOpenPatch
from cg.server.dto.orders.orders_request import OrdersRequest
from cg.server.dto.orders.orders_response import Order, OrdersResponse
from cg.server.endpoints.utils import before_request
Expand Down Expand Up @@ -73,24 +72,24 @@ def get_order(order_id: int):
return make_response(jsonify(error=str(error)), HTTPStatus.NOT_FOUND)


@ORDERS_BLUEPRINT.route("/orders/<order_id>/delivered", methods=["PATCH"])
@ORDERS_BLUEPRINT.route("/orders/<order_id>/open", methods=["PATCH"])
def set_order_delivered(order_id: int):
islean marked this conversation as resolved.
Show resolved Hide resolved
try:
request_data = OrderDeliveredPatch.model_validate(request.json)
delivered: bool = request_data.delivered
response_data: Order = order_service.set_delivery(order_id=order_id, delivered=delivered)
request_data = OrderOpenPatch.model_validate(request.json)
is_open: bool = request_data.is_open
response_data: Order = order_service.set_open(order_id=order_id, open=is_open)
return jsonify(response_data.model_dump()), HTTPStatus.OK
except OrderNotFoundError as error:
return jsonify(error=str(error)), HTTPStatus.NOT_FOUND


@ORDERS_BLUEPRINT.route("/orders/<order_id>/update-delivery-status", methods=["POST"])
@ORDERS_BLUEPRINT.route("/orders/<order_id>/update-open-status", methods=["POST"])
def update_order_delivered(order_id: int):
islean marked this conversation as resolved.
Show resolved Hide resolved
"""Update the delivery status of an order based on the number of delivered analyses."""
"""Update the openness status of an order based on the number of delivered analyses."""
islean marked this conversation as resolved.
Show resolved Hide resolved
try:
request_data = OrderDeliveredUpdateRequest.model_validate(request.json)
delivered_analyses: int = request_data.delivered_analyses_count
order_service.update_delivered(order_id=order_id, delivered_analyses=delivered_analyses)
order_service.update_is_open(order_id=order_id, delivered_analyses=delivered_analyses)
except OrderNotFoundError as error:
return jsonify(error=str(error)), HTTPStatus.NOT_FOUND

Expand Down
18 changes: 9 additions & 9 deletions cg/services/orders/order_service/order_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cg.services.orders.order_service.utils import (
create_order_response,
create_orders_response,
order_is_delivered,
order_is_closed,
)
from cg.services.orders.order_summary_service.dto.order_summary import OrderSummary
from cg.services.orders.order_summary_service.order_summary_service import (
Expand Down Expand Up @@ -41,15 +41,15 @@ def create_order(self, order_data: OrderIn) -> OrderResponse:
self.store.link_case_to_order(order_id=order.id, case_id=case.id)
return create_order_response(order)

def set_delivery(self, order_id: int, delivered: bool) -> OrderResponse:
order: Order = self.store.update_order_delivery(order_id=order_id, delivered=delivered)
def set_open(self, order_id: int, open: bool) -> OrderResponse:
order: Order = self.store.update_order_delivery(order_id=order_id, open=open)
return create_order_response(order)

def update_delivered(self, order_id: int, delivered_analyses: int) -> None:
"""Update the delivery status of an order based on the number of delivered analyses."""
def update_is_open(self, order_id: int, delivered_analyses: int) -> None:
"""Update the openness status of an order based on the number of delivered analyses."""
islean marked this conversation as resolved.
Show resolved Hide resolved
order: Order = self.store.get_order_by_id(order_id)
case_count: int = len(order.cases)
if order_is_delivered(case_count=case_count, delivered_analyses=delivered_analyses):
self.set_delivery(order_id=order_id, delivered=True)
elif order.is_delivered:
self.set_delivery(order_id=order_id, delivered=False)
if order_is_closed(case_count=case_count, delivered_analyses=delivered_analyses):
self.set_open(order_id=order_id, open=False)
elif not order.is_open:
self.set_open(order_id=order_id, open=True)
4 changes: 2 additions & 2 deletions cg/services/orders/order_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create_order_response(order: DatabaseOrder, summary: OrderSummary | None = N
ticket_id=order.ticket_id,
order_date=str(order.order_date.date()),
id=order.id,
is_delivered=order.is_delivered,
is_open=order.is_open,
workflow=order.workflow,
summary=summary,
)
Expand All @@ -30,5 +30,5 @@ def _add_summaries(orders: list[Order], summaries: list[OrderSummary]) -> list[O
return orders


def order_is_delivered(case_count: int, delivered_analyses: int) -> bool:
def order_is_closed(case_count: int, delivered_analyses: int) -> bool:
return delivered_analyses >= case_count
4 changes: 2 additions & 2 deletions cg/store/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,10 +1362,10 @@ def get_orders(self, orders_request: OrdersRequest) -> tuple[list[Order], int]:
"""Filter, sort and paginate orders based on the provided request."""
orders: Query = apply_order_filters(
orders=self._get_query(Order),
filters=[OrderFilter.BY_WORKFLOW, OrderFilter.BY_SEARCH, OrderFilter.BY_DELIVERED],
filters=[OrderFilter.BY_WORKFLOW, OrderFilter.BY_SEARCH, OrderFilter.BY_OPEN],
workflow=orders_request.workflow,
search=orders_request.search,
delivered=orders_request.delivered,
is_open=orders_request.is_open,
)
total_count: int = orders.count()
orders: list[Order] = self.sort_and_paginate_orders(
Expand Down
4 changes: 2 additions & 2 deletions cg/store/crud/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def update_sample_comment(self, sample_id: int, comment: str) -> None:
sample.comment = f"{sample.comment} {comment}" if sample.comment else comment
self.session.commit()

def update_order_delivery(self, order_id: int, delivered: bool) -> Order:
def update_order_delivery(self, order_id: int, open: bool) -> Order:
"""Update the delivery status of an order."""
order: Order = self.get_order_by_id(order_id)
order.is_delivered = delivered
order.is_open = open
self.session.commit()
return order

Expand Down
10 changes: 5 additions & 5 deletions cg/store/filters/status_order_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def filter_orders_by_search(orders: Query, search: str | None, **kwargs) -> Quer
)


def filter_orders_by_delivered(orders: Query, delivered: bool | None, **kwargs) -> Query:
return orders.filter(Order.is_delivered == delivered) if delivered is not None else orders
def filter_orders_by_is_open(orders: Query, is_open: bool | None, **kwargs) -> Query:
return orders.filter(Order.is_open == is_open) if is_open is not None else orders


def apply_sorting(
Expand All @@ -66,7 +66,7 @@ class OrderFilter(Enum):
BY_SEARCH: Callable = filter_orders_by_search
BY_TICKET_ID: Callable = filter_orders_by_ticket_id
BY_WORKFLOW: Callable = filter_orders_by_workflow
BY_DELIVERED: Callable = filter_orders_by_delivered
BY_OPEN: Callable = filter_orders_by_is_open
PAGINATE: Callable = apply_pagination
SORT: Callable = apply_sorting

Expand All @@ -83,7 +83,7 @@ def apply_order_filters(
sort_field: OrderSortField = None,
sort_order: SortOrder = None,
search: str = None,
delivered: bool = None,
is_open: bool = None,
) -> Query:
for filter in filters:
orders: Query = filter(
Expand All @@ -97,6 +97,6 @@ def apply_order_filters(
sort_field=sort_field,
sort_order=sort_order,
search=search,
delivered=delivered,
is_open=is_open,
)
return orders
2 changes: 1 addition & 1 deletion cg/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ class Order(Base):
order_date: Mapped[datetime] = mapped_column(default=datetime.now)
ticket_id: Mapped[int] = mapped_column(unique=True, index=True)
workflow: Mapped[str] = mapped_column(types.Enum(*(workflow.value for workflow in Workflow)))
is_delivered: Mapped[bool] = mapped_column(default=False)
is_open: Mapped[bool] = mapped_column(default=False)

def to_dict(self):
return to_dict(model_instance=self)
Expand Down
Loading