R/checkOrgunitsInHierarchy.R

Defines functions checkOrgunitsInHierarchy

Documented in checkOrgunitsInHierarchy

#' Title
#'
#' @param d datimvalidation object obtained from d2Parser.
#' @param userOrgUnit UID of the operating unit which the user belongs to, or wishes to assume
#' @param d2session datimutils d2session object
#'
#' @return datimvalidation object
#' @export
#'
checkOrgunitsInHierarchy <-
  function(d,
           userOrgUnit,
           d2session = dynGet("d2_default_session", inherits = TRUE)) {

    if (is.null(d)) {
      stop("Data cannot be null")
    }

    if (is.null(userOrgUnit)) {
      stop("You must specify and organisation unit")
    }

    data_orgunits <- unique(d$data$import$orgUnit)

    user_orgunits <- getOrganisationUnitMap(organisationUnit = userOrgUnit, d2session = d2session)

    invalid_orgunits <- data_orgunits[!(data_orgunits %in% user_orgunits$id)]

    if (length(invalid_orgunits) > 0) {

      msg <- "ERROR! Organisation units detected which are not in the provided operating unit."
      d$tests$orgunits_not_in_hierarchy <- invalid_orgunits
      d$info$messages <- appendMessage(d$info$messages, msg, "ERROR")

    } else {
      msg <- "All organisation units were within the provided operating unit.."
      d$info$messages <- appendMessage(d$info$messages, msg, "INFO")
    }

    d
    }
jason-p-pickering/datim-validation documentation built on April 20, 2023, 5:32 a.m.