From bffa54bdadbae1f0b8f6c5aae9c4cb0d7c661b3a Mon Sep 17 00:00:00 2001 From: Jakob Stechow Date: Sat, 21 Dec 2024 20:39:34 +0100 Subject: [PATCH] fix: initialize firebase admin app only if required --- .github/workflows/ci.yml | 2 +- backend/src/main.ts | 18 ------------------ backend/src/notifications/notification.ts | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f2cfc2..1bbe1d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,6 @@ jobs: - name: Run tests run: | - echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env.test + echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env.test && echo FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT=${{ secrets.FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT }} >> .env.test pnpm test working-directory: ./backend diff --git a/backend/src/main.ts b/backend/src/main.ts index d1454a2..39f21a6 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -1,30 +1,12 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -import firebaseAdmin from 'firebase-admin'; - async function bootstrap() { const app = await NestFactory.create(AppModule); app.setGlobalPrefix('api'); app.enableCors(); await app.listen(3000, '0.0.0.0'); - const firebaseServiceAccountJsonContent = - process.env.FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT; - - // TODO: should probably do the same for all other environment variables too. - if (firebaseServiceAccountJsonContent === undefined) { - throw new Error( - 'FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT environment variable must be set.', - ); - } - - firebaseAdmin.initializeApp({ - credential: firebaseAdmin.credential.cert( - JSON.parse(firebaseServiceAccountJsonContent), - ), - }); - const appUrl = await app.getUrl(); console.log(`Flatshare Backend is running on: ${appUrl}`); } diff --git a/backend/src/notifications/notification.ts b/backend/src/notifications/notification.ts index 79c573e..3a2ff66 100644 --- a/backend/src/notifications/notification.ts +++ b/backend/src/notifications/notification.ts @@ -1,10 +1,33 @@ import { Message, getMessaging } from 'firebase-admin/messaging'; +import firebaseAdmin from 'firebase-admin'; + +if (firebaseAdmin.apps.length === 0) { + const firebaseServiceAccountJsonContent = + process.env.FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT; + + // TODO: should probably do the same for all other environment variables too. + if (firebaseServiceAccountJsonContent === undefined) { + throw new Error( + 'FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT environment variable must be set.', + ); + } + + firebaseAdmin.initializeApp({ + credential: firebaseAdmin.credential.cert( + JSON.parse(firebaseServiceAccountJsonContent), + ), + }); +} export async function sendFirebaseMessages({ messages, }: { messages: Message[]; }) { + if (messages.length === 0) { + return; + } + const messaging = getMessaging(); const response = await messaging.sendEach(messages); if (response.failureCount > 0) {