117 lines
4.3 KiB
JavaScript
117 lines
4.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getPastResult = exports.saveResult = void 0;
|
|
const functions = require("firebase-functions");
|
|
const admin = require("firebase-admin");
|
|
const firebaseSecret = require("./firebaseSecret.json");
|
|
const params = {
|
|
type: firebaseSecret.type,
|
|
projectId: firebaseSecret.project_id,
|
|
privateKeyId: firebaseSecret.private_key_id,
|
|
privateKey: firebaseSecret.private_key,
|
|
clientEmail: firebaseSecret.client_email,
|
|
clientId: firebaseSecret.client_id,
|
|
authUri: firebaseSecret.auth_uri,
|
|
tokenUri: firebaseSecret.token_uri,
|
|
authProviderX509CertUrl: firebaseSecret.auth_provider_x509_cert_url,
|
|
clientC509CertUrl: firebaseSecret.client_x509_cert_url,
|
|
};
|
|
admin.initializeApp({
|
|
credential: admin.credential.cert(params),
|
|
});
|
|
const serializeGaId = (_gaid) => {
|
|
if (_gaid) {
|
|
return _gaid.split(".").join("|");
|
|
}
|
|
return "";
|
|
};
|
|
exports.saveResult = functions.https.onCall(async (data, context) => {
|
|
if (!!data && !!context) {
|
|
const { gid } = data;
|
|
const docId = serializeGaId(gid);
|
|
const docData = Object.assign({}, data);
|
|
delete docData.gid;
|
|
const existingData = await admin
|
|
.firestore()
|
|
.collection("results")
|
|
.doc(docId)
|
|
.get()
|
|
.then((doc) => doc.data());
|
|
delete docData.age;
|
|
delete docData.name;
|
|
delete docData.dose;
|
|
if (existingData !== undefined && existingData !== docData) {
|
|
// DATA EXISTS FOR USER
|
|
// BUT DIFF DATA SENT FOR UPDATE
|
|
await admin.firestore().collection("results").doc(docId).set(docData);
|
|
const updateDoc = {};
|
|
Object.keys(docData).forEach((symptom) => {
|
|
// IF WAS TRUE, NOW FALSE = -1
|
|
// IF WAS TRUE, NOW TRUE = 0
|
|
// IF WAS FALSE, NOW TRUE = +1
|
|
// IF WAS FALSE, NOW FALSE = 0
|
|
let incrementValue = 0;
|
|
if (existingData[symptom] === true && docData[symptom] === false) {
|
|
incrementValue = -1;
|
|
}
|
|
else if (existingData[symptom] === false &&
|
|
docData[symptom] === true) {
|
|
incrementValue = 1;
|
|
}
|
|
if (data.dose) {
|
|
updateDoc[`${data.dose}.${data.age}.${symptom}`] =
|
|
admin.firestore.FieldValue.increment(incrementValue);
|
|
}
|
|
else {
|
|
updateDoc[`${data.age}.${symptom}`] =
|
|
admin.firestore.FieldValue.increment(incrementValue);
|
|
}
|
|
});
|
|
admin
|
|
.firestore()
|
|
.collection("stats")
|
|
.doc(data.name)
|
|
.update(updateDoc);
|
|
}
|
|
else if (existingData === undefined) {
|
|
// DATA DOESN'T EXIST FOR USER
|
|
// SAVE USER DATA
|
|
await admin.firestore().collection("results").doc(docId).set(docData);
|
|
// INCREMENT TOTAL RESPONSE COUNT
|
|
const updateDoc = {
|
|
count: admin.firestore.FieldValue.increment(1),
|
|
};
|
|
// INCREMENT VALUES FOR EACH SYMPTOM
|
|
Object.keys(docData).forEach((symptom) => {
|
|
if (docData[symptom] === true) {
|
|
if (data.dose) {
|
|
updateDoc[`${data.dose}.${data.age}.${symptom}`] =
|
|
admin.firestore.FieldValue.increment(1);
|
|
}
|
|
else {
|
|
updateDoc[`${data.age}.${symptom}`] =
|
|
admin.firestore.FieldValue.increment(1);
|
|
}
|
|
}
|
|
});
|
|
admin
|
|
.firestore()
|
|
.collection("stats")
|
|
.doc(data.name)
|
|
.update(updateDoc);
|
|
}
|
|
}
|
|
});
|
|
exports.getPastResult = functions.https.onCall(async (data, context) => {
|
|
if (!!data && !!context) {
|
|
const { gid } = data;
|
|
return await admin
|
|
.firestore()
|
|
.collection("results")
|
|
.doc(serializeGaId(gid))
|
|
.get()
|
|
.then((doc) => doc.data());
|
|
}
|
|
return undefined;
|
|
});
|
|
//# sourceMappingURL=index.js.map
|