Skip to content

Commit

Permalink
fix: dont send firebase messages on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
invertedEcho committed Dec 23, 2024
1 parent bffa54b commit 4dfa212
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ jobs:

- name: Run tests
run: |
echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env.test && echo FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT=${{ secrets.FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT }} >> .env.test
echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env.test
pnpm test
working-directory: ./backend
4 changes: 4 additions & 0 deletions backend/src/assignment/assignment-scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class AssignmentSchedulerService {
)}`,
);

if (process.env.CI === 'true') {
return;
}

const userIds = assignmentsToCreate.map(
(assignment) => assignment.userId,
);
Expand Down
18 changes: 18 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
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');

if (firebaseAdmin.apps.length === 0) {
const firebaseServiceAccountJsonContent =
process.env.FIREBASE_SERVICE_ACCOUNT_JSON_CONTENT;

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}`);
}
Expand Down
27 changes: 3 additions & 24 deletions backend/src/notifications/notification.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
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,
Expand All @@ -29,9 +10,7 @@ export async function sendFirebaseMessages({
}

const messaging = getMessaging();
const response = await messaging.sendEach(messages);
if (response.failureCount > 0) {
console.error({ loc: JSON.stringify(response.responses) });
throw new Error('Did not send all messages!');
}

// TODO: all tokens that error because they are no longer registered should be removed from our table.
await messaging.sendEach(messages);
}
12 changes: 1 addition & 11 deletions frontend/lib/firebase_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;

/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
Expand Down Expand Up @@ -65,5 +55,5 @@ class DefaultFirebaseOptions {
storageBucket: 'flatshare-223fd.firebasestorage.app',
iosBundleId: 'com.invertedecho.flaatshare',
);
}

}

0 comments on commit 4dfa212

Please sign in to comment.