def migrate_case_alert_schedule_instances(self):
        seen_case_ids = set()
        recipient = self.rule.memoized_actions[0].definition.recipients[0]

        for reminder in self.get_source_instances():
            if reminder.case_id in seen_case_ids:
                self.source_duplicate_count += 1
                continue

            seen_case_ids.add(reminder.case_id)

            instance = CaseAlertScheduleInstance(
                domain=self.rule.domain,
                recipient_type=recipient[0],
                recipient_id=recipient[1],
                current_event_num=reminder.current_event_sequence_num,
                schedule_iteration_num=reminder.schedule_iteration_num,
                next_event_due=reminder.next_fire,
                active=reminder.active,
                alert_schedule_id=self.schedule.schedule_id,
                case_id=reminder.case_id,
                rule_id=self.rule.pk,
            )

            if reminder.active and reminder.error:
                self.schedule.move_to_next_event_not_in_the_past(instance)

            instance.save(force_insert=True)
Example #2
0
 def create_case_alert_schedule_instance(self, domain, case_id):
     instance = CaseAlertScheduleInstance(
         domain=domain,
         recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_SELF,
         current_event_num=0,
         schedule_iteration_num=1,
         next_event_due=datetime(2018, 7, 1),
         active=True,
         alert_schedule_id=uuid.uuid4(),
         case_id=case_id,
         rule_id=1,
     )
     instance.save()
     self.addCleanup(instance.delete)
 def create_case_alert_schedule_instance(self, domain, case_id):
     instance = CaseAlertScheduleInstance(
         domain=domain,
         recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_SELF,
         current_event_num=0,
         schedule_iteration_num=1,
         next_event_due=datetime(2018, 7, 1),
         active=True,
         alert_schedule_id=uuid.uuid4(),
         case_id=case_id,
         rule_id=1,
     )
     instance.save()
     self.addCleanup(instance.delete)
Example #4
0
 def create_new_instance_for_recipient(self, recipient_type, recipient_id):
     if self.model_instance:
         instance = CaseAlertScheduleInstance.copy_for_recipient(
             self.model_instance, recipient_type, recipient_id)
         self.handle_existing_instance(instance)
         return instance
     else:
         return CaseAlertScheduleInstance.create_for_recipient(
             self.schedule,
             recipient_type,
             recipient_id,
             move_to_next_event_not_in_the_past=False,
             case_id=self.case.case_id,
             rule_id=self.rule.pk,
             last_reset_case_property_value=self.reset_case_property_value,
         )
Example #5
0
    def create_new_instance_for_recipient(self, recipient_type, recipient_id):
        if self.model_instance:
            instance = CaseAlertScheduleInstance.copy_for_recipient(
                self.model_instance, recipient_type, recipient_id)

            if self.action_definition.reset_case_property_name:
                handle_case_alert_schedule_instance_reset(
                    instance, self.schedule, self.reset_case_property_value)

            return instance
        else:
            return CaseAlertScheduleInstance.create_for_recipient(
                self.schedule,
                recipient_type,
                recipient_id,
                move_to_next_event_not_in_the_past=False,
                case_id=self.case.case_id,
                rule_id=self.rule.pk,
                last_reset_case_property_value=self.reset_case_property_value,
            )
Example #6
0
    def create_new_instance_for_recipient(self, recipient_type, recipient_id):
        if self.model_instance:
            instance = CaseAlertScheduleInstance.copy_for_recipient(
                self.model_instance,
                recipient_type,
                recipient_id
            )

            if self.action_definition.reset_case_property_name:
                handle_case_alert_schedule_instance_reset(instance, self.schedule, self.reset_case_property_value)

            return instance
        else:
            return CaseAlertScheduleInstance.create_for_recipient(
                self.schedule,
                recipient_type,
                recipient_id,
                move_to_next_event_not_in_the_past=False,
                case_id=self.case.case_id,
                rule_id=self.rule.pk,
                last_reset_case_property_value=self.reset_case_property_value,
            )
Example #7
0
    existing_instances = {
        (instance.recipient_type, instance.recipient_id): instance
        for instance in get_case_alert_schedule_instances_for_schedule(case.case_id, schedule)
    }

    if existing_instances:
        # Don't refresh AlertSchedules that have already been sent
        # to avoid sending old alerts to new recipients
        return

    recipients = set(convert_to_tuple_of_tuples(action_definition.recipients))
    for recipient_type, recipient_id in recipients:
        instance = CaseAlertScheduleInstance.create_for_recipient(
            schedule,
            recipient_type,
            recipient_id,
            move_to_next_event_not_in_the_past=False,
            case_id=case.case_id,
            rule_id=rule.pk
        )
        save_case_schedule_instance(instance)


def refresh_case_timed_schedule_instances(case, schedule, action_definition, rule, start_date=None):
    """
    :param case: the CommCareCase/SQL
    :param schedule: the TimedSchedule
    :param action_definition: the CreateScheduleInstanceActionDefinition that is
    causing the schedule instances to be refreshed
    :param rule: the AutomaticUpdateRule that is causing the schedule instances
    to be refreshed