daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //set JSON content type and CORS headers for the response
  2. response.header('Content-Type','application/json');
  3. response.header('Access-Control-Allow-Origin', '*');
  4. response.header('Access-Control-Allow-Headers', '*');
  5.  
  6. //respond to CORS preflight requests
  7. if (request.method == 'OPTIONS') {
  8. response.status(204).send('');
  9. }
  10.  
  11. // pull in firebase
  12. var firebase = require('firebase');
  13.  
  14. require("firebase/firestore");
  15.  
  16. let config = {
  17.   // config stuff here
  18. }
  19.  
  20. if (!firebase.apps.length) {
  21. firebase.initializeApp(config);
  22. }
  23.  
  24. // grab doc id from request
  25. var id = request.query.docId;
  26.  
  27. // declare connection to firestore
  28. var db = firebase.firestore();
  29.  
  30. // grab user from request
  31. var docRef = db.collection("clients").doc(id);
  32.  
  33. // grab the users server and id
  34. docRef.get().then(function(doc) {
  35.  
  36. // grab the account id
  37. var accountId = doc.data().accountId;
  38.  
  39. // declare master variable that will be returned
  40. var toReturn = [];
  41.  
  42. // declare variables that will be returned in toReturn
  43. var gpmGames = [];
  44. var cspmGames = [];
  45. var damageGames = [];
  46. var damageToChampionsGames = [];
  47. var damageTakenGames = [];
  48. var wardsGames = [];
  49.  
  50. db.collection('games')
  51.   .where('accountId', '==', accountId)
  52.   .get()
  53.   .then((res) => {
  54.     var games = res.docs;
  55.     // iterate through games and get averages and totals to return
  56.     games.forEach(function(game) {
  57.  
  58.       gpmGames.push(game.data().gpm);
  59.       cspmGames.push(game.data().cspm);
  60.       damageGames.push(game.data().damage);
  61.       damageToChampionsGames.push(game.data().damagetochampions);
  62.       damageTakenGames.push(game.data().damagerecieved);
  63.       wardsGames.push(game.data().wards);
  64.  
  65.     });
  66.   });
  67.  
  68.   toReturn['gpmGames'] = gpmGames;
  69.   toReturn['cspmGames'] = cspmGames;
  70.   toReturn['damageGames'] = damageGames;
  71.   toReturn['damageToChampionsGames'] = damageToChampionsGames;
  72.   toReturn['damageTakenGames'] = damageTakenGames;
  73.   toReturn['wardsGames'] = wardsGames;
  74.  
  75. response.status(200).send(toReturn);
  76. });
RAW Paste Data
Top