{"size": 2394, "ext": "js", "max_stars_repo_path": "server/Google.js", "max_stars_repo_name": "loftwah/sheetmetal.io", "max_stars_repo_stars_event_min_datetime": "2020-01-28T19:19:40.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-30T10:02:27.000Z", "max_issues_repo_path": "server/Google.js", "max_issues_repo_name": "loftwah/sheetmetal.io", "max_issues_repo_issues_event_min_datetime": "2020-07-17T09:47:41.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-09T01:02:29.000Z", "max_forks_repo_path": "server/Google.js", "max_forks_repo_name": "loftwah/sheetmetal.io", "max_forks_repo_forks_event_min_datetime": "2020-04-03T06:46:57.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-03T06:46:57.000Z"}
{"max_stars_count": 3, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 30.3037974684, "max_line_length": 95, "alphanum_fraction": 0.6846282373}
const Database = require('../db/database')
const { google } = require('googleapis')

const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET
const OAUTH_REDIRECT_URL = process.env.OAUTH_REDIRECT_URL

/**
 * Probably the most annoying part of the whole google library is figuring out refreshed tokens
 * https://github.com/googleapis/google-api-nodejs-client/pull/235 and
 * https://github.com/googleapis/google-api-nodejs-client/issues/783
 * helped a lot
 */

/**
 * Google may refresh the token upon returning a result
 * Save the token so that it's faster next time (and we don't hit account limits on refreshes)
 */
exports.handlePotentiallyNewOauth = (oldOauth, googleLib, userId) => {
  try {
    let responseToken = googleLib['_options'].auth.credentials
    if (oldOauth.access_token !== responseToken.access_token) {
      console.log('newToken needs refreshing', responseToken)
      Database.updateOathForUser(userId, responseToken)
    }
  } catch (error) {
    // fail silently
    console.log('error', error)
  }
}

exports.authorisedClient = googleAuth => {
  const oauth2Client = new google.auth.OAuth2(
    GOOGLE_CLIENT_ID,
    GOOGLE_CLIENT_SECRET,
    OAUTH_REDIRECT_URL
  )
  oauth2Client.setCredentials({ ...googleAuth })
  return google.sheets({ version: 'v4', auth: oauth2Client })
}

exports.handleGoogleError = (error, req, res) => {
  let err = error.response ? error.response.data : error
  console.log('error', err)
  return res.status(500).send(err)
}

exports.getAuthFromHeaders = async (headers, query, userId) => {
  try {
    const queryUserId = query ? query.user : null
    const headersUserId = headers['user'] || null
    if (queryUserId || headersUserId) {
      let userId = headersUserId || queryUserId
      let user = (await Database.getUser(userId)) || null
      return user && user['oauth_token'] ? user['oauth_token'] : null
    } else {
      console.log('no auth')
      return null
    }
  } catch (error) {
    console.log('error', error)
    return null
  }
}

exports.valuesToJson = arrayValues => {
  let headers = arrayValues[0]
  let values = arrayValues.slice(1)
  return values.map(val => {
    // turn the array into an object
    let o = {}
    for (var i = 0; i < headers.length; i++) {
      let header = headers[i]
      o[`${header}`] = val[i]
    }
    return o
  })
}
