diff --git a/app/domain/regulation_computations.py b/app/domain/regulation_computations.py index ad482a93..41aa2641 100644 --- a/app/domain/regulation_computations.py +++ b/app/domain/regulation_computations.py @@ -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() + ) diff --git a/app/models/controller_control.py b/app/models/controller_control.py index ece6a590..0e328c25 100644 --- a/app/models/controller_control.py +++ b/app/models/controller_control.py @@ -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 diff --git a/app/tests/regulations/test_report_infractions.py b/app/tests/regulations/test_report_infractions.py new file mode 100644 index 00000000..c94f9830 --- /dev/null +++ b/app/tests/regulations/test_report_infractions.py @@ -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)