execute method

Future<EventRuleResult> execute({
  1. required Event event,
  2. required String program,
  3. EventDataValue? changedEventDataValue,
  4. String? trackedEntityInstance,
})

Implementation

Future<EventRuleResult> execute(
    {required Event event,
    required String program,
    EventDataValue? changedEventDataValue,
    String? trackedEntityInstance}) async {
  List<ProgramRule> programRules = await ProgramRuleQuery(database: database)
      .withActions()
      .where(attribute: 'program', value: program)
      .get();

  List<ProgramRuleVariable> programRuleVariables =
      await ProgramRuleVariableQuery(database: database)
          .where(attribute: 'program', value: program)
          .get();

  List<EventDataValue> eventDataValues =
      await EventDataValueQuery(database: database)
          .where(attribute: 'event', value: event.id)
          .get();

  Map<String, DataValueObject> dataValueEntities =
      DataValueEntities.fromEventDataValues(eventDataValues);

  if (trackedEntityInstance != null) {
    List<TrackedEntityAttributeValue> attributes =
        await TrackedEntityAttributeValueQuery(database: database)
            .where(
                attribute: 'trackedEntityInstance',
                value: trackedEntityInstance)
            .get();

    dataValueEntities = DataValueEntities.fromAttributeValues(attributes,
        initialEntities: dataValueEntities);
  }

  List<ProgramRuleAction> programRuleActions = ProgramRuleEngine.execute(
    dataValueEntities: dataValueEntities,
    programRules: programRules,
    programRuleVariables: programRuleVariables,
  );

  final queue = Queue(parallel: 50);
  num availableItemCount = 0;

  programRuleActions.forEach((programRuleAction) async {
    if (programRuleAction.programRuleActionType == 'ASSIGN' &&
        programRuleAction.dataElement != null &&
        programRuleAction.data != null &&
        programRuleAction.data != '') {
      availableItemCount++;
      queue.add(() => EventDataValueQuery(database: database)
          .setData(EventDataValue(
              lastUpdated: DateTime.now().toIso8601String(),
              created: DateTime.now().toIso8601String(),
              id: '${event.id}_${programRuleAction.dataElement}',
              name: '${event.event}_${programRuleAction.dataElement}',
              dirty: true,
              dataElement: programRuleAction.dataElement as String,
              event: event.id,
              value: programRuleAction.data as String))
          .save());
    }
  });

  if (availableItemCount == 0) {
    queue.cancel();
  } else {
    await queue.onComplete;
  }
  Event updatedEvent = await EventQuery(database: database)
      .withDataValues()
      .byId(event.id as String)
      .getOne();

  return EventRuleResult(
      event: updatedEvent, programRuleActions: programRuleActions);
}