Skip to content

Commit

Permalink
add update triggers and apikey function
Browse files Browse the repository at this point in the history
  • Loading branch information
brayo-pip committed Apr 2, 2024
1 parent e07d150 commit 46b9217
Showing 1 changed file with 80 additions and 8 deletions.
88 changes: 80 additions & 8 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,85 @@
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/

import {onRequest} from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";
// import {onSchedule} from "firebase-functions/v2/scheduler";
import {
onDocumentCreated,
onDocumentUpdated
} from "firebase-functions/v2/firestore";

// Start writing functions
// https://firebase.google.com/docs/functions/typescript
import * as admin from "firebase-admin";
import {error, info} from "firebase-functions/lib/logger";

// export const helloWorld = onRequest((request, response) => {
// logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
admin.initializeApp();

exports.CalculateCategoryTotals = onDocumentCreated(
"leaderboard/{userId}", (event) => {
info("Calculating totals");
const snapshot = event.data;
if (!snapshot) {
error("Document does not exist");
return;
}
const data = snapshot.data();
const categoriesTotals = data.CategoryTotals as { [key: string]: number };
let total = 0;
for (const category in categoriesTotals) {
/* eslint guard-for-in: 1 */
total += categoriesTotals[category];
}
const db = admin.firestore();
const colpath = "leaderboard";
db.collection(colpath).doc(snapshot.id).update({total: total}).then(() => {
info("Total updated");
}).catch((error) => {
error("Error updating total: ", error);
});
});

exports.UpdateCategoryTotals = onDocumentUpdated(
"leaderboard/{userId}", (event) => {
info("Updating totals on document update");
const snapshot = event.data;
if (!snapshot) {
error("Document does not exist");
return;
}
const data = snapshot.after.data();
const categoriesTotals = data.CategoryTotals as { [key: string]: number };
let total = 0;
for (const category in categoriesTotals) {
/* eslint guard-for-in: 1 */
total += categoriesTotals[category];
}
const db = admin.firestore();
const colpath = "leaderboard";
db.collection(colpath).doc(snapshot.after.id)
.update({total: total}).then(() => {
info("Total updated");
}).catch((error) => {
error("Error updating total: ", error);
});
});

export const getApiKey = functions.https.onCall(async (_, context) => {
info("Generating ApiKey");
info("Request data: ", context);
if (!context.auth) {
error("Not authenticated");
return {error: "Not authenticated"};
}
const db = admin.firestore();
const colpath = "apiKeys";
const docpath = context.auth?.uid;
const docref = db.collection(colpath).doc(docpath);
const doc = await docref.get();
if (doc.exists && doc.data() && doc.data()?.apiKey) {
info("ApiKey found");
return {apiKey: doc.data()?.apiKey};
} else {
const apiKey = genKey.generateApiKey();
await docref.set({apiKey: apiKey});
info("ApiKey set and sent to client");
return {apiKey: apiKey};
}
});

0 comments on commit 46b9217

Please sign in to comment.