21
21
from api .exceptions import ResourceNotFoundError , UnprocessableEntityError
22
22
from api .models import (PRIMARY_CATEGORIES , CalendarEvent , Event ,
23
23
EventCategoryEnum , EventConfiguration , EventTypeEnum ,
24
- WorkCalendarEvent , WorkPhase , db )
24
+ WorkCalendarEvent , WorkPhase , Work , WorkStateEnum , db )
25
25
from api .models .action import Action , ActionEnum
26
26
from api .models .action_configuration import ActionConfiguration
27
27
from api .models .event_template import EventPositionEnum
@@ -115,13 +115,23 @@ def _process_events(
115
115
current_event_old_index : int = None ,
116
116
) -> None :
117
117
"""Process the event date logic"""
118
+ cls ._end_event_anticipated_change_rule (event , event_old )
119
+ all_work_events = cls ._find_events (
120
+ current_work_phase .work_id , None , PRIMARY_CATEGORIES
121
+ )
122
+ all_work_events = sorted (
123
+ all_work_events , key = lambda x : x .actual_date or x .anticipated_date
124
+ )
125
+ cls ._previous_event_acutal_date_rule (all_work_events , event , current_event_old_index )
118
126
all_work_event_configurations = EventConfigurationService .find_configurations (
119
127
event .work_id , _all = True
120
128
)
121
129
cls ._handle_child_events (all_work_event_configurations , event )
122
130
number_of_days_to_be_pushed = cls ._get_number_of_days_to_be_pushed (
123
131
event , event_old , current_work_phase
124
132
)
133
+ if event .actual_date and event .event_configuration .event_position == EventPositionEnum .END :
134
+ cls ._complete_work_phase (current_work_phase )
125
135
if (
126
136
event .event_configuration .event_type_id == EventTypeEnum .TIME_LIMIT_SUSPENSION .value
127
137
and event .actual_date
@@ -139,15 +149,7 @@ def _process_events(
139
149
current_work_phase .update (current_work_phase .as_dict (recursive = False ), commit = False )
140
150
141
151
if number_of_days_to_be_pushed != 0 :
142
- cls ._end_event_anticipated_change_rule (event , event_old )
143
152
144
- all_work_events = cls ._find_events (
145
- current_work_phase .work_id , None , PRIMARY_CATEGORIES
146
- )
147
- all_work_events = sorted (
148
- all_work_events , key = lambda x : x .actual_date or x .anticipated_date
149
- )
150
- cls ._previous_event_acutal_date_rule (all_work_events , event )
151
153
all_work_phases = WorkPhase .find_by_params (
152
154
{"work_id" : current_work_phase .work_id }
153
155
)
@@ -222,6 +224,25 @@ def _process_events(
222
224
current_event_old_index ,
223
225
)
224
226
227
+ @classmethod
228
+ def _complete_work_phase (cls , current_work_phase : WorkPhase ) -> None :
229
+ """Mark the current work phase complete and set the next work phase as the current one in the work"""
230
+ all_work_phases = WorkPhase .find_by_params (
231
+ {"work_id" : current_work_phase .work_id }
232
+ )
233
+ current_work_phase .is_completed = True
234
+ current_work_phase .update (current_work_phase .as_dict (recursive = False ), commit = False )
235
+
236
+ current_work_phase_index = util .find_index_in_array (
237
+ all_work_phases , current_work_phase
238
+ )
239
+ work : Work = Work .find_by_id (current_work_phase .work_id )
240
+ if current_work_phase_index == len (all_work_phases ) - 1 :
241
+ work .work_state = WorkStateEnum .COMPLETED
242
+ else :
243
+ work .current_phase_id = all_work_phases [ current_work_phase_index + 1 ].id
244
+ work .update (work .as_dict (recursive = False ), commit = False )
245
+
225
246
@classmethod
226
247
def _get_number_of_days_to_be_pushed (cls ,
227
248
event : Event ,
@@ -357,7 +378,7 @@ def _end_event_anticipated_change_rule(cls, event: Event, event_old: Event) -> N
357
378
358
379
@classmethod
359
380
def _previous_event_acutal_date_rule (
360
- cls , all_work_events : [Event ], event : Event
381
+ cls , all_work_events : [Event ], event : Event , current_event_old_index : int
361
382
) -> None :
362
383
"""Check to see if the previous event has actual date present
363
384
0 commit comments