Skip to content

Commit

Permalink
Merge pull request #176 from pbchase/add_end_date_param
Browse files Browse the repository at this point in the history
Update get_hipaa_disclosure_log_from_ehr_fhir_logs()
  • Loading branch information
saipavan10-git authored Feb 14, 2025
2 parents 2fcf36a + 8583617 commit 8c817b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
7 changes: 6 additions & 1 deletion R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")) |>
Expand Down
7 changes: 6 additions & 1 deletion man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions redcapcustodian.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 2cfe0968-c181-4bdc-a53a-43442dbeb736

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
22 changes: 16 additions & 6 deletions tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
Expand Down

0 comments on commit 8c817b0

Please sign in to comment.