fix: reset is_blocked_due_to_contract flag when intervention is assigned to an unblocked contract

This commit is contained in:
kdeterme 2026-09-02 09:56:07 +02:00
parent 4824415290
commit 9429402292
2 changed files with 42 additions and 3 deletions

View file

@ -499,6 +499,8 @@ class Intervention(models.Model):
self.is_blocked_due_to_contract = True
if is_new and self.status in ('to_be_processed', 'assigned', 'in_progress'):
self.status = 'in_preparation'
else:
self.is_blocked_due_to_contract = False
if self.status in STATUS_ORDERS:
self.status_order = STATUS_ORDERS[self.status]

View file

@ -1,5 +1,5 @@
import json
from django.test import TestCase
from django.test import TestCase, RequestFactory
from django.contrib.auth import get_user_model
from django.utils import timezone
@ -16,6 +16,7 @@ from interventions.views.helpers import get_contract_recipients, get_contract_cc
class BlockedContractsTests(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.User = get_user_model()
self.thematic, _ = Thematic.objects.get_or_create(code='trafficlights', defaults={'name': 'Feux de signalisation'})
self.category, _ = AssetCategory.objects.get_or_create(name_fr='Test Category', thematic=self.thematic)
@ -62,8 +63,8 @@ class BlockedContractsTests(TestCase):
uca_blocked = UserContractAccess.objects.create(user_config=self.internal_cfg, contract=self.contract_blocked, can_view_interventions=True)
from common.models import UserContractStatusPermission
for st in ['in_preparation', 'in_progress', 'to_be_processed']:
UserContractStatusPermission.objects.create(user_contract=uca_active, status=st, can_view=True)
UserContractStatusPermission.objects.create(user_contract=uca_blocked, status=st, can_view=True)
UserContractStatusPermission.objects.create(user_contract=uca_active, status=st, can_view=True, can_change_status_to=True)
UserContractStatusPermission.objects.create(user_contract=uca_blocked, status=st, can_view=True, can_change_status_to=True)
# External user (contractor)
self.external_user = self.User.objects.create_user(username='provider_user', password='pwd')
@ -218,3 +219,39 @@ class BlockedContractsTests(TestCase):
filtered_unblocked = apply_intervention_filters_from_querydict(base_qs, qdict_unblocked)
self.assertIn(itv_active, filtered_unblocked)
self.assertNotIn(itv_blocked, filtered_unblocked)
def test_unblocking_when_changing_contract_to_active(self):
symptom = Symptom.objects.create(name_fr='Feu éteint', thematic=self.thematic)
# 1. Create intervention on blocked contract
itv = Intervention.objects.create(
title='Intervention initiale bloquée',
contract=self.contract_blocked,
assigned_provider=self.company,
status='in_preparation',
thematic=self.thematic,
asset_category=self.category,
symptom=symptom,
)
self.assertTrue(itv.is_blocked_due_to_contract)
self.assertTrue(itv.is_contract_blocked)
allowed_before = user_allowed_transitions(self.internal_user, 'in_preparation', intervention=itv)
self.assertNotIn('to_be_processed', allowed_before)
# 2. Reassign to active contract
itv.contract = self.contract_active
itv.save()
# 3. Verify flags are reset and workflow is unlocked
self.assertFalse(itv.is_blocked_due_to_contract)
self.assertFalse(itv.is_contract_blocked)
allowed_after = user_allowed_transitions(self.internal_user, 'in_preparation', intervention=itv)
self.assertIn('to_be_processed', allowed_after)
# 4. Verify transition to to_be_processed succeeds
request = self.factory.post('/workflow/update-status/')
request.user = self.internal_user
success, msg, _ = attempt_status_update(request, itv, self.internal_user, 'to_be_processed')
self.assertTrue(success)
itv.refresh_from_db()
self.assertEqual(itv.status, 'to_be_processed')