V1.1.0/add compat functions, script to update, data source select
This commit is contained in:
+179
-3
@@ -45,6 +45,7 @@ const serializeGaId = (_gaid: string): string => {
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
const addResultToStats = async (data : SaveResultRequestData) => {
|
||||
const cleanData : Record<string, any> =
|
||||
Object.assign({}, data);
|
||||
@@ -56,6 +57,15 @@ const addResultToStats = async (data : SaveResultRequestData) => {
|
||||
count: admin.firestore.FieldValue.increment(1),
|
||||
};
|
||||
if (data.name === VAX_NAMES.PB || data.name === VAX_NAMES.MD) {
|
||||
if (data.dose === VAX_DOSES.ONE) {
|
||||
updateDoc[`${VAX_DOSES.ONE}.${data.age}.count`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
} else if (data.dose === VAX_DOSES.TWO) {
|
||||
updateDoc[`${VAX_DOSES.ONE}.${data.age}.count`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
updateDoc[`${VAX_DOSES.TWO}.${data.age}.count`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
}
|
||||
Object.keys(VAX_DOSES).forEach((doseKey : string) => {
|
||||
const dose : string = VAX_DOSES[doseKey];
|
||||
Object.keys(cleanData[dose]).forEach((symptom : string) => {
|
||||
@@ -70,6 +80,8 @@ const addResultToStats = async (data : SaveResultRequestData) => {
|
||||
});
|
||||
});
|
||||
} else {
|
||||
updateDoc[`${data.age}.count`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
Object.keys(cleanData).forEach((symptom : string) => {
|
||||
let updateDocSymptom = symptom.replace("/", "OR");
|
||||
if (symptom === "DIARRHEA") {
|
||||
@@ -119,7 +131,10 @@ const removeResultFromStats = async (data : FirebaseFirestore.DocumentData) => {
|
||||
admin.firestore().collection("stats").doc(data.name).update(updateDoc);
|
||||
};
|
||||
|
||||
export const getStats = functions.https.onCall(
|
||||
/**
|
||||
* FUNCTIONS FROM V1.1.0 TO CURRENT
|
||||
*/
|
||||
export const getStatsV1 = functions.https.onCall(
|
||||
async () => {
|
||||
const stats = await admin.firestore().collection("stats").get();
|
||||
const docs = stats.docs
|
||||
@@ -132,7 +147,7 @@ export const getStats = functions.https.onCall(
|
||||
return returnData;
|
||||
});
|
||||
|
||||
export const saveResult = functions.https.onCall(
|
||||
export const saveResultV1 = functions.https.onCall(
|
||||
async (data: SaveResultRequestData, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
@@ -162,7 +177,7 @@ export const saveResult = functions.https.onCall(
|
||||
}
|
||||
});
|
||||
|
||||
export const getPastResult =
|
||||
export const getPastResultV1 =
|
||||
functions.https.onCall(async (data : {gid:string}, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
@@ -227,3 +242,164 @@ export const getPastResult =
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
/**
|
||||
* FUNCTIONS FROM V1.0.0 TO V1.0.1
|
||||
*/
|
||||
export const saveResult = functions.https.onCall(
|
||||
async (data: SaveResultRequestData, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
const docId: string = serializeGaId(gid);
|
||||
const docData: Record<string, string | boolean> =
|
||||
Object.assign({}, data);
|
||||
delete docData.gid;
|
||||
const existingData = await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
.doc(docId)
|
||||
.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 : Record<string, admin.firestore.FieldValue> = {};
|
||||
Object.keys(docData).forEach((symptom : string) => {
|
||||
// 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);
|
||||
} 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 : Record<string, admin.firestore.FieldValue> = {
|
||||
count: admin.firestore.FieldValue.increment(-1),
|
||||
};
|
||||
Object.keys(existingData).forEach((symptom : string) => {
|
||||
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 : Record<string, admin.firestore.FieldValue> = {
|
||||
count: admin.firestore.FieldValue.increment(1),
|
||||
};
|
||||
// INCREMENT VALUES FOR EACH SYMPTOM
|
||||
Object.keys(docData).forEach((symptom : string) => {
|
||||
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 : Record<string, admin.firestore.FieldValue> = {
|
||||
count: admin.firestore.FieldValue.increment(1),
|
||||
};
|
||||
// INCREMENT VALUES FOR EACH SYMPTOM
|
||||
Object.keys(docData).forEach((symptom : string) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const getPastResult = functions.https.onCall(async (data, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
const existingData : admin.firestore.DocumentData | undefined = await admin
|
||||
.firestore()
|
||||
.collection("results")
|
||||
.doc(serializeGaId(gid))
|
||||
.get()
|
||||
.then((doc) => doc.data());
|
||||
if (existingData) {
|
||||
if (existingData["NAUSEA / VOMITING"]) {
|
||||
existingData["NAUSEA OR VOMITING"] =
|
||||
existingData["NAUSEA / VOMITING"];
|
||||
}
|
||||
return existingData;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
|
||||
const admin = require("firebase-admin");
|
||||
const firebaseSecret = require("./firebaseSecret.json");
|
||||
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(firebaseSecret),
|
||||
});
|
||||
|
||||
const VAX_NAMES = {
|
||||
PB: "Pfizer-BioNTech",
|
||||
MD: "Moderna",
|
||||
JJ: "J&J Janssen",
|
||||
};
|
||||
|
||||
const VAX_DOSES = {
|
||||
ONE: "Dose 1",
|
||||
TWO: "Dose 2",
|
||||
};
|
||||
|
||||
const VAX_AGES = {
|
||||
"Pfizer-BioNTech": {
|
||||
YOUNGER: "18 - 55",
|
||||
OLDER: "56 & Older",
|
||||
},
|
||||
"Moderna": {
|
||||
YOUNGER: "18 - 64",
|
||||
OLDER: "65 & Older",
|
||||
},
|
||||
"J&J Janssen": {
|
||||
YOUNGER: "18 - 59",
|
||||
OLDER: "60 & Older",
|
||||
},
|
||||
};
|
||||
|
||||
// PFIZER COUNTS
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.PB)
|
||||
.where("dose", "in", [VAX_DOSES.ONE, VAX_DOSES.TWO])
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.PB].YOUNGER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("PB_ONE_YOUNGER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.PB)
|
||||
.update({
|
||||
[`${VAX_DOSES.ONE}.${VAX_AGES[VAX_NAMES.PB].YOUNGER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.PB)
|
||||
.where("dose", "==", VAX_DOSES.TWO)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.PB].YOUNGER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("PB_TWO_YOUNGER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.PB)
|
||||
.update({
|
||||
[`${VAX_DOSES.TWO}.${VAX_AGES[VAX_NAMES.PB].YOUNGER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.PB)
|
||||
.where("dose", "in", [VAX_DOSES.ONE, VAX_DOSES.TWO])
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.PB].OLDER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("PB_ONE_OLDER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.PB)
|
||||
.update({
|
||||
[`${VAX_DOSES.ONE}.${VAX_AGES[VAX_NAMES.PB].OLDER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.PB)
|
||||
.where("dose", "==", VAX_DOSES.TWO)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.PB].OLDER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("PB_TWO_OLDER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.PB)
|
||||
.update({
|
||||
[`${VAX_DOSES.TWO}.${VAX_AGES[VAX_NAMES.PB].OLDER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
// MODERNA COUNTS
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.MD)
|
||||
.where("dose", "in", [VAX_DOSES.ONE, VAX_DOSES.TWO])
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.MD].YOUNGER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("MD_ONE_YOUNGER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.MD)
|
||||
.update({
|
||||
[`${VAX_DOSES.ONE}.${VAX_AGES[VAX_NAMES.MD].YOUNGER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.MD)
|
||||
.where("dose", "==", VAX_DOSES.TWO)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.MD].YOUNGER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("MD_TWO_YOUNGER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.MD)
|
||||
.update({
|
||||
[`${VAX_DOSES.TWO}.${VAX_AGES[VAX_NAMES.MD].YOUNGER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.MD)
|
||||
.where("dose", "in", [VAX_DOSES.ONE, VAX_DOSES.TWO])
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.MD].OLDER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("MD_ONE_OLDER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.MD)
|
||||
.update({
|
||||
[`${VAX_DOSES.ONE}.${VAX_AGES[VAX_NAMES.MD].OLDER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.MD)
|
||||
.where("dose", "==", VAX_DOSES.TWO)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.MD].OLDER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("MD_TWO_OLDER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.MD)
|
||||
.update({
|
||||
[`${VAX_DOSES.TWO}.${VAX_AGES[VAX_NAMES.MD].OLDER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
// J&J COUNTS
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.JJ)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.JJ].YOUNGER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("JJ_YOUNGER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.JJ)
|
||||
.update({
|
||||
[`${VAX_AGES[VAX_NAMES.JJ].YOUNGER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
admin.firestore().collection("results")
|
||||
.where("name", "==", VAX_NAMES.JJ)
|
||||
.where("age", "==", VAX_AGES[VAX_NAMES.JJ].OLDER)
|
||||
.get()
|
||||
.then((docs) => {
|
||||
console.log("JJ_OLDER", docs.docs.length);
|
||||
admin.firestore()
|
||||
.collection("stats")
|
||||
.doc(VAX_NAMES.JJ)
|
||||
.update({
|
||||
[`${VAX_AGES[VAX_NAMES.JJ].OLDER}.count`]:
|
||||
docs.docs.length,
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user