From d91a5283e2c9246760c110332e59513de62cfa07 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 21 May 2017 02:44:05 +0200 Subject: new backend to fetch feedback from a google doc --- lib/awfeedback/index.js | 63 ++++++++++++++++++++++++++++++++ lib/awfeedback/package.json | 15 ++++++++ lib/awfeedback/tokens/client_secret.json | 1 + lib/awfeedback/tokens/client_token.json | 1 + lib/okpush/index.js | 2 + 5 files changed, 82 insertions(+) create mode 100644 lib/awfeedback/index.js create mode 100644 lib/awfeedback/package.json create mode 100644 lib/awfeedback/tokens/client_secret.json create mode 100644 lib/awfeedback/tokens/client_token.json (limited to 'lib') diff --git a/lib/awfeedback/index.js b/lib/awfeedback/index.js new file mode 100644 index 0000000..187661b --- /dev/null +++ b/lib/awfeedback/index.js @@ -0,0 +1,63 @@ +var fs = require('fs') +var google = require('googleapis') +var googleAuth = require('google-auth-library') + +function AWFeedback (options) { if (!(this instanceof AWFeedback)) return new AWFeedback(options) + options = options || {} + if (!options.express) + throw new Error('Express not provided to AWFeedback'); + if (!options.config) + throw new Error('Configuration not provided to AWFeedback'); + + var express = options.express + var router = express.Router() + var config = options.config + var db = options.db + + console.log(config) + + var credentials = JSON.parse( fs.readFileSync(config.credentials) ) + var token = JSON.parse( fs.readFileSync(config.token) ) + + var clientSecret = credentials.installed.client_secret + var clientId = credentials.installed.client_id + var redirectUrl = credentials.installed.redirect_uris[0] + var gauth = new googleAuth() + var auth = new gauth.OAuth2(clientId, clientSecret, redirectUrl) + auth.credentials = token + + var sheets = google.sheets('v4'); + + router.get('/index', function (req, res) { + sheets.spreadsheets.values.get({ + auth: auth, + spreadsheetId: config.spreadsheet, + range: 'Form Responses 1!A2:E', + }, function(err, response) { + if (err) { + res.json([]) + return + } + var rows = response.values; + if (rows.length == 0) { + res.json([]) + } else { + var parsed = rows.filter( row => row[4] === 'x' || row[4] === 'X' ) + .map( row => [ row[0], row[1], row[3] ] ) + res.json(parsed) + } + }); + }) + + router.post('*', function (req, res) { + throw new Error('AWFeedback POST requests not implemented') + }) + + this._router = router +} + +AWFeedback.prototype.middleware = function () { + return this._router +} + +module.exports = AWFeedback diff --git a/lib/awfeedback/package.json b/lib/awfeedback/package.json new file mode 100644 index 0000000..4864e41 --- /dev/null +++ b/lib/awfeedback/package.json @@ -0,0 +1,15 @@ +{ + "name": "awfeedback", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "UNLICENSED", + "dependencies": { + "google-auth-library": "^0.10.0", + "googleapis": "^19.0.0" + } +} diff --git a/lib/awfeedback/tokens/client_secret.json b/lib/awfeedback/tokens/client_secret.json new file mode 100644 index 0000000..4973155 --- /dev/null +++ b/lib/awfeedback/tokens/client_secret.json @@ -0,0 +1 @@ +{"installed":{"client_id":"607110202132-nenuro8k1eto67iorj6gkjncqrc4kfi6.apps.googleusercontent.com","project_id":"aerial-valor-168323","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"8FeJgtC1Cv-FttEZPrSe7KHp","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} \ No newline at end of file diff --git a/lib/awfeedback/tokens/client_token.json b/lib/awfeedback/tokens/client_token.json new file mode 100644 index 0000000..8faae0c --- /dev/null +++ b/lib/awfeedback/tokens/client_token.json @@ -0,0 +1 @@ +{"access_token":"ya29.GltQBM77mgZn34e0222_9njAqHHqj-KemaAmWEkmojLt224L20CYUyxyq5OoiQVGtVXEAAsQQYY95qGQjigQwTjg9pVMi71QAENejf43FjMlb1AucdPGEa390ERT","refresh_token":"1/glG1o8edgir-nUU9ln8E0Fc5MI3zzmlBPDqFHDYfV8Y","token_type":"Bearer","expiry_date":1495328054832} \ No newline at end of file diff --git a/lib/okpush/index.js b/lib/okpush/index.js index 5e6b040..67b7c00 100644 --- a/lib/okpush/index.js +++ b/lib/okpush/index.js @@ -114,6 +114,8 @@ function OKPush (options) { channel: req.body.channel, platform: req.body.platform, }) + .then( () => console.log('added new token') ) + .catch( () => console.log('caught error') ) res.sendStatus(200) }) -- cgit v1.2.3-70-g09d2