From bb4eb3ef53d6646ed5b6fa6c1dd080bac3a5812b Mon Sep 17 00:00:00 2001
From: Krystian Igras <krystian.igras@contractors.roche.com>
Date: Mon, 27 Feb 2023 09:30:58 +0000
Subject: [PATCH] Readjust step and filter ids after removing it.

---
 DESCRIPTION        | 2 +-
 NEWS.md            | 3 ++-
 R/cohort_methods.R | 6 ++++--
 R/list_operators.R | 5 +++++
 R/step.R           | 7 +++++++
 renv/settings.dcf  | 2 +-
 6 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 11c67af..b609264 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: cohortBuilder
 Type: Package
 Title: Data Source Agnostic Filtering Tools
-Version: 0.1.2
+Version: 0.1.2.9000
 Authors@R: 
     c(person('Krystian', 'Igras', 
              email = 'krystian8207@gmail.com', 
diff --git a/NEWS.md b/NEWS.md
index 5f06494..dbd915f 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,8 +1,9 @@
-# cohortBuilder 0.1.2
+# cohortBuilder (development version)
 
 * Changed the way reproducible code is returned. Now more flexibility is allowed with using e.g. `.repro_code_tweak` method.
 * The `tblist` source reproducible code is now using pipe chains for each dataset filtering.
 * Optimized filtering with having cache computed only for active filters.
+* Properly readjust steps and filters ids after step is removed.
 
 # cohortBuilder 0.1.1
 
diff --git a/R/cohort_methods.R b/R/cohort_methods.R
index 93171e8..d0a8dd7 100644
--- a/R/cohort_methods.R
+++ b/R/cohort_methods.R
@@ -150,10 +150,12 @@ Cohort <- R6::R6Class(
       run_hooks(hook$pre, self, private, step_id)
 
       step_id <- as.character(step_id)
+      clear_data_ids <- steps_range(step_id, rev(names(private$steps))[1])
       private$steps[[step_id]] <- NULL
-      private$cache[[step_id]] <- NULL
-      private$data_objects[steps_range(step_id, length(private$data_objects))] <- NULL
+      private$cache[clear_data_ids] <- NULL
+      private$data_objects[clear_data_ids] <- NULL
       private$steps <- adjust_names(private$steps)
+      private$steps <- purrr::imodify(private$steps, readjust_step)
       if (!is.null(private$steps) && run_flow) {
         self$run_flow(min_step = step_id)
       }
diff --git a/R/list_operators.R b/R/list_operators.R
index 2cec54c..a7ddba2 100644
--- a/R/list_operators.R
+++ b/R/list_operators.R
@@ -35,6 +35,11 @@ rename_item <- function(list_obj, old_name, new_name) {
   return(list_obj)
 }
 
+modify_item <- function(list_obj, new_val, what) {
+  list_obj[[what]] <- new_val
+  return(list_obj)
+}
+
 #' Get function definition
 #'
 #' Whenever the function with provided name exists anywhere, the one is
diff --git a/R/step.R b/R/step.R
index db3c63d..b98ce5e 100644
--- a/R/step.R
+++ b/R/step.R
@@ -88,6 +88,13 @@ steps_range <- function(from, to) {
   )
 }
 
+readjust_step <- function(step, new_id) {
+  step$id <- new_id
+  step$filters <- purrr::modify(step$filters, modify_item, new_val = new_id, what = "step_id")
+
+  return(step)
+}
+
 prev_step <- function(idx) {
   as.character(as.integer(idx) - 1)
 }
diff --git a/renv/settings.dcf b/renv/settings.dcf
index 205616d..6456a7a 100644
--- a/renv/settings.dcf
+++ b/renv/settings.dcf
@@ -1,5 +1,5 @@
 external.libraries:
-ignored.packages:
+ignored.packages: cohortBuilder
 package.dependency.fields: Imports, Depends, LinkingTo
 r.version:
 snapshot.type: all