diff --git a/R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R b/R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R index 341ce47..bd89183 100644 --- a/R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R +++ b/R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R @@ -4,9 +4,12 @@ #' given a DBI connection object to the REDCap database and some optional #' parameters to narrow the returned result. #' +#' Optionally filter the data with the data range `[start_date, end_date)`. +#' #' @param conn a DBI connection object to the REDCap database #' @param ehr_id a vector of REDCap EHR_IDs for the EHR(s) of interest (optional) #' @param start_date The first date from which we should return results (optional) +#' @param end_date The last date (non-inclusive) from which we should return results (optional) #' #' @return A dataframe suitable for generating a HIPAA disclosure log #' @export @@ -29,7 +32,8 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs <- function( conn, ehr_id = NA_real_, - start_date = as.Date(NA)) { + start_date = as.Date(NA), + end_date = as.Date(NA)) { # rename parameters for local use ehr_id_local <- ehr_id @@ -68,6 +72,7 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs <- function( dplyr::tbl(conn, "redcap_ehr_fhir_logs") |> dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |> dplyr::filter(is.na(start_date) | .data$created_at >= start_date) |> + dplyr::filter(is.na(end_date) | .data$created_at < end_date) |> dplyr::filter(ehr_id_is_na | .data$ehr_id %in% ehr_id_local) |> dplyr::left_join(user_information, by = c("user_id" = "ui_id")) |> dplyr::left_join(projects, by = c("project_id")) |> diff --git a/man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd b/man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd index d6ee0b6..726df5b 100644 --- a/man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd +++ b/man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd @@ -7,7 +7,8 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs( conn, ehr_id = NA_real_, - start_date = as.Date(NA) + start_date = as.Date(NA), + end_date = as.Date(NA) ) } \arguments{ @@ -16,6 +17,8 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs( \item{ehr_id}{a vector of REDCap EHR_IDs for the EHR(s) of interest (optional)} \item{start_date}{The first date from which we should return results (optional)} + +\item{end_date}{The last date (non-inclusive) from which we should return results (optional)} } \value{ A dataframe suitable for generating a HIPAA disclosure log @@ -24,6 +27,8 @@ A dataframe suitable for generating a HIPAA disclosure log Read a data needed for a HIPAA disclosure log from a REDCap database given a DBI connection object to the REDCap database and some optional parameters to narrow the returned result. + +Optionally filter the data with the data range `[start_date, end_date)`. } \examples{ \dontrun{ diff --git a/redcapcustodian.Rproj b/redcapcustodian.Rproj index 88ff2b5..f0a99e2 100644 --- a/redcapcustodian.Rproj +++ b/redcapcustodian.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 2cfe0968-c181-4bdc-a53a-43442dbeb736 RestoreWorkspace: Default SaveWorkspace: Default diff --git a/tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R b/tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R index 97fdbce..1e4012d 100644 --- a/tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R +++ b/tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R @@ -26,12 +26,12 @@ testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", { head(n = 30) |> dplyr::collect() |> dplyr::mutate( - ehr_id = sample(1:3, n(), replace = TRUE), - created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = n()) + ehr_id = sample(1:3, dplyr::n(), replace = TRUE), + created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = dplyr::n()) ) # Write the mutated data back to the database - duckdb_register(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs) + duckdb::duckdb_register(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs) # Required column names required_names <- c( @@ -44,15 +44,25 @@ testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", { testthat::expect_gt(nrow(result), 0) testthat::expect_equal( nrow(result), - result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow() + result |> dplyr::distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow() ) result_filtered_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 1) testthat::expect_true(all(result_filtered_ehr_id$ehr_id == 1)) start_date <- Sys.Date() - 5 - result_filtered_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date) - testthat::expect_true(all(result_filtered_date$disclosure_date >= start_date)) + result_filtered_start_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date) + testthat::expect_true(all(result_filtered_start_date$disclosure_date >= start_date)) + + end_date <- Sys.Date() - 5 + result_filtered_end_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, end_date = end_date) + testthat::expect_true(all(result_filtered_end_date$disclosure_date < end_date)) + + start_date <- Sys.Date() - 7 + end_date <- Sys.Date() - 5 + result_filtered_start_and_end_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date, end_date = end_date) + testthat::expect_true(all(result_filtered_start_and_end_date$disclosure_date >= start_date)) + testthat::expect_true(all(result_filtered_start_and_end_date$disclosure_date < end_date)) result_combined_filters <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 2, start_date = start_date) testthat::expect_true(all(result_combined_filters$ehr_id == 2))