@Override
  @Transactional(readOnly = false)
  public Defect reviewDefect(Long defectId, String assignedTo) {
    Defect existingDefect = defectRepository.getById(defectId);
    String existingAsignee = existingDefect.getAssignedTo();

    existingDefect.setStatus(DefectStatus.ASSIGNED);
    existingDefect.setAssignedTo(assignedTo);
    defectRepository.update(existingDefect);

    for (Task task : taskService.createTaskQuery().taskAssignee(existingAsignee).list()) {
      if (task.getName().equalsIgnoreCase("reviewDefect")) {
        Long taskDefectId = (Long) runtimeService.getVariable(task.getExecutionId(), "defectId");
        if (taskDefectId.equals(defectId)) {
          taskService.complete(task.getId());
          logger.info("Reviewed Defect: " + task.getName() + " Defect Id: " + taskDefectId);
        }
      }
    }
    logger.info(
        "Task assigned to: "
            + existingAsignee
            + " : "
            + taskService.createTaskQuery().taskAssignee(existingAsignee));
    logger.info(
        "Task assigned to: "
            + assignedTo
            + " : "
            + taskService.createTaskQuery().taskAssignee(assignedTo));

    return existingDefect;
  }
  @Override
  @Transactional(readOnly = false)
  public Defect createDefect(Defect defect) {
    defect.setCreatedDate(new LocalDate());
    defect.setStatus(DefectStatus.NEW);
    Defect newDefect = defectRepository.save(defect);

    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("defectId", newDefect.getId());
    vars.put("assignee", defect.getAssignedTo());
    runtimeService.startProcessInstanceByKey(
        DEFECT_TRACKING_PROCESS_KEY, newDefect.getId().toString(), vars);
    return defect;
  }
  @Override
  @Transactional(readOnly = false)
  public Defect markResolved(Long defectId, String assignedTo, String resolution) {
    Defect existingDefect = getDefectById(defectId);
    String existingAssignee = existingDefect.getAssignedTo();
    existingDefect.setResolution(resolution);
    existingDefect.setStatus(DefectStatus.RESOLVED);
    existingDefect.setAssignedTo(assignedTo);
    defectRepository.update(existingDefect);

    for (Task task : taskService.createTaskQuery().taskAssignee(existingAssignee).list()) {
      if (task.getName().equalsIgnoreCase("resolveDefect")) {
        Long taskDefectId = (Long) runtimeService.getVariable(task.getExecutionId(), "defectId");
        if (taskDefectId.equals(defectId)) {
          taskService.complete(task.getId());
          logger.info("Resolved Defect: " + task.getName() + " Defect Id: " + taskDefectId);
        }
      }
    }

    return existingDefect;
  }