@@ -1602,11 +1602,53 @@ def _make_requirement_detail_object(requirements: list):
16021602 return requirement_details
16031603
16041604
1605+ def _validate_and_delete_enforcement_action (
1606+ enforcement_action_config , requirement_id , inspection_id , session
1607+ ):
1608+ """
1609+ Validate and delete a specific enforcement action type.
1610+
1611+ @param enforcement_action_config: Configuration dict containing model, status_field,
1612+ allowed_status, map_field, update_method, and error_message
1613+ @param requirement_id: Requirement id
1614+ @param inspection_id: Inspection id
1615+ @param session: Database session
1616+ """
1617+ model = enforcement_action_config ["model" ]
1618+ status_field = enforcement_action_config ["status_field" ]
1619+ allowed_status = enforcement_action_config ["allowed_status" ]
1620+ map_field = enforcement_action_config ["map_field" ]
1621+ update_method = enforcement_action_config ["update_method" ]
1622+ error_message = enforcement_action_config ["error_message" ]
1623+
1624+ # Get all enforcement actions of this type for the inspection
1625+ enforcement_actions = model .get_by_inspection_id (inspection_id )
1626+
1627+ # Filter to only those mapped to this requirement
1628+ requirement_enforcement_actions = [
1629+ action
1630+ for action in enforcement_actions
1631+ if requirement_id
1632+ in [map_obj .inspection_requirement_id for map_obj in getattr (action , map_field )]
1633+ ]
1634+
1635+ # Check if any are not in the allowed status
1636+ if any (
1637+ getattr (action , status_field ) != allowed_status
1638+ for action in requirement_enforcement_actions
1639+ ):
1640+ raise UnprocessableEntityError (error_message )
1641+
1642+ # Delete all enforcement actions of this type for this requirement
1643+ for action in requirement_enforcement_actions :
1644+ update_method (action .id , {"is_deleted" : True , "is_active" : False }, session )
1645+
1646+
16051647def _check_enforcement_action_existennce (
16061648 new_enforcement_ids , requirement_id , inspection_id , session
16071649):
16081650 """
1609- Check if the corresponding enforcement action such as Order, Warning Letter etc.
1651+ Check if the corresponding enforcement action such as Order, Warning Letter, Administrative Penalty etc.
16101652
16111653 are created. This method throws validation error when these actions are not in DRAFTING status.
16121654 @param new_enforcement_ids: List of new enforcement action ids
@@ -1623,55 +1665,68 @@ def _check_enforcement_action_existennce(
16231665 removed_action_ids = set (existing_enforcement_ids ).difference (
16241666 set (new_enforcement_ids )
16251667 )
1626- if removed_action_ids :
1627- # If the removed enforcement action is either ORDER or WARNING LETTER, then check if
1628- # the order or warning letter is in DRAFTING status. If not, throw validation error.
1629- # If the order or warning letter is in DRAFTING status, then delete the order or warning letter.
1630- if EnforcementActionOptionEnum .ORDER .value in removed_action_ids :
1631- orders = OrderModel .get_by_inspection_id (inspection_id )
1632- requirement_orders = [
1633- order
1634- for order in orders
1635- if requirement_id
1636- in [
1637- order_map .inspection_requirement_id
1638- for order_map in order .order_requirement_maps
1639- ]
1640- ]
1641- if any (
1642- order .order_progress != OrderProgressEnum .DRAFTING
1643- for order in requirement_orders
1644- ):
1645- raise UnprocessableEntityError (
1646- "You cannot change enforcement action as order exists and is not in DRAFTING status."
1647- )
1648- for order in requirement_orders :
1649- OrderModel .update_order (
1650- order .id , {"is_deleted" : True , "is_active" : False }, session
1651- )
1652- if EnforcementActionOptionEnum .WARNING_LETTER .value in removed_action_ids :
1653- warnings = WarningLetterModel .get_by_inspection_id (inspection_id )
1654- requirement_warnings = [
1655- warning
1656- for warning in warnings
1657- if requirement_id
1658- in [
1659- warning_map .inspection_requirement_id
1660- for warning_map in warning .warning_letter_requirement_maps
1661- ]
1662- ]
1663- if any (
1664- warning .progress != WarningLetterProgressEnum .DRAFTING
1665- for warning in requirement_warnings
1666- ):
1667- raise UnprocessableEntityError (
1668- "You cannot change enforcement action as warning letter exists and is not in DRAFTING status."
1669- )
16701668
1671- for warning in requirement_warnings :
1672- WarningLetterModel .update_warning_letter (
1673- warning .id , {"is_deleted" : True , "is_active" : False }, session
1674- )
1669+ if not removed_action_ids :
1670+ return
1671+
1672+ # Configuration for each enforcement action type
1673+ enforcement_configs = {
1674+ EnforcementActionOptionEnum .ORDER .value : {
1675+ "model" : OrderModel ,
1676+ "status_field" : "order_progress" ,
1677+ "allowed_status" : OrderProgressEnum .DRAFTING ,
1678+ "map_field" : "order_requirement_maps" ,
1679+ "update_method" : OrderModel .update_order ,
1680+ "error_message" : "You cannot change enforcement action as order exists and is not in DRAFTING status." ,
1681+ },
1682+ EnforcementActionOptionEnum .WARNING_LETTER .value : {
1683+ "model" : WarningLetterModel ,
1684+ "status_field" : "progress" ,
1685+ "allowed_status" : WarningLetterProgressEnum .DRAFTING ,
1686+ "map_field" : "warning_letter_requirement_maps" ,
1687+ "update_method" : WarningLetterModel .update_warning_letter ,
1688+ "error_message" : (
1689+ "You cannot change enforcement action as warning letter exists and is not in DRAFTING status."
1690+ ),
1691+ },
1692+ EnforcementActionOptionEnum .ADMINISTRATIVE_PENALTY_RECOMMENDATION .value : {
1693+ "model" : AdministrativePenaltyModel ,
1694+ "status_field" : "referral_status" ,
1695+ "allowed_status" : ReferralStatusEnum .DRAFTING ,
1696+ "map_field" : "administrative_penalty_requirement_maps" ,
1697+ "update_method" : AdministrativePenaltyModel .update_administrative_penalty ,
1698+ "error_message" : (
1699+ "You cannot change enforcement action as administrative penalty exists and is not in DRAFTING status."
1700+ ),
1701+ },
1702+ EnforcementActionOptionEnum .VIOLATION_TICKET .value : {
1703+ "model" : ViolationTicketModel ,
1704+ "status_field" : "status" ,
1705+ "allowed_status" : ViolationTicketStatusEnum .ISSUED ,
1706+ "map_field" : "violation_ticket_requirement_maps" ,
1707+ "update_method" : ViolationTicketModel .update_violation_ticket ,
1708+ "error_message" : (
1709+ "You cannot change enforcement action as violation ticket exists and is not in ISSUED status."
1710+ ),
1711+ },
1712+ EnforcementActionOptionEnum .CHARGE_RECOMMENDATION .value : {
1713+ "model" : ChargeRecommendationModel ,
1714+ "status_field" : "status" ,
1715+ "allowed_status" : ChargeRecommendationStatusEnum .DRAFTING ,
1716+ "map_field" : "charge_recommendation_requirement_maps" ,
1717+ "update_method" : ChargeRecommendationModel .update_charge_recommendation ,
1718+ "error_message" : (
1719+ "You cannot change enforcement action as charge recommendation exists and is not in DRAFTING status."
1720+ ),
1721+ },
1722+ }
1723+
1724+ # Process each removed enforcement action
1725+ for action_id in removed_action_ids :
1726+ if action_id in enforcement_configs :
1727+ _validate_and_delete_enforcement_action (
1728+ enforcement_configs [action_id ], requirement_id , inspection_id , session
1729+ )
16751730
16761731
16771732def _update_the_findigs_by_images (
0 commit comments