v1.1.0/server side second dose
This commit is contained in:
@@ -24,5 +24,4 @@ yarn-debug.log*
|
||||
yarn-error.log*
|
||||
*.cache
|
||||
*.log
|
||||
functions/lib/
|
||||
functions/src/backup.json
|
||||
|
||||
+64
-25
@@ -19,16 +19,24 @@ admin.initializeApp({
|
||||
credential: admin.credential.cert(params),
|
||||
});
|
||||
|
||||
interface RequestData {
|
||||
gid: string;
|
||||
}
|
||||
type SideEffect = Record<string, boolean>;
|
||||
type DoseToSideEffect = Record<string, SideEffect>;
|
||||
type DBResult = {
|
||||
name : string;
|
||||
age : string;
|
||||
"J&J Janssen"? : SideEffect;
|
||||
"Moderna"? : DoseToSideEffect;
|
||||
"Pfizer-BioNTech"? : DoseToSideEffect;
|
||||
};
|
||||
|
||||
interface SaveResultRequestData extends RequestData {
|
||||
type SaveResultRequestData = {
|
||||
name: string;
|
||||
age: string;
|
||||
dose: string;
|
||||
[x: string]: string;
|
||||
}
|
||||
gid: string;
|
||||
} & {
|
||||
[x: string] : boolean;
|
||||
};
|
||||
|
||||
const serializeGaId = (_gaid: string): string => {
|
||||
if (_gaid) {
|
||||
@@ -37,30 +45,61 @@ const serializeGaId = (_gaid: string): string => {
|
||||
return "";
|
||||
};
|
||||
|
||||
if (process.env.NODE_ENV && process.env.NODE_ENV === "dev") {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const backup = require("./backup.json");
|
||||
const BackupResults : Record<string, Record<string,
|
||||
boolean | Record<string, unknown> | string>> =
|
||||
backup.results;
|
||||
const requestDataToDbData = (data: SaveResultRequestData) : DBResult => {
|
||||
console.log(data);
|
||||
return {name: "a", age: "d"};
|
||||
};
|
||||
|
||||
const BackupStats : Record<string,
|
||||
Record<string, unknown> |
|
||||
Record<string, number> |
|
||||
Record<string, Record<string, number> |
|
||||
Record<string, Record<string, Record<string, number>>>>> =
|
||||
backup.stats;
|
||||
const addResultToStats = async (data : DBResult) => {
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
Object.keys(BackupResults).forEach((key : string) => {
|
||||
admin.firestore().collection("results").doc(key).set(BackupResults[key]);
|
||||
const removeResultFromStats = async (data : DBResult) => {
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
const setResultInDb = async (data: SaveResultRequestData) => {
|
||||
const {name, age, dose} = data;
|
||||
const result : DBResult = {name, age};
|
||||
const rawResult : Record<string, boolean> = {};
|
||||
Object.keys(data).filter(k => data[k] === true || data[k] === false).forEach(k => {
|
||||
rawResult[k] = data[k];
|
||||
});
|
||||
Object.keys(BackupStats).forEach((key : string) => {
|
||||
admin.firestore().collection("stats").doc(key).set(BackupStats[key]);
|
||||
});
|
||||
}
|
||||
if (!!dose.replace(/\s/g, "")) {
|
||||
result[name][dose][age] =
|
||||
}
|
||||
await addResultToStats(requestDataToDbData(data));
|
||||
return result;
|
||||
};
|
||||
|
||||
export const saveResult = functions.https.onCall(
|
||||
async (data: SaveResultRequestData, 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());
|
||||
// No existing result
|
||||
if (existingResult === undefined) {
|
||||
// Set result
|
||||
setResultInDb(data);
|
||||
} else {
|
||||
const {dose, name, age} = data;
|
||||
if (existingResult.name === name && // Same vax
|
||||
existingResult.age === age && // Same age group
|
||||
!!dose.replace(/\s/g, "") && // Has dose sub category
|
||||
!existingResult[name][age][dose]) { // Dose not existing
|
||||
setResultInDb(data);
|
||||
} else {
|
||||
await removeResultFromStats(existingResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
const docId: string = serializeGaId(gid);
|
||||
@@ -197,7 +236,7 @@ export const saveResult = functions.https.onCall(
|
||||
}
|
||||
});
|
||||
|
||||
export const getPastResult = functions.https.onCall(async (data, context) => {
|
||||
export const getPastResult = functions.https.onCall(async (data : {gid:string}, context) => {
|
||||
if (!!data && !!context) {
|
||||
const {gid} = data;
|
||||
const existingData : admin.firestore.DocumentData | undefined = await admin
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject",
|
||||
"deploy": "firebase deploy --only hosting",
|
||||
"serve" : "firebase serve --only hosting"
|
||||
"serve": "firebase serve --only hosting"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": [
|
||||
|
||||
Reference in New Issue
Block a user