Skip to content

Commit 8c817b0

Browse files
Merge pull request #176 from pbchase/add_end_date_param
Update get_hipaa_disclosure_log_from_ehr_fhir_logs()
2 parents 2fcf36a + 8583617 commit 8c817b0

4 files changed

+29
-8
lines changed

R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#' given a DBI connection object to the REDCap database and some optional
55
#' parameters to narrow the returned result.
66
#'
7+
#' Optionally filter the data with the data range `[start_date, end_date)`.
8+
#'
79
#' @param conn a DBI connection object to the REDCap database
810
#' @param ehr_id a vector of REDCap EHR_IDs for the EHR(s) of interest (optional)
911
#' @param start_date The first date from which we should return results (optional)
12+
#' @param end_date The last date (non-inclusive) from which we should return results (optional)
1013
#'
1114
#' @return A dataframe suitable for generating a HIPAA disclosure log
1215
#' @export
@@ -29,7 +32,8 @@
2932
get_hipaa_disclosure_log_from_ehr_fhir_logs <- function(
3033
conn,
3134
ehr_id = NA_real_,
32-
start_date = as.Date(NA)) {
35+
start_date = as.Date(NA),
36+
end_date = as.Date(NA)) {
3337

3438
# rename parameters for local use
3539
ehr_id_local <- ehr_id
@@ -68,6 +72,7 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs <- function(
6872
dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
6973
dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |>
7074
dplyr::filter(is.na(start_date) | .data$created_at >= start_date) |>
75+
dplyr::filter(is.na(end_date) | .data$created_at < end_date) |>
7176
dplyr::filter(ehr_id_is_na | .data$ehr_id %in% ehr_id_local) |>
7277
dplyr::left_join(user_information, by = c("user_id" = "ui_id")) |>
7378
dplyr::left_join(projects, by = c("project_id")) |>

man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

redcapcustodian.Rproj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 2cfe0968-c181-4bdc-a53a-43442dbeb736
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R

+16-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
2626
head(n = 30) |>
2727
dplyr::collect() |>
2828
dplyr::mutate(
29-
ehr_id = sample(1:3, n(), replace = TRUE),
30-
created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = n())
29+
ehr_id = sample(1:3, dplyr::n(), replace = TRUE),
30+
created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = dplyr::n())
3131
)
3232

3333
# Write the mutated data back to the database
34-
duckdb_register(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs)
34+
duckdb::duckdb_register(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs)
3535

3636
# Required column names
3737
required_names <- c(
@@ -44,15 +44,25 @@ testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
4444
testthat::expect_gt(nrow(result), 0)
4545
testthat::expect_equal(
4646
nrow(result),
47-
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow()
47+
result |> dplyr::distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow()
4848
)
4949

5050
result_filtered_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 1)
5151
testthat::expect_true(all(result_filtered_ehr_id$ehr_id == 1))
5252

5353
start_date <- Sys.Date() - 5
54-
result_filtered_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date)
55-
testthat::expect_true(all(result_filtered_date$disclosure_date >= start_date))
54+
result_filtered_start_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date)
55+
testthat::expect_true(all(result_filtered_start_date$disclosure_date >= start_date))
56+
57+
end_date <- Sys.Date() - 5
58+
result_filtered_end_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, end_date = end_date)
59+
testthat::expect_true(all(result_filtered_end_date$disclosure_date < end_date))
60+
61+
start_date <- Sys.Date() - 7
62+
end_date <- Sys.Date() - 5
63+
result_filtered_start_and_end_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date, end_date = end_date)
64+
testthat::expect_true(all(result_filtered_start_and_end_date$disclosure_date >= start_date))
65+
testthat::expect_true(all(result_filtered_start_and_end_date$disclosure_date < end_date))
5666

5767
result_combined_filters <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 2, start_date = start_date)
5868
testthat::expect_true(all(result_combined_filters$ehr_id == 2))

0 commit comments

Comments
 (0)