---
title: "Administrative Tasks with rdfp"
author: "Steven M. Mortimer"
date: "2018-03-29"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 4
    keep_md: true
vignette: >
  %\VignetteIndexEntry{Administrative Tasks with rdfp}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE}
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN,
  eval = NOT_CRAN
)
```

First, we load `dplyr` and `lubridate` to use in this vignette. Next we load the 
`rdfp` package and specify the DFP Network ID that we would like to connect to.

```{r load-package}
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(lubridate)))
library(rdfp)
options(rdfp.network_code = 123456789)
```

To use the package it is **not** necessary to setup your own application `client_id`
and `client_secret`. By default you will be using the package client. The package 
client does not grant anyone access to your DFP instance it simply provides the 
medium for you to connect to your own data. If you wanted to setup your own Google 
Client to connect to DFP then go to the [Google Developer's Console](https://console.developers.google.com)
and then specify your `client_id` and `client_secret` like so: 

```{r other-params, eval=FALSE}
options(rdfp.application_name = "MyApp")
options(rdfp.client_id = "012345678901-99thisisatest99.apps.googleusercontent.com")
options(rdfp.client_secret = "Th1s1sMyC1ientS3cr3t")

dfp_auth()
```

```{r auth, include = FALSE}
library(here)
token_path <- here::here("tests", "testthat", "rdfp_token.rds")
suppressMessages(dfp_auth(token = token_path, verbose = FALSE))
options_path <- here::here("tests", "testthat", "rdfp_options.rds")
rdfp_options <- readRDS(options_path)
options(rdfp.network_code = rdfp_options$network_code)
```

#### Check Current User Info

```{r}
# Check current user or network
user_info <- dfp_getCurrentUser()
user_info[,c('id', 'isActive')]
network_info <- dfp_getCurrentNetwork()
network_info[,c('id', 'networkCode')]
```

#### Create Team and Users

```{r, eval=FALSE}
# create a team and user and add the user to that team

#first create the team
request_data <- list(teams=list(name="TestTeam1", 
                                description='API Test Team 1', 
                                hasAllCompanies='true', 
                                hasAllInventory='true',
                                teamAccessType='READ_WRITE'))
dfp_createTeams_result <- dfp_createTeams(request_data)

# second create the user
request_data <- data.frame(name = paste0("TestUser", 1:3),
                           email = paste0('testuser', 1:3, '@gmail.com'), 
                           roleId = rep(-1, 3))
dfp_createUsers_result <- dfp_createUsers(request_data)

# third associate the user to that team
request_data <- data.frame(teamid=rep(dfp_createTeams_result$id, 3),
                           userid=dfp_createUsers_result$id)
dfp_createUserTeamAssociations_result <- dfp_createUserTeamAssociations(request_data)

# Note: User roleId = -1 is the Administrative role
# use dfp_getAllRoles() to view other options

# Creating custom roles is a premium feature, which
# small business accounts won't have and creating the roles 
# can only be done from the user interface, not the API.
```

#### Create Companies and Contacts

```{r eval = FALSE}
# create a company and add a contact to it

# first create the company
request_data <- list(companies=list(name="TestCompany1", 
                                    type='HOUSE_ADVERTISER', 
                                    address='123 Main St Hometown, FL USA', 
                                    email='testcompany1@gmail.com', 
                                    comment='API Test'))
dfp_createCompanies_result <- dfp_createCompanies(request_data)

# second create the contact and specify the companyId
request_data <- list(contacts=list(name="TestContact1", 
                                    companyId=dfp_createCompanies_result$id, 
                                    status='UNINVITED', 
                                    cellPhone='(888) 999-7777',
                                    comment='API Test', 
                                    email='testcontact1@gmail.com'))
dfp_createContacts_result <- dfp_createContacts(request_data)
```
