Files
AVSDevR.MarineNoiseRegistry/R/db_organisations.R
2026-02-10 17:54:17 +00:00

219 lines
8.3 KiB
R

#' MNR database queries for Organsiations
#'
#' @export
# nolint next: object_name_linter. R6Class
MNR.DB.Organisations <- R6::R6Class(
"MNR.DB.Organisations",
inherit = MNR.DB,
public = list(
#' @description Fetch the details of an organisation from the database
#' @param org_id <integer> The id of the organisation to fetch
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns A query object for an organsation (collect = FALSE) or a
#' tibble with the organisation details
getOrganisation = function(org_id, collect = TRUE) {
private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(id == !!org_id) %>%
private$db_client$collectOrReturn()
},
#' @description Fetch the details of all organisations from the database
#' @param as_int <logical> Return only the ids (TRUE)
#' @param name_only <logical> Return only the name (TRUE)
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns The organisation ids if as_int is TRUE, the organisation names
#' if as_int is FALSE and name_only is TRUE, else the organisations or an
#' uncollected query object
getAllOrganisations = function(
as_int = FALSE, name_only = TRUE, collect = TRUE
) {
qry <- private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE)
if (as_int) {
qry %>%
dplyr::pull(id)
} else if (name_only) {
qry %>%
dplyr::pull(name)
} else {
qry %>%
private$db_client$collectOrReturn()
}
},
#' @description Check if an organisation is a regulator
#' @param org_id <integer> The id of the organisation to check
#' @returns TRUE if the organisation is a regulator
isRegulator = function(org_id) {
private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(flag_regulator == TRUE) %>%
dplyr::filter(id == !!org_id) %>%
dplyr::count() %>%
dplyr::pull() %>%
magrittr::is_greater_than(0)
},
#' @description Fetch the details of all the regulators from the database
#' @param as_int <logical> Return only the ids (TRUE)
#' @param name_only <logical> Return only the name (TRUE)
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns The regulator ids if as_int is TRUE, the regulator names if
#' as_int is FALSE and name_only is TRUE, else the regulator or an
#' uncollected query object
getRegulators = function(as_int = FALSE, name_only = TRUE, collect = TRUE) {
qry <- private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(flag_regulator == TRUE)
if (as_int) {
qry %>%
dplyr::pull(id)
} else if (name_only) {
qry %>%
dplyr::pull(name)
} else {
qry %>%
private$db_client$collectOrReturn()
}
},
#' @description Fetch the members of an organisation
#' @param org_name <character> The name of the organisation to check
#' @param org_id <integer> The id of the organisation to check
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns A tibble of the organisation members or an uncollected query
getOrganisationMembers = function(
org_name = "", org_id = -1, collect = TRUE
) {
private$db_client$table("users") %>%
dplyr::filter(flag_enabled == TRUE) %>%
dplyr::filter(flag_verified == TRUE) %>%
dplyr::mutate(known_as = paste(first_name, last_name)) %>%
dplyr::left_join(
private$db_client$table("organisation_members") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::select(user_id, organisation_id),
by = c(id = "user_id")
) %>%
dplyr::inner_join(
private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter((id == !!org_id) | (name == !!org_name)) %>%
dplyr::select(id),
by = c(organisation_id = "id")
) %>%
dplyr::select(-organisation_id) %>%
private$db_client$collectOrReturn()
},
#' @description Check if user's organisation is an agent for another
#' organisation
#' @param org_id <integer> The id of the organisation to check
#' @returns TRUE if the user's organisation is an agent
isAgent = function(org_id) {
private$db_client$table("organisation_agents") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(agent_id == !!org_id) %>%
dplyr::count() %>%
dplyr::pull() %>%
magrittr::is_greater_than(0)
},
#' @description Fetch the agents of an organisation
#' @param org_id <integer> The id of the organisation to check
#' @param as_int <logical> Return only the id (TRUE)
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns An uncollected query object or a tibble of agents
getOrganisationAgents = function(org_id, as_int = FALSE, collect = TRUE) {
qry <- private$db_client$table("organisation_agents") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(organisation_id == !!org_id) %>%
dplyr::select(id = agent_id) %>%
dplyr::distinct() %>%
dplyr::left_join(
private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE),
by = "id"
)
if (as_int) {
qry %>%
dplyr::pull(id)
} else {
qry %>%
private$db_client$collectOrReturn()
}
},
#' @description Fetch the recipients of an organisation
#' @param agent_id <integer> The id of the organisation (agent) to check
#' @param as_int <logical> Return only the id (TRUE)
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns An uncollected query object or a tibble of recipients
getAgentRecipients = function(agent_id, as_int = FALSE, collect = TRUE) {
qry <- private$db_client$table("organisation_agents") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::filter(agent_id == !!agent_id) %>%
dplyr::select(id = organisation_id) %>%
dplyr::distinct() %>%
dplyr::left_join(
private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE),
by = "id"
)
if (as_int) {
qry %>%
dplyr::pull(id)
} else {
qry %>%
private$db_client$collectOrReturn()
}
},
#' @description Fetch the organisation of a user
#' @param user_id <integer> The id of the user to fetch the organisation of
#' @param as_int <logical> Return only the id (TRUE)
#' @param name_only <logical> Return only the name (TRUE)
#' @param collect <logical> Collect the query before returning (TRUE) or
#' return the built query object (FALSE)
#' @returns The organisation id if as_int is TRUE, the organisation name if
#' as_int is FALSE and name_only is TRUE, else the organisation or an
#' uncollected query object
getUserOrganisation = function(
user_id, as_int = TRUE, name_only = TRUE, collect = TRUE
) {
qry <- private$db_client$table("organisations") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::left_join(
private$db_client$table("organisation_members") %>%
dplyr::filter(flag_approved == TRUE) %>%
dplyr::select(user_id, organisation_id),
by = c(id = "organisation_id")
) %>%
dplyr::filter(user_id == !!user_id) %>%
dplyr::slice_min(user_id, n = 1)
if (as_int) {
qry %>%
dplyr::pull(id)
} else if (name_only) {
qry %>%
dplyr::pull(name)
} else {
qry %>%
private$db_client$collectOrReturn()
}
}
)
)