diff --git a/functions/src/update-loans.ts b/functions/src/update-loans.ts index 770315fd..a3b8f60c 100644 --- a/functions/src/update-loans.ts +++ b/functions/src/update-loans.ts @@ -7,6 +7,7 @@ import { Contract } from '../../common/contract' import { PortfolioMetrics, User } from '../../common/user' import { getLoanUpdates } from '../../common/loans' import { createLoanIncomeNotification } from './create-notification' +import { filterDefined } from 'common/util/array' const firestore = admin.firestore() @@ -30,16 +31,18 @@ async function updateLoansCore() { log( `Loaded ${users.length} users, ${contracts.length} contracts, and ${bets.length} bets.` ) - const userPortfolios = await Promise.all( - users.map(async (user) => { - const portfolio = await getValues( - firestore - .collection(`users/${user.id}/portfolioHistory`) - .orderBy('timestamp', 'desc') - .limit(1) - ) - return portfolio[0] - }) + const userPortfolios = filterDefined( + await Promise.all( + users.map(async (user) => { + const portfolio = await getValues( + firestore + .collection(`users/${user.id}/portfolioHistory`) + .orderBy('timestamp', 'desc') + .limit(1) + ) + return portfolio[0] + }) + ) ) log(`Loaded ${userPortfolios.length} portfolios`) const portfolioByUser = keyBy(userPortfolios, (portfolio) => portfolio.userId)