diff --git a/apiserver/plane/graphql/types/state.py b/apiserver/plane/graphql/types/state.py index a2cdf8172d..fa9e91c5e7 100644 --- a/apiserver/plane/graphql/types/state.py +++ b/apiserver/plane/graphql/types/state.py @@ -1,11 +1,26 @@ +# Third-party library imports +from typing import Optional + # Strawberry Imports import strawberry import strawberry_django +# Django Imports +from asgiref.sync import sync_to_async + # Module imports from plane.db.models import State +@sync_to_async +def get_group_states(project_id: str, state_group: str) -> list[State]: + states = State.objects.filter( + project_id=project_id, group=state_group, deleted_at__isnull=True + ).order_by("sequence") + + return list(states) + + @strawberry_django.type(State) class StateType: id: strawberry.ID @@ -27,3 +42,20 @@ class StateType: @strawberry.field def project(self) -> int: return self.project_id + + @strawberry.field + async def order(self) -> Optional[float]: + if self.group in ["started", "unstarted"]: + project_id = self.project_id + state_group = self.group + + group_states = await get_group_states( + project_id=project_id, state_group=state_group + ) + + current_state_index = group_states.index(self) + 1 + group_states_count = len(group_states) + + return current_state_index / group_states_count + + return None