# get libraries
if (!require(librarian)){
  install.packages("librarian")
  library(librarian)
}
librarian::shelf(
  clockify, dplyr, DT, glue, lubridate, readr)

# CHANGE THESE VARIABLES!
api_txt <- "~/My Drive (ben@ecoquants.com)/private/clockify-api-key_ben@ecoquants.com.txt"
project_name <- "meds-team-sci"

# use API key to authenticate
CLOCKIFY_API_KEY <- readLines(api_txt)
set_api_key(CLOCKIFY_API_KEY)

# get data
d_projects <- projects() %>% 
      select(project_id, project_name)
d_users <- users() %>% 
      select(user_id, user_name)
d_times <- time_entries(concise = F) %>% 
  left_join(
    d_projects, by = "project_id") %>% 
  left_join(
    d_users, by = "user_id")

# show table
d_times %>% 
  filter(
    project_name == !!project_name) %>% 
  select(
    user_name, project_name, 
    time_start, duration, description) %>% 
  arrange(user_name, project_name, time_start) %>% 
  datatable() %>%
  formatRound(columns=c('duration'), digits=3)