'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const express = require('express');
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// CORS Express middleware to enable CORS Requests.
const cors = require('cors');
app.use(cors({ origin: true }))
app.post('/submitResponse', (req, res) => {
console.log('/submitResponse');
if (!req.body.info)
res.status(422).send()
const payload = req.body.info;
console.log(payload);
const responses = db.collection("responses")
responses.add({
payload: payload,
timestamp: admin.firestore.FieldValue.serverTimestamp()
}).then(function(docRef) {
console.log("Response written with ID: ", docRef.id);
res.status(200).send(JSON.stringify(docRef.id))
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
});
exports.app = functions.https.onRequest(app);