Skip to content

Commit

Permalink
Functions: Update firebase-functions dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mkue committed Dec 29, 2024
1 parent e301103 commit be85fec
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 126 deletions.
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"axios": "^1.7.7",
"dotenv": "^16.4.5",
"firebase-admin": "^12.7.0",
"firebase-functions": "5.1.1",
"firebase-functions": "^6.2.0",
"handlebars": "^4.7.8",
"handlebars-i18next": "^1.0.3",
"i18next-resources-to-backend": "^1.2.1",
Expand Down
30 changes: 15 additions & 15 deletions functions/src/firestore/firestore-auditor/FirestoreAuditor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, test } from '@jest/globals';
import functionsTest from 'firebase-functions-test';
import auditCollectionTriggerFunction from '.';
import auditFirestore from '.';
import { FirestoreAdmin } from '../../../../shared/src/firebase/admin/FirestoreAdmin';
import { getOrInitializeFirebaseAdmin } from '../../../../shared/src/firebase/admin/app';

Expand Down Expand Up @@ -35,9 +35,9 @@ describe('FirestoreAuditor', () => {
await testDoc.set({ foo: 'bar' });
const snap = testEnv.firestore.makeDocumentSnapshot({ foo: 'bar' }, testDoc.path);
const change = testEnv.makeChange(null, snap);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });
// retrieve the doc. Test that last_updated_at is set
const updatedDoc = await testDoc.get();
expect(updatedDoc.data()!.foo).toEqual('bar');
Expand All @@ -53,9 +53,9 @@ describe('FirestoreAuditor', () => {
await testSubColDoc.set({ foo: 'bar' });
const snap = testEnv.firestore.makeDocumentSnapshot({ foo: 'bar' }, testSubColDoc.path);
const change = testEnv.makeChange(null, snap);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });
// retrieve the doc. Test that last_updated_at is set
const updatedDoc = await testSubColDoc.get();
expect(updatedDoc.data()!.foo).toEqual('bar');
Expand All @@ -76,9 +76,9 @@ describe('FirestoreAuditor', () => {
const snapBefore = testEnv.firestore.makeDocumentSnapshot({ foo: 'before', last_updated_at: 123 }, testDoc.path);
const snapAfter = testEnv.firestore.makeDocumentSnapshot({ foo: 'after' }, testDoc.path);
const change = testEnv.makeChange(snapBefore, snapAfter);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });
// retrieve the doc. Test that last_updated_at is updated
const updatedDoc = await testDoc.get();
expect(updatedDoc.data()!.foo).toEqual('after');
Expand All @@ -101,9 +101,9 @@ describe('FirestoreAuditor', () => {
);
const snapAfter = testEnv.firestore.makeDocumentSnapshot({ foo: 'after' }, testSubColDoc.path);
const change = testEnv.makeChange(snapBefore, snapAfter);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });
// retrieve the doc. Test that last_updated_at is updated
const updatedDoc = await testSubColDoc.get();
expect(updatedDoc.data()!.foo).toEqual('after');
Expand All @@ -121,9 +121,9 @@ describe('FirestoreAuditor', () => {
// setup simulated deletion
const snapBefore = testEnv.firestore.makeDocumentSnapshot({ foo: 'before', last_updated_at: 123 }, testDoc.path);
const change = testEnv.makeChange(snapBefore, null);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });

// test that the history is populated
const history = await testColHistoryEntry.get();
Expand All @@ -140,9 +140,9 @@ describe('FirestoreAuditor', () => {
testSubColDoc.path,
);
const change = testEnv.makeChange(snapBefore, null);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });

// test that the history is populated
const history = await testSubColHistoryEntry.get();
Expand All @@ -159,9 +159,9 @@ describe('FirestoreAuditor', () => {
await testHistoryEntry.set({ foo: 'bar' });
const snap = testEnv.firestore.makeDocumentSnapshot({ foo: 'bar' }, testHistoryEntry.path);
const change = testEnv.makeChange(null, snap);
const wrapped = testEnv.wrap(auditCollectionTriggerFunction);
const wrapped = testEnv.wrap(auditFirestore);
// call the trigger with the simulated change
await wrapped(change);
await wrapped({ data: change });
// retrieve the doc. Test that last_updated_at is not set
const updatedDoc = await testHistoryEntry.get();
expect(updatedDoc.data()!.foo).toEqual('bar');
Expand Down
27 changes: 12 additions & 15 deletions functions/src/firestore/firestore-auditor/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { DocumentSnapshot } from '@google-cloud/firestore';
import * as functions from 'firebase-functions';
import { Change, EventContext } from 'firebase-functions';
import { logger } from 'firebase-functions';
import { onDocumentWritten } from 'firebase-functions/v2/firestore';
import { DateTime } from 'luxon';
import { DEFAULT_REGION } from '../../../../shared/src/firebase';
import { FirestoreAuditor } from './FirestoreAuditor';

/**
* Triggers changes to documents of root collections
*/
export default functions
// Using v1 functions because tests are not supported with v2 yet: https://github.com/firebase/firebase-functions-test/issues/163
.region(DEFAULT_REGION)
.firestore.document('{collectionId}/{document=**}')
.onWrite(async (change: Change<DocumentSnapshot>, context: EventContext) => {
const firestoreAuditor = new FirestoreAuditor();
return firestoreAuditor.auditFirestore(change, DateTime.fromISO(context.timestamp));
});
const auditFirestore = onDocumentWritten({ document: '{collectionId}/{document=**}' }, async (event) => {
const firestoreAuditor = new FirestoreAuditor();
if (!event.data) {
logger.warn('No data in event');
return;
}
await firestoreAuditor.auditFirestore(event.data, DateTime.fromISO(event.time));
});

export default auditFirestore;
4 changes: 2 additions & 2 deletions functions/src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import auditCollectionTriggerFunction from './firestore-auditor';
import auditFirestore from './firestore-auditor';

export const auditCollectionTrigger = auditCollectionTriggerFunction;
export const auditCollectionTrigger = auditFirestore;
12 changes: 6 additions & 6 deletions functions/src/webhooks/admin/payment-process/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as functions from 'firebase-functions';
import { onCall } from 'firebase-functions/v2/https';
import { DateTime } from 'luxon';
import { DEFAULT_REGION } from '../../../../../shared/src/firebase';
import { FirestoreAdmin } from '../../../../../shared/src/firebase/admin/FirestoreAdmin';
import { PaymentProcessTaskType } from '../../../../../shared/src/types/payment';
import { toPaymentDate } from '../../../../../shared/src/types/recipient';
Expand All @@ -15,10 +15,9 @@ export interface PaymentProcessProps {
timestamp: number; // seconds
}

export default functions
// Using v1 functions because tests are not supported with v2 yet: https://github.com/firebase/firebase-functions-test/issues/163
.region(DEFAULT_REGION)
.https.onCall(async ({ type, timestamp }: PaymentProcessProps, { auth }) => {
export default onCall<PaymentProcessProps, Promise<string>>(
{ memory: '2GiB' },
async ({ auth, data: { timestamp, type } }) => {
const firestoreAdmin = new FirestoreAdmin();
await firestoreAdmin.assertGlobalAdmin(auth?.token?.email);
const paymentDate = toPaymentDate(DateTime.fromSeconds(timestamp, { zone: 'utc' }));
Expand All @@ -42,4 +41,5 @@ export default functions
}

return await task.run(paymentDate);
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ afterEach(async () => {
});

test('GetPaymentCSV', async () => {
const result = await triggerFunction(
{ type: PaymentProcessTaskType.GetPaymentCSV, timestamp: paymentDate.toSeconds() },
{ auth: { token: { email: '[email protected]' } } },
);
const result = await triggerFunction({
data: { type: PaymentProcessTaskType.GetPaymentCSV, timestamp: paymentDate.toSeconds() },
// @ts-ignore
auth: { token: { email: '[email protected]' } },
});

const rows = result.split('\n').map((row: string) => row.split(','));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ afterEach(async () => {
});

test('GetRegistrationCSV', async () => {
const result = await triggerFunction(
{ type: PaymentProcessTaskType.GetRegistrationCSV, timestamp: paymentDate.toSeconds() },
{ auth: { token: { email: '[email protected]' } } },
);
const result = await triggerFunction({
data: { type: PaymentProcessTaskType.GetRegistrationCSV, timestamp: paymentDate.toSeconds() },
// @ts-ignore
auth: { token: { email: '[email protected]' } },
});

const rows = result.split('\n').map((row: string) => row.split(','));
expect(rows).toHaveLength(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ afterEach(async () => {
});

test('CreatePayments', async () => {
const result = await triggerFunction(
{ type: PaymentProcessTaskType.CreatePayments, timestamp: paymentDate.toSeconds() },
{
auth: { token: { email: '[email protected]' } },
},
);
const result = await triggerFunction({
data: { type: PaymentProcessTaskType.CreatePayments, timestamp: paymentDate.toSeconds() },
// @ts-ignore
auth: { token: { email: '[email protected]' } },
});
expect(result).toEqual(
'Set status of 3 payments to paid and created 3 payments for next month. Set status to "former" for 0 recipients and status to active for 1 recipients.',
);
Expand Down Expand Up @@ -81,10 +80,11 @@ test('CreatePayments', async () => {
expect(nextPayment.currency).toEqual('SLE');
}

const secondExecutionResult = await triggerFunction(
{ type: PaymentProcessTaskType.CreatePayments, timestamp: paymentDate.toSeconds() },
{ auth: { token: { email: '[email protected]' } } },
);
const secondExecutionResult = await triggerFunction({
data: { type: PaymentProcessTaskType.CreatePayments, timestamp: paymentDate.toSeconds() },
// @ts-ignore
auth: { token: { email: '[email protected]' } },
});
expect(secondExecutionResult).toEqual(
'Set status of 0 payments to paid and created 0 payments for next month. Set status to "former" for 0 recipients and status to active for 0 recipients.',
);
Expand Down
4 changes: 2 additions & 2 deletions functions/src/webhooks/twilio/TwilioOutgoingMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as functions from 'firebase-functions';
import { onCall } from 'firebase-functions/v2/https';
import { MessageInstance } from 'twilio/lib/rest/api/v2010/account/message';
import { FirestoreAdmin } from '../../../../shared/src/firebase/admin/FirestoreAdmin';
import { Entity } from '../../../../shared/src/types';
Expand All @@ -20,7 +20,7 @@ export class TwilioOutgoingMessageHandler {
}

getFunction = () =>
functions.https.onCall(async ({ recipients, template }: TwilioOutgoingMessageFunctionProps, { auth }) => {
onCall<TwilioOutgoingMessageFunctionProps>(async ({ data: { template, recipients }, auth }) => {
await this.firestoreAdmin.assertGlobalAdmin(auth?.token?.email);
let [successCount, skippedCount] = [0, 0];
for await (const { values: recipient, id } of recipients) {
Expand Down
Loading

0 comments on commit be85fec

Please sign in to comment.