Skip to content

Commit

Permalink
Merge pull request #451 from MTES-MCT/fix/bug-report-infractions
Browse files Browse the repository at this point in the history
Fix/bug report infractions
  • Loading branch information
tristan-gueguen authored Nov 6, 2024
2 parents 17972aa + eb5c2bd commit 03f7946
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
12 changes: 8 additions & 4 deletions app/domain/regulation_computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def get_regulatory_alerts(user_id, start_date=None, end_date=None):


def get_regulatory_computations(user_id, start_date=None, end_date=None):
return RegulationComputation.query.filter(
RegulationComputation.user_id == user_id,
RegulationComputation.day.between(start_date, end_date),
).all()
return (
RegulationComputation.query.filter(
RegulationComputation.user_id == user_id,
RegulationComputation.day.between(start_date, end_date),
)
.order_by(RegulationComputation.creation_time)
.all()
)
17 changes: 10 additions & 7 deletions app/models/controller_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,30 @@ def report_infractions(self):
)
observed_infractions = []
for regulatory_alert in regulatory_alerts:

extra = regulatory_alert.extra
if not extra or not "sanction_code" in extra:
continue

regulation_computations_for_the_day = [
rc
for rc in regulation_computations
if rc.day == regulatory_alert.day
]
if len(regulation_computations_for_the_day) == 0:
nb_rc_for_the_day = len(regulation_computations_for_the_day)
if nb_rc_for_the_day == 0:
continue
if (
len(regulation_computations_for_the_day) == 2
nb_rc_for_the_day == 2
and regulatory_alert.submitter_type != SubmitterType.ADMIN
):
continue
if (
regulation_computations_for_the_day[0].submitter_type
nb_rc_for_the_day == 1
and regulation_computations_for_the_day[0].submitter_type
!= regulatory_alert.submitter_type
):
continue

extra = regulatory_alert.extra
if not extra or not "sanction_code" in extra:
continue
sanction_code = extra.get("sanction_code")
is_reportable = "NATINF" in sanction_code
check_type = regulatory_alert.regulation_check.type
Expand Down
54 changes: 54 additions & 0 deletions app/tests/regulations/test_report_infractions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import datetime

from app import db
from app.helpers.submitter_type import SubmitterType
from app.models import RegulationComputation, RegulatoryAlert, RegulationCheck
from app.models.controller_control import ControllerControl, ControlType
from app.seed import ControllerUserFactory
from app.seed.helpers import get_date
from app.tests.regulations import RegulationsTest


class TestReportInfractions(RegulationsTest):
def test_report_infractions_take_correct_alerts(self):
controller_user = ControllerUserFactory.create()
work_day = get_date(how_many_days_ago=2)
random_regulation_check = RegulationCheck.query.first()

# order is important - this order gave a bug when the test was written
for submitter_type in [SubmitterType.EMPLOYEE, SubmitterType.ADMIN]:
db.session.add(
RegulationComputation(
day=work_day,
submitter_type=submitter_type,
user=self.employee,
)
)

db.session.add(
RegulatoryAlert(
day=work_day,
submitter_type=SubmitterType.ADMIN,
user=self.employee,
extra={"sanction_code": "Code"},
regulation_check=random_regulation_check,
)
)

new_control = ControllerControl(
qr_code_generation_time=datetime.datetime.now(),
user_id=self.employee.id,
user_first_name=self.employee.first_name,
user_last_name=self.employee.last_name,
control_type=ControlType.mobilic,
controller_id=controller_user.id,
company_name=self.company.name,
vehicle_registration_number="AAA 11 BBB",
nb_controlled_days=28,
)
db.session.add(new_control)
db.session.commit()

new_control.report_infractions()

self.assertEqual(len(new_control.observed_infractions), 1)

0 comments on commit 03f7946

Please sign in to comment.