From f9469a0efc4928910b0efc6561ccfc0dc859faa6 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:15:05 +0200 Subject: [PATCH 01/16] Initial commit --- ..._08_09_7770dcad8bde_rename_order_status.py | 23 +++++++++++++++++++ cg/server/dto/orders/orders_request.py | 2 +- cg/server/dto/orders/orders_response.py | 2 +- cg/server/endpoints/orders.py | 2 +- .../orders/order_service/order_service.py | 18 +++++++-------- cg/services/orders/order_service/utils.py | 4 ++-- cg/store/crud/read.py | 2 +- cg/store/crud/update.py | 4 ++-- cg/store/filters/status_order_filters.py | 10 ++++---- cg/store/models.py | 2 +- 10 files changed, 46 insertions(+), 23 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..9012434368 --- /dev/null +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -0,0 +1,23 @@ +"""Rename order status +Revision ID: 7770dcad8bde +Revises: 817cf7fea40d +Create Date: 2024-08-09 09:47:47.814700 +""" + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "7770dcad8bde" +down_revision = "817cf7fea40d" +branch_labels = None +depends_on = None + + +def upgrade(): + pass + + +def downgrade(): + pass \ No newline at end of file diff --git a/cg/server/dto/orders/orders_request.py b/cg/server/dto/orders/orders_request.py index 3d88b4f436..8847160c97 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 + 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 20f1f1b822..6c8450dee7 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -77,7 +77,7 @@ def set_order_delivered(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) + response_data: Order = order_service.set_open(order_id=order_id, delivered=delivered) return jsonify(response_data.model_dump()), HTTPStatus.OK 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..85605ee239 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_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_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.""" 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 3c55837e0f..7633672548 100644 --- a/cg/store/crud/read.py +++ b/cg/store/crud/read.py @@ -1360,7 +1360,7 @@ def get_orders(self, orders_request: OrdersRequest) -> tuple[list[Order], int]: filters=[OrderFilter.BY_WORKFLOW, OrderFilter.BY_SEARCH, OrderFilter.BY_DELIVERED], workflow=orders_request.workflow, search=orders_request.search, - delivered=orders_request.delivered, + delivered=not orders_request.delivered, ) 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 5d07132ba2..75f29fe2c0 100644 --- a/cg/store/crud/update.py +++ b/cg/store/crud/update.py @@ -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 diff --git a/cg/store/filters/status_order_filters.py b/cg/store/filters/status_order_filters.py index a9c500425e..b2b6ea7a86 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, open: bool | None, **kwargs) -> Query: + return orders.filter(Order.is_open == open) if 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 be739d2fd3..ff31eddee9 100644 --- a/cg/store/models.py +++ b/cg/store/models.py @@ -970,7 +970,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) From 1aa222550052fa993f9791eb0c368aad4affa439 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:17:39 +0200 Subject: [PATCH 02/16] Changed is_delivered to is_open --- ..._08_09_7770dcad8bde_rename_order_status.py | 21 +++++++++++++++++-- cg/server/admin.py | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py index 9012434368..aaeef0f5da 100644 --- a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -7,6 +7,7 @@ import sqlalchemy as sa from alembic import op +from sqlalchemy import orm # revision identifiers, used by Alembic. revision = "7770dcad8bde" @@ -15,9 +16,25 @@ depends_on = None + + def upgrade(): - pass + 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 + + def downgrade(): - pass \ No newline at end of file + op.alter_column( + table_name="order", new_column_name="is_delivered", type_=sa.Boolean, column_name="is_open" + ) + bind = op.get_bind() + session = orm.Session(bind=bind) + for order in session.query("order").all(): + order.is_delivered = not order.is_delivered \ No newline at end of file diff --git a/cg/server/admin.py b/cg/server/admin.py index 4dc8b2b171..e68ba2e107 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 From f2a1d9ab7ae05c610cd69625de12e72184ffe3cb Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:19:47 +0200 Subject: [PATCH 03/16] Start change of endpoints --- cg/server/endpoints/orders.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index 6c8450dee7..3e04cdba57 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -72,7 +72,7 @@ def get_order(order_id: int): return make_response(jsonify(error=str(error)), HTTPStatus.NOT_FOUND) -@ORDERS_BLUEPRINT.route("/orders//delivered", methods=["PATCH"]) +@ORDERS_BLUEPRINT.route("/orders//open", methods=["PATCH"]) def set_order_delivered(order_id: int): try: request_data = OrderDeliveredPatch.model_validate(request.json) From 56d675ac6e7a5abf54cb20ad0616f6eb33cac35a Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:36:20 +0200 Subject: [PATCH 04/16] Start change of endpoints --- .../dto/orders/order_delivery_update_request.py | 2 +- cg/server/dto/orders/order_patch_request.py | 4 ++-- cg/server/endpoints/orders.py | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) 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..e5a3b58545 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): + is_open: bool diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index 3e04cdba57..2aac05ada3 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -30,7 +30,7 @@ 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 OrderDeliveredPatch, 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 @@ -75,21 +75,21 @@ def get_order(order_id: int): @ORDERS_BLUEPRINT.route("/orders//open", methods=["PATCH"]) def set_order_delivered(order_id: int): try: - request_data = OrderDeliveredPatch.model_validate(request.json) - delivered: bool = request_data.delivered - response_data: Order = order_service.set_open(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//update-delivery-status", methods=["POST"]) +@ORDERS_BLUEPRINT.route("/orders//update-open-status", methods=["POST"]) def update_order_delivered(order_id: int): - """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.""" 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 From b79e1b45d2271e918b52fcba3a8fc0428b2e10e9 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:37:41 +0200 Subject: [PATCH 05/16] Fix imports --- cg/server/endpoints/orders.py | 3 +-- cg/services/orders/order_service/order_service.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index 2aac05ada3..d285c23334 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -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, OrderOpenPatch +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 diff --git a/cg/services/orders/order_service/order_service.py b/cg/services/orders/order_service/order_service.py index 85605ee239..670f97a42f 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, + 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 ( From 51e8920cb9b61623d9fb2563f1ddc45d71d3cbe1 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:39:41 +0200 Subject: [PATCH 06/16] Fix --- cg/server/dto/orders/orders_request.py | 2 +- cg/store/crud/read.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cg/server/dto/orders/orders_request.py b/cg/server/dto/orders/orders_request.py index 8847160c97..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 - open: bool | None = None + is_open: bool | None = None diff --git a/cg/store/crud/read.py b/cg/store/crud/read.py index 7633672548..1aedf58823 100644 --- a/cg/store/crud/read.py +++ b/cg/store/crud/read.py @@ -1357,10 +1357,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=not orders_request.delivered, + is_open=orders_request.is_open, ) total_count: int = orders.count() orders: list[Order] = self.sort_and_paginate_orders( From bae4387944ac271f83d9d06ade966c20837adbee Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:40:53 +0200 Subject: [PATCH 07/16] black --- .../versions/2024_08_09_7770dcad8bde_rename_order_status.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py index aaeef0f5da..0b40ff382d 100644 --- a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -16,8 +16,6 @@ depends_on = None - - def upgrade(): op.alter_column( table_name="order", column_name="is_delivered", type_=sa.Boolean, new_column_name="is_open" @@ -28,8 +26,6 @@ def upgrade(): order.is_open = not order.is_open - - def downgrade(): op.alter_column( table_name="order", new_column_name="is_delivered", type_=sa.Boolean, column_name="is_open" @@ -37,4 +33,4 @@ def downgrade(): bind = op.get_bind() session = orm.Session(bind=bind) for order in session.query("order").all(): - order.is_delivered = not order.is_delivered \ No newline at end of file + order.is_delivered = not order.is_delivered From 6897fb8caa4253a8f32fb9c20b4186eda7d69178 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 15 Aug 2024 13:55:19 +0200 Subject: [PATCH 08/16] Fix tests --- cg/store/filters/status_order_filters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cg/store/filters/status_order_filters.py b/cg/store/filters/status_order_filters.py index b2b6ea7a86..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_is_open(orders: Query, open: bool | None, **kwargs) -> Query: - return orders.filter(Order.is_open == open) if open 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( From f07f84b82b2b46378ce412cb149b1ef9c4587954 Mon Sep 17 00:00:00 2001 From: islean Date: Mon, 2 Sep 2024 14:34:40 +0200 Subject: [PATCH 09/16] Correct revision --- .../versions/2024_08_09_7770dcad8bde_rename_order_status.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py index 0b40ff382d..79b718eab9 100644 --- a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -1,17 +1,17 @@ """Rename order status Revision ID: 7770dcad8bde -Revises: 817cf7fea40d +Revises: bb4c6dbad991 Create Date: 2024-08-09 09:47:47.814700 """ import sqlalchemy as sa +from sqlalchemy import orm from alembic import op -from sqlalchemy import orm # revision identifiers, used by Alembic. revision = "7770dcad8bde" -down_revision = "817cf7fea40d" +down_revision = "bb4c6dbad991" branch_labels = None depends_on = None From bfc916010a67c91053efe86f5f547842d210a028 Mon Sep 17 00:00:00 2001 From: islean Date: Mon, 2 Sep 2024 15:40:36 +0200 Subject: [PATCH 10/16] Fix revision --- ..._08_09_7770dcad8bde_rename_order_status.py | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py index 79b718eab9..cb6af54df8 100644 --- a/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py +++ b/alembic/versions/2024_08_09_7770dcad8bde_rename_order_status.py @@ -6,6 +6,7 @@ import sqlalchemy as sa from sqlalchemy import orm +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column from alembic import op @@ -16,21 +17,34 @@ 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(): + 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" ) - bind = op.get_bind() - session = orm.Session(bind=bind) - for order in session.query("order").all(): - order.is_delivered = not order.is_delivered From 28fa75c8238992d2e01769513809942ce1cbe0d5 Mon Sep 17 00:00:00 2001 From: islean Date: Wed, 4 Sep 2024 16:48:14 +0200 Subject: [PATCH 11/16] Rework endpoints --- cg/server/endpoints/orders.py | 19 +++++++++---------- .../orders/order_service/order_service.py | 2 +- cg/store/crud/update.py | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index 9dfb9b739b..e1473b68ec 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -28,7 +28,10 @@ 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.delivery_message.delivery_message_response import ( + DeliveryMessageResponse, +) +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 @@ -38,14 +41,10 @@ delivery_message_service, lims, order_service, - osticket, order_submitter_registry, + osticket, ) -from cg.store.models import ( - Application, - Customer, -) - +from cg.store.models import Application, Customer ORDERS_BLUEPRINT = Blueprint("orders", __name__, url_prefix="/api/v1") ORDERS_BLUEPRINT.before_request(before_request) @@ -73,7 +72,7 @@ def get_order(order_id: int): @ORDERS_BLUEPRINT.route("/orders//open", methods=["PATCH"]) -def set_order_delivered(order_id: int): +def set_order_open(order_id: int): try: request_data = OrderOpenPatch.model_validate(request.json) is_open: bool = request_data.is_open @@ -84,10 +83,10 @@ def set_order_delivered(order_id: int): @ORDERS_BLUEPRINT.route("/orders//update-open-status", methods=["POST"]) -def update_order_delivered(order_id: int): +def update_order_open(order_id: int): """Update the openness status 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_is_open(order_id=order_id, delivered_analyses=delivered_analyses) except OrderNotFoundError as error: diff --git a/cg/services/orders/order_service/order_service.py b/cg/services/orders/order_service/order_service.py index 670f97a42f..8ef8e59cb6 100644 --- a/cg/services/orders/order_service/order_service.py +++ b/cg/services/orders/order_service/order_service.py @@ -42,7 +42,7 @@ def create_order(self, order_data: OrderIn) -> OrderResponse: return create_order_response(order) def set_open(self, order_id: int, open: bool) -> OrderResponse: - order: Order = self.store.update_order_delivery(order_id=order_id, open=open) + order: Order = self.store.update_order_status(order_id=order_id, open=open) return create_order_response(order) def update_is_open(self, order_id: int, delivered_analyses: int) -> None: diff --git a/cg/store/crud/update.py b/cg/store/crud/update.py index 75f29fe2c0..7d934182cd 100644 --- a/cg/store/crud/update.py +++ b/cg/store/crud/update.py @@ -29,8 +29,8 @@ 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, open: 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_open = open self.session.commit() From 6bfc23629fc971e6598672f92c58577adcd8cfc6 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 5 Sep 2024 12:59:45 +0200 Subject: [PATCH 12/16] Fix tests --- cg/server/endpoints/orders.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index e95a1afb0a..1826de7743 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -25,7 +25,6 @@ ) 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 ( @@ -33,7 +32,6 @@ ) 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 @@ -43,7 +41,6 @@ lims, order_service, order_submitter_registry, - osticket, ticket_handler, ) from cg.store.models import Application, Customer From f8010de473f5a6da397dec268388eb325f19c42f Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 5 Sep 2024 14:03:25 +0200 Subject: [PATCH 13/16] Change parameter name --- cg/server/dto/orders/order_patch_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cg/server/dto/orders/order_patch_request.py b/cg/server/dto/orders/order_patch_request.py index e5a3b58545..94b09a8f34 100644 --- a/cg/server/dto/orders/order_patch_request.py +++ b/cg/server/dto/orders/order_patch_request.py @@ -2,4 +2,4 @@ class OrderOpenPatch(BaseModel): - is_open: bool + open: bool From 587cc0c477390198d014954f3ca518133c414864 Mon Sep 17 00:00:00 2001 From: islean Date: Thu, 5 Sep 2024 14:09:56 +0200 Subject: [PATCH 14/16] Change parameter name --- cg/server/endpoints/orders.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index 1826de7743..e692609220 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -74,7 +74,7 @@ def get_order(order_id: int): def set_order_open(order_id: int): try: request_data = OrderOpenPatch.model_validate(request.json) - is_open: bool = request_data.is_open + 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: From 60fbdc4beb349c60ae3e9dc40da97af2bd6fa884 Mon Sep 17 00:00:00 2001 From: islean Date: Fri, 6 Sep 2024 15:54:48 +0200 Subject: [PATCH 15/16] Address comments --- cg/server/endpoints/orders.py | 2 +- cg/services/orders/order_service/order_service.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cg/server/endpoints/orders.py b/cg/server/endpoints/orders.py index e692609220..ac64a94178 100644 --- a/cg/server/endpoints/orders.py +++ b/cg/server/endpoints/orders.py @@ -83,7 +83,7 @@ def set_order_open(order_id: int): @ORDERS_BLUEPRINT.route("/orders//update-open-status", methods=["POST"]) def update_order_open(order_id: int): - """Update the openness status of an order based on the number of delivered analyses.""" + """Update the is_open parameter of an order based on the number of delivered analyses.""" try: request_data = OrderOpenUpdateRequest.model_validate(request.json) delivered_analyses: int = request_data.delivered_analyses_count diff --git a/cg/services/orders/order_service/order_service.py b/cg/services/orders/order_service/order_service.py index 8ef8e59cb6..6bbc2c3023 100644 --- a/cg/services/orders/order_service/order_service.py +++ b/cg/services/orders/order_service/order_service.py @@ -46,7 +46,7 @@ def set_open(self, order_id: int, open: bool) -> OrderResponse: return create_order_response(order) 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.""" + """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_closed(case_count=case_count, delivered_analyses=delivered_analyses): From 7913bfc836bee87e953d8301ff8c76f5b804d4f7 Mon Sep 17 00:00:00 2001 From: islean Date: Mon, 9 Sep 2024 10:04:33 +0200 Subject: [PATCH 16/16] Default order is open --- cg/store/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cg/store/models.py b/cg/store/models.py index 3ed86d02f1..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_open: Mapped[bool] = mapped_column(default=False) + is_open: Mapped[bool] = mapped_column(default=True) def to_dict(self): return to_dict(model_instance=self)