Skip to content

Commit

Permalink
Merge branch 'dev' into design-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nethmalgunawardhana authored Nov 24, 2024
2 parents e8b5c1b + 5a8ad4a commit db7a44d
Show file tree
Hide file tree
Showing 20 changed files with 1,726 additions and 727 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# dependencies
/node_modules
/node_modules*
/.pnp
.pnp.*
.yarn/*
Expand Down Expand Up @@ -31,6 +32,8 @@ yarn-error.log*

# env files (can opt-in for committing if needed)
.env.local
.env*
!.env.example


# vercel
Expand Down
66 changes: 23 additions & 43 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Import Firebase Functions and Admin SDK
const logger = require("firebase-functions/logger");
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");
const {
import {initializeApp} from "firebase-admin/app";
import {getFirestore} from "firebase-admin/firestore";
import {
onDocumentCreated,
onDocumentUpdated,
onDocumentDeleted,
} = require("firebase-functions/v2/firestore");
} from "firebase-functions/v2/firestore";
import {logger} from "firebase-functions";

initializeApp();
const db = getFirestore();
Expand All @@ -18,25 +17,24 @@ const AGGREGATED_DOC_PATH = "metadata/aggregatedInfo";
const aggregateData = async () => {
try {
const aggregatedData: {
"info-sponsors": Record<string, any>;
"info-timeline": Record<string, any>;
"info-sponsors": Record<string, unknown>;
"info-timeline": Record<string, unknown>;
} = {"info-sponsors": {}, "info-timeline": {}};

// Aggregate data from info-sponsors
const sponsorsSnapshot = await db.collection("info-sponsors").get();
sponsorsSnapshot.forEach((doc: any) => {
sponsorsSnapshot.forEach((doc) => {
aggregatedData["info-sponsors"][doc.id] = doc.data();
});

// Aggregate data from info-timeline
const timelineSnapshot = await db.collection("info-timeline").get();
timelineSnapshot.forEach((doc: any) => {
timelineSnapshot.forEach((doc) => {
aggregatedData["info-timeline"][doc.id] = doc.data();
});

// Save the aggregated data to the Firestore document
await db.doc(AGGREGATED_DOC_PATH).set(aggregatedData);

logger.info("Successfully aggregated data into:", AGGREGATED_DOC_PATH);
} catch (error) {
logger.error("Error aggregating data:", error);
Expand All @@ -46,41 +44,23 @@ const aggregateData = async () => {
// Helper to create triggers for a given collection
const createTriggers = (collectionPath: string) => {
return {
onCreate: onDocumentCreated(
`${collectionPath}/{docId}`,
async (event: any) => {
logger.info(
`Document created in ${collectionPath}:`,
event.params.docId
);
await aggregateData();
}
),
onUpdate: onDocumentUpdated(
`${collectionPath}/{docId}`,
async (event: any) => {
logger.info(
`Document updated in ${collectionPath}:`,
event.params.docId
);
await aggregateData();
}
),
onDelete: onDocumentDeleted(
`${collectionPath}/{docId}`,
async (event: any) => {
logger.info(
`Document deleted in ${collectionPath}:`,
event.params.docId
);
await aggregateData();
}
),
onCreate: onDocumentCreated(`${collectionPath}/{docId}`, async (event) => {
logger.info(`Document created in ${collectionPath}:`, event.params.docId);
await aggregateData();
}),
onUpdate: onDocumentUpdated(`${collectionPath}/{docId}`, async (event) => {
logger.info(`Document updated in ${collectionPath}:`, event.params.docId);
await aggregateData();
}),
onDelete: onDocumentDeleted(`${collectionPath}/{docId}`, async (event) => {
logger.info(`Document deleted in ${collectionPath}:`, event.params.docId);
await aggregateData();
}),
};
};

// Export triggers for /info-sponsors
exports.aggregateInfoSponsors = createTriggers("info-sponsors");
export const aggregateInfoSponsors = createTriggers("info-sponsors");

// Export triggers for /info-timeline
exports.aggregateInfoTimeline = createTriggers("info-timeline");
export const aggregateInfoTimeline = createTriggers("info-timeline");
Loading

0 comments on commit db7a44d

Please sign in to comment.