190 lines
6.7 KiB
JavaScript
190 lines
6.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getPastResult = exports.saveResult = exports.getStats = 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 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;
|
|
// Get existing result
|
|
const existingResult = await admin
|
|
.firestore()
|
|
.collection("results")
|
|
.doc(serializeGaId(gid))
|
|
.get()
|
|
.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 {
|
|
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")
|
|
.doc(serializeGaId(gid))
|
|
.get()
|
|
.then((doc) => doc.data());
|
|
if (existingData) {
|
|
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"];
|
|
}
|
|
});
|
|
}
|
|
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;
|
|
});
|
|
//# sourceMappingURL=index.js.map
|