Skip to content

Commit

Permalink
workplan card data issues (#2300)
Browse files Browse the repository at this point in the history
* workplan card data issues

* Make work inactive when the end states has been choosen
  • Loading branch information
dinesh-aot authored Jun 3, 2024
1 parent 9d6395e commit a10e50b
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 125 deletions.
14 changes: 10 additions & 4 deletions epictrack-api/src/api/actions/set_work_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from api.actions.base import ActionFactory
from api.models import db
from api.models.work import Work, WorkStateEnum
from api.models.work import Work, WorkStateEnum, EndingWorkStateEnum
from .change_phase_end_event import ChangePhaseEndEvent


Expand All @@ -12,15 +12,21 @@ class SetWorkState(ActionFactory):
def run(self, source_event, params) -> None:
"""Sets the work as per action configuration"""
work_state = params.get("work_state")
if work_state in [WorkStateEnum.TERMINATED.value, WorkStateEnum.WITHDRAWN.value]:
if work_state in [
WorkStateEnum.TERMINATED.value,
WorkStateEnum.WITHDRAWN.value,
]:
change_phase_end_event = ChangePhaseEndEvent()
change_phase_end_event_param = {
"phase_name": source_event.event_configuration.work_phase.name,
"work_type_id": source_event.work.work_type_id,
"ea_act_id": source_event.work.ea_act_id,
"event_name": source_event.event_configuration.name
"event_name": source_event.event_configuration.name,
}
change_phase_end_event.run(source_event, change_phase_end_event_param)
is_active = True
if work_state in [state.value for state in EndingWorkStateEnum]:
is_active = False
db.session.query(Work).filter(Work.id == source_event.work_id).update(
{Work.work_state: work_state}
{Work.work_state: work_state, Work.is_active: is_active}
)
2 changes: 1 addition & 1 deletion epictrack-api/src/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from .task_event_responsibility import TaskEventResponsibility
from .task_template import TaskTemplate
from .types import Type
from .work import Work, WorkStateEnum
from .work import Work, WorkStateEnum, EndingWorkStateEnum
from .work_calendar_event import WorkCalendarEvent
from .work_issue_updates import WorkIssueUpdates
from .work_issues import WorkIssues
Expand Down
63 changes: 57 additions & 6 deletions epictrack-api/src/api/models/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@
import enum
from typing import List, Tuple

from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, String, Text, and_, exists, func, or_
from sqlalchemy import (
Boolean,
Column,
DateTime,
Enum,
ForeignKey,
Integer,
String,
Text,
and_,
exists,
func,
or_
)
from sqlalchemy.orm import relationship
from sqlalchemy.ext.hybrid import hybrid_property

Expand All @@ -42,6 +55,15 @@ class WorkStateEnum(enum.Enum):
COMPLETED = "COMPLETED"


class EndingWorkStateEnum(enum.Enum):
"""Ending states for work"""

WITHDRAWN = "WITHDRAWN"
TERMINATED = "TERMINATED"
CLOSED = "CLOSED"
COMPLETED = "COMPLETED"


class Work(BaseModelVersioned):
"""Model class for Work."""

Expand Down Expand Up @@ -143,10 +165,7 @@ def fetch_all_works(
search_filters: WorkplanDashboardSearchOptions = None
) -> Tuple[List[Work], int]:
"""Fetch all active works."""
query = cls.query.filter_by(is_active=True, is_deleted=False)

query = cls.filter_by_search_criteria(query, search_filters)

query = cls.filter_by_search_criteria(cls.query, search_filters)
query = query.order_by(Work.start_date.desc())

no_pagination_options = not pagination_options or not pagination_options.page or not pagination_options.size
Expand Down Expand Up @@ -233,5 +252,37 @@ def _filter_by_env_regions(cls, query, env_regions):
@classmethod
def _filter_by_work_states(cls, query, work_states):
if work_states:
query = query.filter(Work.work_state.in_(work_states))
ending_states = [state.value for state in EndingWorkStateEnum]
filtered_ending_states = [work_state for work_state in work_states if work_state in ending_states]
filtered_non_ending_states = [
work_state
for work_state in work_states
if work_state not in ending_states
]
ending_state_query = None
if filtered_ending_states:
ending_state_query = query.filter(
and_(
Work.work_state.in_(filtered_ending_states),
Work.is_active.is_(False),
Work.is_deleted.is_(False),
)
)
if filtered_non_ending_states:
query = query.filter(
and_(
Work.work_state.in_(filtered_non_ending_states),
Work.is_active.is_(True),
Work.is_deleted.is_(False),
)
)
if ending_state_query:
if (
not filtered_non_ending_states
): # if non ending state query is not pressent
query = ending_state_query
else: # if both state queries are present
query = query.union(ending_state_query)
else:
query = query.filter_by(is_active=True, is_deleted=False)
return query
6 changes: 6 additions & 0 deletions epictrack-api/src/api/schemas/response/work_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ class WorkPhaseAdditionalInfoResponseSchema(Schema):
)
current_milestone = fields.Str(metadata={"description": "Current milestone in the phase"})
next_milestone = fields.Str(metadata={"description": "Next milestone in the phase"})
next_milestone_date = fields.DateTime(metadata={"description": "Anticipated date of the next milestone"})
milestone_progress = fields.Number(metadata={"description": "Milestone progress"})
decision_milestone = fields.Str(metadata={"description": "Last completed decision milestone in the phase"})
decision = fields.Str(metadata={"description": "Decision taken in the decision milestone"})
decision_milestone_date = fields.DateTime(
metadata={"description": "Actual date of last completed decision milestone in the phase"}
)
is_last_phase = fields.Number(metadata={"description": "Indicate if this the last phase of the work"})
days_left = fields.Number(
metadata={"description": "Number of days left in the phase"}
Expand Down
41 changes: 41 additions & 0 deletions epictrack-api/src/api/services/common_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Service to manage ActSection."""
from datetime import datetime


def find_event_date(event) -> datetime:
"""Returns the event actual or anticipated date"""
return event.actual_date if event.actual_date else event.anticipated_date


def event_compare_func(event_x, event_y):
"""Compare function for event sort"""
# if (
# event_x.event_position == EventPositionEnum.START.value
# or event_y.event_position == EventPositionEnum.END.value
# ):
# return -1
# if (
# event_y.event_position == EventPositionEnum.START.value
# or event_x.event_position == EventPositionEnum.END.value
# ):
# return 1
if (find_event_date(event_x).date() - find_event_date(event_y).date()).days == 0:
return -1 if event_x.id < event_y.id else 1
if (find_event_date(event_x).date() - find_event_date(event_y).date()).days < 0:
return -1
if (find_event_date(event_x).date() - find_event_date(event_y).date()).days > 0:
return 1
return 0
Loading

0 comments on commit a10e50b

Please sign in to comment.