diff --git a/functions/src/cron/exchange-rate-import/ExchangeRateImporter.ts b/functions/src/cron/exchange-rate-import/ExchangeRateImporter.ts index a1057e7b7..70a12e73b 100644 --- a/functions/src/cron/exchange-rate-import/ExchangeRateImporter.ts +++ b/functions/src/cron/exchange-rate-import/ExchangeRateImporter.ts @@ -1,5 +1,5 @@ import axios from 'axios'; -import * as functions from 'firebase-functions'; +import { logger } from 'firebase-functions'; import { DateTime } from 'luxon'; import { FirestoreAdmin } from '../../../../shared/src/firebase/admin/FirestoreAdmin'; import { EXCHANGE_RATES_PATH, ExchangeRates, ExchangeRatesEntry } from '../../../../shared/src/types/ExchangeRates'; @@ -67,7 +67,7 @@ export class ExchangeRateImporter { fetchAndStoreExchangeRates = async (dt: DateTime): Promise => { const rates = await this.fetchExchangeRates(dt); await this.storeExchangeRates(rates); - functions.logger.info('Ingested exchange rates'); + logger.info('Ingested exchange rates'); return rates; }; } diff --git a/functions/src/cron/exchange-rate-import/index.ts b/functions/src/cron/exchange-rate-import/index.ts index 901cd7443..c2ae97cf8 100644 --- a/functions/src/cron/exchange-rate-import/index.ts +++ b/functions/src/cron/exchange-rate-import/index.ts @@ -1,29 +1,25 @@ -import * as functions from 'firebase-functions'; +import { logger } from 'firebase-functions'; +import { onSchedule } from 'firebase-functions/lib/v2/providers/scheduler'; import { DateTime } from 'luxon'; import { ExchangeRateImporter } from './ExchangeRateImporter'; /** * Function periodically scrapes currency exchange rates and saves them to firebase */ -export default functions - .runWith({ - timeoutSeconds: 540, - }) - .pubsub.schedule('0 1 * * *') - .onRun(async () => { - const exchangeRateImporter = new ExchangeRateImporter(); - const existingExchangeRates = await exchangeRateImporter.getAllExchangeRates(); - for ( - let timestamp = ExchangeRateImporter.startTimestamp; - timestamp <= Date.now() / 1000; - timestamp += ExchangeRateImporter.secondsInDay - ) { - if (!existingExchangeRates.has(timestamp)) { - try { - await exchangeRateImporter.fetchAndStoreExchangeRates(DateTime.fromSeconds(timestamp)); - } catch (error) { - functions.logger.error(`Could not ingest exchange rate`, error); - } +export default onSchedule('0 1 * * *', async () => { + const exchangeRateImporter = new ExchangeRateImporter(); + const existingExchangeRates = await exchangeRateImporter.getAllExchangeRates(); + for ( + let timestamp = ExchangeRateImporter.startTimestamp; + timestamp <= Date.now() / 1000; + timestamp += ExchangeRateImporter.secondsInDay + ) { + if (!existingExchangeRates.has(timestamp)) { + try { + await exchangeRateImporter.fetchAndStoreExchangeRates(DateTime.fromSeconds(timestamp)); + } catch (error) { + logger.error(`Could not ingest exchange rate`, error); } } - }); + } +}); diff --git a/functions/src/cron/postfinance-balance-import/PostFinanceBalanceImporter.ts b/functions/src/cron/postfinance-balance-import/PostFinanceBalanceImporter.ts index 96907ad1a..fd90d2756 100644 --- a/functions/src/cron/postfinance-balance-import/PostFinanceBalanceImporter.ts +++ b/functions/src/cron/postfinance-balance-import/PostFinanceBalanceImporter.ts @@ -1,4 +1,4 @@ -import * as functions from 'firebase-functions'; +import { logger } from 'firebase-functions'; import imaps from 'imap-simple'; import _ from 'lodash'; import { Source, simpleParser } from 'mailparser'; @@ -40,7 +40,7 @@ export class PostFinanceBalanceImporter { retrieveBalanceMails = async (): Promise => { try { - functions.logger.info('Start checking balance inbox'); + logger.info('Start checking balance inbox'); const config = { imap: { user: POSTFINANCE_EMAIL_USER, @@ -54,7 +54,7 @@ export class PostFinanceBalanceImporter { }; const connection = await imaps.connect(config); await connection.openBox('INBOX'); - functions.logger.info('Connected to inbox'); + logger.info('Connected to inbox'); const messages = await connection.search(this.searchCriteria, this.fetchOptions); const balances = await Promise.all( messages.map(async (item: any) => { @@ -66,10 +66,10 @@ export class PostFinanceBalanceImporter { }), ); connection.end(); - functions.logger.info('Retrieved balances'); + logger.info('Retrieved balances'); return balances.flat(); } catch (error) { - functions.logger.error('Could not ingest balance mails', error); + logger.error('Could not ingest balance mails', error); return []; } }; @@ -87,7 +87,7 @@ export class PostFinanceBalanceImporter { } as BankBalance, ]; } catch { - functions.logger.info(`Could not parse email with subject ${mail.subject}`); + logger.info(`Could not parse email with subject ${mail.subject}`); return []; } }; diff --git a/functions/src/cron/postfinance-balance-import/index.ts b/functions/src/cron/postfinance-balance-import/index.ts index 162c1ef36..51f34bb39 100644 --- a/functions/src/cron/postfinance-balance-import/index.ts +++ b/functions/src/cron/postfinance-balance-import/index.ts @@ -1,11 +1,11 @@ -import * as functions from 'firebase-functions'; +import { onSchedule } from 'firebase-functions/v2/scheduler'; import { PostFinanceBalanceImporter } from './PostFinanceBalanceImporter'; /** * Function periodically connects to the gmail account where we send the postfinance balance statements, * parses the emails and stores the current balances into firestore. */ -export default functions.pubsub.schedule('0 * * * *').onRun(async () => { +export default onSchedule('0 * * * *', async () => { const postFinanceImporter = new PostFinanceBalanceImporter(); const balances = await postFinanceImporter.retrieveBalanceMails(); await postFinanceImporter.storeBalances(balances); diff --git a/functions/src/webhooks/admin/scripts/index.ts b/functions/src/webhooks/admin/scripts/index.ts index 7ee9ee87d..fece8e1c8 100644 --- a/functions/src/webhooks/admin/scripts/index.ts +++ b/functions/src/webhooks/admin/scripts/index.ts @@ -1,4 +1,4 @@ -import * as functions from 'firebase-functions'; +import { logger } from 'firebase-functions'; import { onCall } from 'firebase-functions/v2/https'; import { FirestoreAdmin } from '../../../../../shared/src/firebase/admin/FirestoreAdmin'; import { StripeEventHandler } from '../../../../../shared/src/stripe/StripeEventHandler'; @@ -34,27 +34,27 @@ const batchImportStripeChargesFunction = onCall(async ({ auth }) => { const stripeBatchSize = 100; // max batch size supported by stripe const charges = []; try { - functions.logger.info('Querying Stripe API...'); + logger.info('Querying Stripe API...'); for await (const charge of stripeEventHandler.stripe.charges.list({ expand: ['data.balance_transaction', 'data.invoice'], limit: stripeBatchSize, })) { charges.push(charge); } - functions.logger.info(`Querying stripe finished.`); + logger.info(`Querying stripe finished.`); await Promise.all( charges.map((charge) => { try { stripeEventHandler.storeCharge(charge); } catch (error) { - functions.logger.error(error); + logger.error(error); } }), ); - functions.logger.info(`Ingestion finished.`); + logger.info(`Ingestion finished.`); } catch (error) { - functions.logger.error(error); + logger.error(error); throw error; } }); diff --git a/functions/src/webhooks/stripe/index.ts b/functions/src/webhooks/stripe/index.ts index 4c359abd5..68e2fc9a3 100644 --- a/functions/src/webhooks/stripe/index.ts +++ b/functions/src/webhooks/stripe/index.ts @@ -1,4 +1,4 @@ -import * as functions from 'firebase-functions'; +import { logger } from 'firebase-functions'; import { onRequest } from 'firebase-functions/v2/https'; import Stripe from 'stripe'; import { FirestoreAdmin } from '../../../../shared/src/firebase/admin/FirestoreAdmin'; @@ -21,12 +21,12 @@ export default onRequest(async (request, response) => { break; } default: { - functions.logger.info(`Unhandled event type ${event.type}`); + logger.info(`Unhandled event type ${event.type}`); } } response.send(); } catch (error) { - functions.logger.error(error); + logger.error(error); response.status(500).send(`Webhook Error. Check the logs.`); } });