v1.0.0/release
finalize update and set logic
This commit is contained in:
+69
-6
@@ -38,17 +38,80 @@ const serializeGaId = (_gaid: string): string => {
|
||||
};
|
||||
|
||||
export const saveResult = functions.https.onCall(
|
||||
(data: SaveResultRequestData, context) => {
|
||||
async (data: SaveResultRequestData, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
const docId: string = serializeGaId(gid);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const docData : any = Object.assign({}, data);
|
||||
const docData: Record<string, string | boolean> =
|
||||
Object.assign({}, data);
|
||||
delete docData.gid;
|
||||
admin.firestore().collection("results").doc(docId).set(docData);
|
||||
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 : 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}`] =
|
||||
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 : 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}`] =
|
||||
admin.firestore.FieldValue.increment(1);
|
||||
} else {
|
||||
updateDoc[`${data.age}.${symptom}`] =
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user