Skip to content

Commit 840670d

Browse files
Merge pull request #158 from pbchase/add_get_hipaa_disclosure_log_from_ehr_fhir_logs
Add get_hipaa_disclosure_log_from_ehr_fhir_logs()
2 parents 28e8bfa + 98ff8d6 commit 840670d

9 files changed

+213
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export(export_allocation_tables_from_project)
2020
export(get_bad_emails_from_individual_emails)
2121
export(get_bad_emails_from_listserv_digest)
2222
export(get_current_time)
23+
export(get_hipaa_disclosure_log_from_ehr_fhir_logs)
2324
export(get_institutional_person_data)
2425
export(get_job_duration)
2526
export(get_package_scope_var)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#' get_hipaa_disclosure_log_from_ehr_fhir_logs
2+
#' @description
3+
#' Read a data needed for a HIPAA disclosure log from a REDCap database
4+
#' given a DBI connection object to the REDCap database and some optional
5+
#' parameters to narrow the returned result.
6+
#'
7+
#' @param conn a DBI connection object to the REDCap database
8+
#' @param ehr_id the REDCap EHR_ID for the EHR of interest (optional)
9+
#' @param start_date The first date from which we should return results (optional)
10+
#'
11+
#' @return A dataframe suitable for generating a HIPAA disclosure log
12+
#' @export
13+
#'
14+
#' @examples
15+
#' \dontrun{
16+
#' library(tidyverse)
17+
#' library(lubridate)
18+
#' library(REDCapR)
19+
#' library(dotenv)
20+
#' library(redcapcustodian)
21+
#' library(DBI)
22+
#' library(RMariaDB)
23+
#'
24+
#' init_etl("export_fhir_traffic_log")
25+
#' conn <- connect_to_redcap_db()
26+
#'
27+
#' get_hipaa_disclosure_log_from_ehr_fhir_logs(conn)
28+
#' }
29+
get_hipaa_disclosure_log_from_ehr_fhir_logs <- function(
30+
conn,
31+
ehr_id = NA_real_,
32+
start_date = as.Date(NA)) {
33+
# make DBI objects for joins
34+
user_information <- dplyr::tbl(conn, "redcap_user_information") |>
35+
dplyr::select(
36+
"ui_id",
37+
"username"
38+
)
39+
40+
projects <- dplyr::tbl(conn, "redcap_projects") |>
41+
dplyr::select(
42+
"project_id",
43+
"app_title",
44+
"project_pi_firstname",
45+
"project_pi_mi",
46+
"project_pi_lastname",
47+
"project_pi_email",
48+
"project_pi_alias",
49+
"project_irb_number"
50+
)
51+
52+
disclosures <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
53+
dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |>
54+
dplyr::left_join(user_information, by = c("user_id" = "ui_id")) |>
55+
dplyr::left_join(projects, by = c("project_id")) |>
56+
dplyr::collect() |>
57+
dplyr::mutate(disclosure_date = lubridate::floor_date(.data$created_at, unit = "day")) |>
58+
dplyr::select(-c("id", "created_at")) |>
59+
dplyr::distinct() |>
60+
dplyr::arrange(.data$disclosure_date) |>
61+
dplyr::rename(redcap_project_name = "app_title") |>
62+
dplyr::select(
63+
"disclosure_date",
64+
"fhir_id",
65+
"mrn",
66+
"project_irb_number",
67+
"project_pi_firstname",
68+
"project_pi_mi",
69+
"project_pi_lastname",
70+
"project_pi_email",
71+
"redcap_project_name",
72+
"username",
73+
dplyr::everything()
74+
)
75+
76+
return(disclosures)
77+
}

man/get_hipaa_disclosure_log_from_ehr_fhir_logs.Rd

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

tests/testthat/helper.R

+5
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ if (is.null(salt)) {
3232
set_package_scope_var("salt", paste0(runif(1), runif(1), runif(1)))
3333
salt <- get_package_scope_var("salt")
3434
}
35+
36+
# write a dataframe, referenced by 'table_name' to tests/testthat/directory_under_test_path
37+
write_rds_to_test_dir <- function(table_name, directory_under_test_path) {
38+
get(table_name) |> saveRDS(testthat::test_path(directory_under_test_path, paste0(table_name, ".rds")))
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
library(tidyverse)
2+
library(lubridate)
3+
library(REDCapR)
4+
library(dotenv)
5+
library(redcapcustodian)
6+
library(DBI)
7+
library(RMariaDB)
8+
9+
dotenv::load_dot_env("prod.env")
10+
conn <- connect_to_redcap_db()
11+
12+
project_ids_of_interest <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
13+
dplyr::filter(.data$resource_type == "Patient") |>
14+
dplyr::distinct(project_id) |>
15+
dplyr::collect() |>
16+
sample_n(size = 1) |>
17+
pull(project_id)
18+
19+
redcap_ehr_fhir_logs <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
20+
dplyr::filter(.data$resource_type == "Patient" &
21+
.data$mrn != "" &
22+
project_id == project_ids_of_interest) |>
23+
dplyr::collect()
24+
25+
redcap_ui_ids_of_interest <- redcap_ehr_fhir_logs |>
26+
dplyr::distinct(user_id) |>
27+
dplyr::collect() |>
28+
dplyr::pull(user_id)
29+
30+
redcap_user_information <- dplyr::tbl(conn, "redcap_user_information") |>
31+
dplyr::filter(ui_id %in% redcap_ui_ids_of_interest) |>
32+
dplyr::select(
33+
"ui_id",
34+
"username"
35+
) |>
36+
dplyr::collect()
37+
38+
redcap_projects <- dplyr::tbl(conn, "redcap_projects") |>
39+
dplyr::filter(project_id %in% project_ids_of_interest) |>
40+
dplyr::select(
41+
"project_id",
42+
"app_title",
43+
"project_pi_firstname",
44+
"project_pi_mi",
45+
"project_pi_lastname",
46+
"project_pi_email",
47+
"project_pi_alias",
48+
"project_irb_number"
49+
) |>
50+
collect()
51+
52+
# Save our test tables
53+
test_tables <- c(
54+
"redcap_ehr_fhir_logs",
55+
"redcap_user_information",
56+
"redcap_projects"
57+
)
58+
purrr::walk(test_tables, write_rds_to_test_dir, "hipaa_disclosure_log")
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
2+
# read our test data
3+
directory_under_test_path <- "hipaa_disclosure_log"
4+
test_tables <- c(
5+
"redcap_ehr_fhir_logs",
6+
"redcap_user_information",
7+
"redcap_projects"
8+
)
9+
10+
conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:")
11+
purrr::walk(test_tables, create_a_table_from_rds_test_data, conn, "hipaa_disclosure_log")
12+
13+
required_names <- c(
14+
"disclosure_date", "fhir_id", "mrn", "project_irb_number"
15+
)
16+
17+
result <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn)
18+
19+
# test for the required columns
20+
testthat::expect_contains(names(result), required_names)
21+
# test for at least one row
22+
testthat::expect_gt(nrow(result), 0)
23+
# test for only distinct rows
24+
testthat::expect_equal(
25+
nrow(result),
26+
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow())
27+
28+
DBI::dbDisconnect(conn, shutdown=TRUE)
29+
})

0 commit comments

Comments
 (0)