def _predict_qty_of_terminals_needed(
        self, expedition: ExpeditionsEntity = ExpeditionsEntity()
    ) -> ExpeditionsEntity:
        wc: WorkCentersEntity = self._work_center_repository.find(
            expedition.work_center.id)
        wc.calcule_qty_of_terminals_available()

        avg_of_consume_in_wc = self._work_center_repository.get_average_of_attendence_by_days_period(
            expedition.work_center, wc.days_qty_ideal_for_coverage)

        if avg_of_consume_in_wc != 0:
            expedition.qty_of_terminals = self._business_rules.get_right_qty_to_cover_the_demand_by_days(
                wc.days_qty_ideal_for_coverage, avg_of_consume_in_wc,
                wc.qty_of_terminals_available)
        return expedition
    def on_post(self, req: Request, resp: Response):
        expedition = {}
        try:
            expedition_data = req.media
            destiny_of_expedition = None
            work_center_id = expedition_data.get('work_center_id')

            if work_center_id != None:
                destiny_of_expedition = self._work_centers_use_case.find(
                    work_center_id)

            if destiny_of_expedition == None:
                falcon.HTTPError(
                    "Error", ExpeditionOperationsRejectionMessages.
                    WORK_CENTER_IS_REQUIRED)

            expedition = ExpeditionsEntity()
            expedition.fill(
                qty_of_terminals=expedition_data.get('qty_of_terminals'),
                work_center=destiny_of_expedition,
                auto_predict_qty_needed=expedition_data.get(
                    'auto_predict_qty_needed'),
            )

            expedition = self._resource_use_cases.create(expedition)
            self._work_centers_use_case.update_calculated_values(
                destiny_of_expedition)
            expedition_updated = self._resource_use_cases.find(expedition.id)

        except UseCaseException as ex:
            raise falcon.HTTPBadRequest(falcon.HTTP_400, str(ex))
        except Exception as ex:
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))

        resp.status = falcon.HTTP_CREATED
        resp.body = falcon.media.JSONHandler().serialize(
            expedition_updated.to_dict(), falcon.MEDIA_JSON)
    def test_is_getting_right_qty_of_terminals_in_send_auto_predict_expeditions(self):
        use_cases = WorkCentersUseCases(self._data_source)
        work_center = use_cases.create(self._fake_wc_entity)
        business_rules = WorkCenterBusinessRules()
        expedition_use_case = ExpeditionsUseCases(self._data_source)

        self._register_fake_attendences_in(work_center, 75, work_center.days_qty_ideal_for_coverage)
        self._register_fake_expeditions_in(work_center, 9, 10)

        work_center_updated = use_cases.find(work_center.id)

        terminails_available_before = business_rules.get_qty_of_terminals_available(
            work_center_updated)
        avg_attendance = use_cases.get_average_of_attendences_in_wc(
            work_center_updated, work_center.days_qty_ideal_for_coverage)

        classfication_before = business_rules.get_coverage_classification(
            terminails_available_before, avg_attendance, work_center.days_qty_ideal_for_coverage)

        expedition = expedition_use_case.create(ExpeditionsEntity(
            work_center=work_center,
            auto_predict_qty_needed=True
        ))
        
        # Atualizando dados do work_center
        work_center_updated = use_cases.find(work_center.id)

        terminails_available = business_rules.get_qty_of_terminals_available(
            work_center_updated)
        
        avg_attendance = use_cases.get_average_of_attendences_in_wc(
            work_center_updated, work_center.days_qty_ideal_for_coverage)
        classfication_after = business_rules.get_coverage_classification(
            terminails_available, avg_attendance, work_center.days_qty_ideal_for_coverage)

        self.assertEqual(classfication_before, CoverageClassifications.RED)
        self.assertEqual(classfication_after, CoverageClassifications.GREEN)
    def create(
        self, expedition: ExpeditionsEntity = ExpeditionsEntity()
    ) -> ExpeditionsEntity:
        if expedition.work_center == None:
            raise UseCaseException(
                ExpeditionOperationsRejectionMessages.WORK_CENTER_IS_REQUIRED)

        if (expedition.auto_predict_qty_needed is False
                and (expedition.qty_of_terminals == None
                     or expedition.qty_of_terminals == 0)):
            raise UseCaseException(ExpeditionOperationsRejectionMessages.
                                   QTY_OF_TERMINALS_IS_REQUIRED)

        try:
            if (expedition.auto_predict_qty_needed):
                expedition = self._predict_qty_of_terminals_needed(expedition)

            model_created = self._expeditions_repository.persist(expedition)
            self._expeditions_repository.save_transaction()
        except Exception as ex:
            self._expeditions_repository.revert_transaction()
            raise ex

        return model_created.to_entity()
 def to_entity(self) -> ExpeditionsEntity:
     return ExpeditionsEntity(self.id, self.qty_of_terminals,
                              self.was_canceled, self.work_center,
                              self.auto_predict_qty_needed)
 def _register_fake_expeditions_in(self, work_center: WorkCentersEntity, qty_of_expeditions: int = 1, qty_of_terminals_per_expedition: int = 100):
     while len(ExpeditionsUseCases(self._data_source).get_all()) < qty_of_expeditions:
         ExpeditionsUseCases(self._data_source).create(ExpeditionsEntity(
             qty_of_terminals=qty_of_terminals_per_expedition,
             work_center=work_center
         ))
 def cancel_expedition(self,
                       entity: ExpeditionsEntity) -> ExpeditionsEntity:
     entity.was_canceled = True
     return self.update(entity)