Integrate ConditionEvaluatorService Into ActionValidationService

by ADMIN 65 views

Ticket 3.3: Integrate ConditionEvaluatorService into ActionValidationService

Goal

The primary objective of this ticket is to refactor the ActionValidationService._checkSinglePrerequisite method to delegate prerequisite evaluation to the ConditionEvaluatorService. This involves injecting the ConditionEvaluatorService into the ActionValidationService constructor, locating the prerequisite loop in the isValid method, removing the old logic, and implementing a new loop that iterates through JSON Logic objects and calls the evaluate method of the ConditionEvaluatorService.

Description

The current ActionValidationService._checkSinglePrerequisite method contains placeholder logic or custom condition handling. This ticket replaces that logic entirely. The refactored code will iterate through the prerequisites array (now containing JSON Logic objects), pass each object along with the pre-assembled JsonLogicEvaluationContext (from Ticket 3.2) to ConditionEvaluatorService.evaluate, and handle the boolean result.

Tasks

Inject ConditionEvaluatorService

To integrate the ConditionEvaluatorService into the ActionValidationService, we need to modify the constructor to accept an instance of ConditionEvaluatorService (or JsonLogicEvaluationService) as a dependency and store it (e.g., this.#conditionEvaluatorService). We also need to update the dependency injection setup accordingly.

// Before
class ActionValidationService {
  constructor() {
    // ...
  }
}

// After
class ActionValidationService {
  constructor(conditionEvaluatorService) {
    this.#conditionEvaluatorService = conditionEvaluatorService;
  }
}

Locate Prerequisite Loop

We need to find the loop within ActionValidationService.isValid that iterates over actionDefinition.prerequisites. This loop likely currently calls _checkSinglePrerequisite.

// Before
class ActionValidationService {
  isValid(actionDefinition) {
    // ...
    for (const prerequisite of actionDefinition.prerequisites) {
      this._checkSinglePrerequisite(prerequisite);
    }
    // ...
  }
}

Remove Old Logic

We need to delete the ActionValidationService._checkSinglePrerequisite method entirely. Its logic is being replaced.

// Before
class ActionValidationService {
  _checkSinglePrerequisite(prerequisite) {
    // Old logic
  }
}

Implement New Loop Logic

Inside the prerequisites loop in isValid, we need to:

  • For each prerequisiteRule (which is now a JSON Logic object) in the actionDefinition.prerequisites array:
  • Retrieve the JsonLogicEvaluationContext assembled in Ticket 3.2.
  • Call this.#conditionEvaluatorService.evaluate(prerequisiteRule, evaluationContext).
  • Log the rule being evaluated and the boolean result for debugging.
  • If the result is false, immediately return false from isValid (action validation fails). Add appropriate debug logging indicating which prerequisite failed.
// After
class ActionValidationService {
  isValid(actionDefinition) {
    // ...
    for (const prerequisiteRule of actionDefinition.prerequisites) {
      const evaluationContext = this._getJsonLogicEvaluationContext();
      const result = this.#conditionEvaluatorService.evaluate(prerequisiteRule evaluationContext);
      console.log(`Evaluating prerequisite: ${prerequisiteRule}`);
      console.log(`Result: ${result}`);
      if (!result) {
        console.log(`Prerequisite failed: ${prerequisiteRule}`);
        return false;
      }
    }
    // ...
  }
}

Acceptance Criteria

To ensure that the integration of the ConditionEvaluatorService into the ActionValidationService is successful, we need to meet the following acceptance criteria:

  • ActionValidationService correctly injects and uses the ConditionEvaluatorService.
  • The old _checkSinglePrerequisite method is removed.
  • The loop processing actionDefinition.prerequisites in isValid now iterates through JSON Logic objects.
  • Inside the loop, conditionEvaluatorService.evaluate is called for each prerequisiteRule, passing the rule and the correct JsonLogicEvaluationContext.
  • The boolean result from evaluate is correctly checked.
  • isValid returns false immediately if any prerequisite evaluation returns false.
  • Appropriate debug logging is added for prerequisite evaluation start, result, and failure.

Dependencies

To complete this task, we need to ensure that the following dependencies are met:

  • Ticket 2 (Runtime Evaluator): ConditionEvaluatorService (or JsonLogicEvaluationService) MUST be implemented and provide an evaluate(rule, context) method returning a boolean.
  • Ticket 3.2: The JsonLogicEvaluationContext MUST be correctly assembled before this loop executes.

Estimate

Ticket 3.3: Integrate ConditionEvaluatorService into ActionValidationService

Q&A

Q: What is the primary objective of this ticket? A: The primary objective of this ticket is to refactor the ActionValidationService._checkSinglePrerequisite method to delegate prerequisite evaluation to the ConditionEvaluatorService.

Q: Why is the current ActionValidationService._checkSinglePrerequisite method being replaced? A: The current ActionValidationService._checkSinglePrerequisite method contains placeholder logic or custom condition handling. This ticket replaces that logic entirely.

Q: What is the JsonLogicEvaluationContext and how is it used? A: The JsonLogicEvaluationContext is a pre-assembled context that is passed to the ConditionEvaluatorService.evaluate method. It is used to evaluate the JSON Logic objects in the actionDefinition.prerequisites array.

Q: How is the ConditionEvaluatorService injected into the ActionValidationService? A: The ConditionEvaluatorService is injected into the ActionValidationService constructor, where it is stored as a dependency (e.g., this.#conditionEvaluatorService).

Q: What is the new loop logic in the isValid method? A: The new loop logic in the isValid method iterates through the actionDefinition.prerequisites array, passing each JSON Logic object to the ConditionEvaluatorService.evaluate method along with the pre-assembled JsonLogicEvaluationContext. The boolean result from the evaluation is then checked, and if it is false, the method returns false immediately.

Q: What are the acceptance criteria for this ticket? A: The acceptance criteria for this ticket are:

  • ActionValidationService correctly injects and uses the ConditionEvaluatorService.
  • The old _checkSinglePrerequisite method is removed.
  • The loop processing actionDefinition.prerequisites in isValid now iterates through JSON Logic objects.
  • Inside the loop, conditionEvaluatorService.evaluate is called for each prerequisiteRule, passing the rule and the correct JsonLogicEvaluationContext.
  • The boolean result from evaluate is correctly checked.
  • isValid returns false immediately if any prerequisite evaluation returns false.
  • Appropriate debug logging is added for prerequisite evaluation start, result, and failure.

Q: What are the dependencies for this ticket? A: The dependencies for this ticket are:

  • Ticket 2 (Runtime Evaluator): ConditionEvaluatorService (or JsonLogicEvaluationService) MUST be implemented and provide an evaluate(rule, context) method returning a boolean.
  • Ticket 3.2: The JsonLogicEvaluationContext MUST be correctly assembled before this loop executes.

Q: What is the estimate for this task? A: The estimate for this task is Medium (Involves refactoring core validation logic and integrating a new service).

Troubleshooting

Q: What if the ConditionEvaluatorService is not injected correctly? A: If the ConditionEvaluatorService is not injected correctly, the ActionValidationService will not be able to use it to evaluate the prerequisites. This can be checked by verifying that the EvaluatorService is being injected into the ActionValidationService constructor.

Q: What if the JsonLogicEvaluationContext is not assembled correctly? A: If the JsonLogicEvaluationContext is not assembled correctly, the ConditionEvaluatorService will not be able to evaluate the prerequisites correctly. This can be checked by verifying that the JsonLogicEvaluationContext is being assembled correctly before the loop in the isValid method.

Q: What if the loop in the isValid method is not iterating through the actionDefinition.prerequisites array correctly? A: If the loop in the isValid method is not iterating through the actionDefinition.prerequisites array correctly, the prerequisites will not be evaluated correctly. This can be checked by verifying that the loop is iterating through the array correctly and that the ConditionEvaluatorService is being called for each prerequisite.