v1.1.0/number values and efficacy
This commit is contained in:
+138
-132
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getPastResult = exports.saveResult = void 0;
|
||||
exports.getPastResult = exports.saveResult = exports.getStats = void 0;
|
||||
const functions = require("firebase-functions");
|
||||
const admin = require("firebase-admin");
|
||||
const firebaseSecret = require("./firebaseSecret.json");
|
||||
@@ -19,152 +19,120 @@ const params = {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(params),
|
||||
});
|
||||
const VAX_NAMES = {
|
||||
PB: "Pfizer-BioNTech",
|
||||
MD: "Moderna",
|
||||
JJ: "J&J Janssen",
|
||||
};
|
||||
const VAX_DOSES = {
|
||||
ONE: "Dose 1",
|
||||
TWO: "Dose 2",
|
||||
};
|
||||
const serializeGaId = (_gaid) => {
|
||||
if (_gaid) {
|
||||
return _gaid.split(".").join("|");
|
||||
}
|
||||
return "";
|
||||
};
|
||||
const addResultToStats = async (data) => {
|
||||
const cleanData = Object.assign({}, data);
|
||||
delete cleanData.name;
|
||||
delete cleanData.dose;
|
||||
delete cleanData.age;
|
||||
delete cleanData.gid;
|
||||
const updateDoc = {
|
||||
count: admin.firestore.FieldValue.increment(1),
|
||||
};
|
||||
if (data.name === VAX_NAMES.PB || data.name === VAX_NAMES.MD) {
|
||||
Object.keys(VAX_DOSES).forEach((doseKey) => {
|
||||
const dose = VAX_DOSES[doseKey];
|
||||
Object.keys(cleanData[dose]).forEach((symptom) => {
|
||||
if (cleanData[dose][symptom]) {
|
||||
updateDoc[`${dose}.${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
Object.keys(cleanData).forEach((symptom) => {
|
||||
updateDoc[`${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
});
|
||||
}
|
||||
admin.firestore().collection("stats").doc(data.name).update(updateDoc);
|
||||
};
|
||||
const removeResultFromStats = async (data) => {
|
||||
const cleanData = Object.assign({}, data);
|
||||
delete cleanData.name;
|
||||
delete cleanData.dose;
|
||||
delete cleanData.age;
|
||||
delete cleanData.gid;
|
||||
const updateDoc = {
|
||||
count: admin.firestore.FieldValue.increment(-1),
|
||||
};
|
||||
if (data.name === VAX_NAMES.PB || data.name === VAX_NAMES.MD) {
|
||||
Object.keys(VAX_DOSES).forEach((doseKey) => {
|
||||
const dose = VAX_DOSES[doseKey];
|
||||
Object.keys(cleanData[dose]).forEach((symptom) => {
|
||||
if (cleanData[dose][symptom]) {
|
||||
updateDoc[`${dose}.${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(-1);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
Object.keys(cleanData).forEach((symptom) => {
|
||||
updateDoc[`${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(-1);
|
||||
});
|
||||
}
|
||||
admin.firestore().collection("stats").doc(data.name).update(updateDoc);
|
||||
};
|
||||
exports.getStats = functions.https.onCall(async () => {
|
||||
const stats = await admin.firestore().collection("stats").get();
|
||||
const docs = stats.docs
|
||||
.map((docSnap) => ({ name: docSnap.id, result: docSnap.data() }));
|
||||
const returnData = {};
|
||||
docs.forEach((vaxResult) => {
|
||||
returnData[vaxResult.name] = {};
|
||||
returnData[vaxResult.name] = vaxResult.result;
|
||||
});
|
||||
return returnData;
|
||||
});
|
||||
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
|
||||
// Get existing result
|
||||
const existingResult = await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
.doc(docId)
|
||||
.doc(serializeGaId(gid))
|
||||
.get()
|
||||
.then((doc) => doc.data());
|
||||
if (existingData !== undefined && existingData !== docData &&
|
||||
existingData.name === docData.name) {
|
||||
// DATA EXISTS FOR USER
|
||||
// BUT DIFF DATA SENT FOR UPDATE
|
||||
await admin.firestore().collection("results").doc(docId).set(docData);
|
||||
delete docData.age;
|
||||
delete docData.name;
|
||||
delete docData.dose;
|
||||
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.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(incrementValue);
|
||||
}
|
||||
else {
|
||||
updateDoc[`${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(incrementValue);
|
||||
}
|
||||
});
|
||||
admin
|
||||
.firestore()
|
||||
.collection("stats")
|
||||
.doc(data.name)
|
||||
.update(updateDoc);
|
||||
.then((docSnap) => docSnap.data());
|
||||
// Update user result
|
||||
const cleanData = Object.assign({}, data);
|
||||
delete cleanData.gid;
|
||||
await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
.doc(serializeGaId(gid))
|
||||
.set(cleanData);
|
||||
// No existing result
|
||||
if (existingResult === undefined) {
|
||||
await addResultToStats(data);
|
||||
}
|
||||
else if (existingData !== undefined && existingData !== docData &&
|
||||
existingData.name !== docData.name) {
|
||||
// UNDO DATA FOR OLD VAX
|
||||
// DECREMENT VALUES FOR EACH SYMPTOM ON OLD VAX
|
||||
const undoDoc = {
|
||||
count: admin.firestore.FieldValue.increment(-1),
|
||||
};
|
||||
Object.keys(existingData).forEach((symptom) => {
|
||||
if (symptom !== "name" && symptom !== "dose" &&
|
||||
symptom !== "age") {
|
||||
if (existingData[symptom] === true) {
|
||||
if (existingData.dose) {
|
||||
undoDoc[`${existingData.dose}.${existingData.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(-1);
|
||||
}
|
||||
else {
|
||||
undoDoc[`${existingData.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
admin
|
||||
.firestore()
|
||||
.collection("stats")
|
||||
.doc(existingData.name)
|
||||
.update(undoDoc);
|
||||
// PROCESS NEW DATA FOR USE
|
||||
// SAVE USER DATA
|
||||
await admin.firestore().collection("results").doc(docId).set(docData);
|
||||
delete docData.age;
|
||||
delete docData.name;
|
||||
delete docData.dose;
|
||||
// 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.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
else {
|
||||
updateDoc[`${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
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);
|
||||
delete docData.age;
|
||||
delete docData.name;
|
||||
delete docData.dose;
|
||||
// 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.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
else {
|
||||
updateDoc[`${data.age}.${symptom.replace("/", "OR")}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
admin
|
||||
.firestore()
|
||||
.collection("stats")
|
||||
.doc(data.name)
|
||||
.update(updateDoc);
|
||||
else {
|
||||
await removeResultFromStats(existingResult);
|
||||
await addResultToStats(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
exports.getPastResult = functions.https.onCall(async (data, context) => {
|
||||
if (!!data && !!context) {
|
||||
const { gid } = data;
|
||||
let hasToUpdateDb = false;
|
||||
const existingData = await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
@@ -172,11 +140,49 @@ exports.getPastResult = functions.https.onCall(async (data, context) => {
|
||||
.get()
|
||||
.then((doc) => doc.data());
|
||||
if (existingData) {
|
||||
if (existingData["NAUSEA / VOMITING"]) {
|
||||
existingData["NAUSEA OR VOMITING"] =
|
||||
existingData["NAUSEA / VOMITING"];
|
||||
const returnData = Object.assign({}, existingData);
|
||||
if (existingData.name === VAX_NAMES.PB ||
|
||||
existingData.name === VAX_NAMES.MD) {
|
||||
Object.keys(VAX_DOSES).forEach((doseKey) => {
|
||||
const dose = VAX_DOSES[doseKey];
|
||||
if (!existingData[dose]) {
|
||||
hasToUpdateDb = true;
|
||||
const temp = {};
|
||||
Object.keys(existingData).forEach((symptom) => {
|
||||
if (symptom !== "name" &&
|
||||
symptom !== "age" &&
|
||||
symptom !== "dose") {
|
||||
if (existingData.dose === dose) {
|
||||
temp[symptom] = existingData[symptom];
|
||||
delete returnData[symptom];
|
||||
}
|
||||
else {
|
||||
temp[symptom] = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
returnData[dose] = temp;
|
||||
}
|
||||
if (returnData[dose]["NAUSEA / VOMITING"]) {
|
||||
returnData["NAUSEA OR VOMITING"] =
|
||||
returnData["NAUSEA / VOMITING"];
|
||||
}
|
||||
});
|
||||
}
|
||||
return existingData;
|
||||
else {
|
||||
if (returnData["NAUSEA / VOMITING"]) {
|
||||
returnData["NAUSEA OR VOMITING"] =
|
||||
returnData["NAUSEA / VOMITING"];
|
||||
}
|
||||
}
|
||||
if (hasToUpdateDb) {
|
||||
await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
.doc(serializeGaId(gid))
|
||||
.set(returnData);
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user