From f3371816e83ad977ba958dcfa9f657a3480b4fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isak=20Ohlsson=20=C3=85ngnell?= <40887124+islean@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:33:25 +0200 Subject: [PATCH] Rename order status column (#3570) (minor) ### Changed - The is_delivered column in Order is renamed to is_open with values flipped. --- ..._08_09_7770dcad8bde_rename_order_status.py | 50 +++++++++++++++++++ cg/server/admin.py | 2 +- .../orders/order_delivery_update_request.py | 2 +- cg/server/dto/orders/order_patch_request.py | 4 +- cg/server/dto/orders/orders_request.py | 2 +- cg/server/dto/orders/orders_response.py | 2 +- cg/server/endpoints/orders.py | 27 +++++----- .../orders/order_service/order_service.py | 18 +++---- cg/services/orders/order_service/utils.py | 4 +- cg/store/crud/read.py | 4 +- cg/store/crud/update.py | 6 +-- cg/store/filters/status_order_filters.py | 10 ++-- cg/store/models.py | 2 +- 13 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py diff --git a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py new file mode 100644 index 0000000000..cb6af54df8 --- /dev/null +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -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" +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" + ) diff --git a/cg/server/admin.py b/cg/server/admin.py index a77d1bdc82..12fbc0446c 100644 --- a/cg/server/admin.py +++ b/cg/server/admin.py @@ -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 diff --git a/cg/server/dto/orders/order_delivery_update_request.py b/cg/server/dto/orders/order_delivery_update_request.py index e2b51b92c1..6a83062b6e 100644 --- a/cg/server/dto/orders/order_delivery_update_request.py +++ b/cg/server/dto/orders/order_delivery_update_request.py @@ -1,5 +1,5 @@ from pydantic import BaseModel, Field -class OrderDeliveredUpdateRequest(BaseModel): +class OrderOpenUpdateRequest(BaseModel): delivered_analyses_count: int = Field(..., alias="deliveredAnalysesCount") diff --git a/cg/server/dto/orders/order_patch_request.py b/cg/server/dto/orders/order_patch_request.py index 2332242e12..94b09a8f34 100644 --- a/cg/server/dto/orders/order_patch_request.py +++ b/cg/server/dto/orders/order_patch_request.py @@ -1,5 +1,5 @@ from pydantic import BaseModel -class OrderDeliveredPatch(BaseModel): - delivered: bool +class OrderOpenPatch(BaseModel): + open: bool diff --git a/cg/server/dto/orders/orders_request.py b/cg/server/dto/orders/orders_request.py index 3d88b4f436..01d75e929b 100644 --- a/cg/server/dto/orders/orders_request.py +++ b/cg/server/dto/orders/orders_request.py @@ -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 diff --git a/cg/server/dto/orders/orders_response.py b/cg/server/dto/orders/orders_response.py index bdb6ede332..7c95e3123e 100644 --- a/cg/server/dto/orders/orders_response.py +++ b/cg/server/dto/orders/orders_response.py @@ -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 diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index d2bd64bb15..ac64a94178 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -25,16 +25,13 @@ ) from cg.io.controller import WriteStream from cg.meta.orders import OrdersAPI -from cg.meta.orders.ticket_handler import TicketHandler 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_delivery_update_request import OrderOpenUpdateRequest +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 @@ -73,24 +70,24 @@ def get_order(order_id: int): return make_response(jsonify(error=str(error)), HTTPStatus.NOT_FOUND) -@ORDERS_BLUEPRINT.route("/orders//delivered", methods=["PATCH"]) -def set_order_delivered(order_id: int): +@ORDERS_BLUEPRINT.route("/orders//open", methods=["PATCH"]) +def set_order_open(order_id: int): 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.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//update-delivery-status", methods=["POST"]) -def update_order_delivered(order_id: int): - """Update the delivery status of an order based on the number of delivered analyses.""" +@ORDERS_BLUEPRINT.route("/orders//update-open-status", methods=["POST"]) +def update_order_open(order_id: int): + """Update the is_open parameter of an order based on the number of delivered analyses.""" try: - request_data = OrderDeliveredUpdateRequest.model_validate(request.json) + request_data = OrderOpenUpdateRequest.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 diff --git a/cg/services/orders/order_service/order_service.py b/cg/services/orders/order_service/order_service.py index ac2d83b187..6bbc2c3023 100644 --- a/cg/services/orders/order_service/order_service.py +++ b/cg/services/orders/order_service/order_service.py @@ -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 ( @@ -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_status(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 is_open parameter of an order based on the number of delivered analyses.""" 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) diff --git a/cg/services/orders/order_service/utils.py b/cg/services/orders/order_service/utils.py index 61877a517e..12be905774 100644 --- a/cg/services/orders/order_service/utils.py +++ b/cg/services/orders/order_service/utils.py @@ -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, ) @@ -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 diff --git a/cg/store/crud/read.py b/cg/store/crud/read.py index b0d3b318ea..04cb7e9c27 100644 --- a/cg/store/crud/read.py +++ b/cg/store/crud/read.py @@ -1370,10 +1370,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( diff --git a/cg/store/crud/update.py b/cg/store/crud/update.py index d6c62e0fdc..03cf3c407f 100644 --- a/cg/store/crud/update.py +++ b/cg/store/crud/update.py @@ -31,10 +31,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: - """Update the delivery status of an order.""" + def update_order_status(self, order_id: int, open: bool) -> Order: + """Update the 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 diff --git a/cg/store/filters/status_order_filters.py b/cg/store/filters/status_order_filters.py index a9c500425e..9f08d1a9ea 100644 --- a/cg/store/filters/status_order_filters.py +++ b/cg/store/filters/status_order_filters.py @@ -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( @@ -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 @@ -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( @@ -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 diff --git a/cg/store/models.py b/cg/store/models.py index d4699c4986..4f1e780cdc 100644 --- a/cg/store/models.py +++ b/cg/store/models.py @@ -974,7 +974,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=True) def to_dict(self): return to_dict(model_instance=self)