Fix loans (user without a portfolio throws error)

This commit is contained in:
James Grugett 2022-09-18 01:05:55 -05:00
parent 4aea3b96d7
commit eb021f30f5

View File

@ -7,6 +7,7 @@ import { Contract } from '../../common/contract'
import { PortfolioMetrics, User } from '../../common/user' import { PortfolioMetrics, User } from '../../common/user'
import { getLoanUpdates } from '../../common/loans' import { getLoanUpdates } from '../../common/loans'
import { createLoanIncomeNotification } from './create-notification' import { createLoanIncomeNotification } from './create-notification'
import { filterDefined } from 'common/util/array'
const firestore = admin.firestore() const firestore = admin.firestore()
@ -30,7 +31,8 @@ async function updateLoansCore() {
log( log(
`Loaded ${users.length} users, ${contracts.length} contracts, and ${bets.length} bets.` `Loaded ${users.length} users, ${contracts.length} contracts, and ${bets.length} bets.`
) )
const userPortfolios = await Promise.all( const userPortfolios = filterDefined(
await Promise.all(
users.map(async (user) => { users.map(async (user) => {
const portfolio = await getValues<PortfolioMetrics>( const portfolio = await getValues<PortfolioMetrics>(
firestore firestore
@ -41,6 +43,7 @@ async function updateLoansCore() {
return portfolio[0] return portfolio[0]
}) })
) )
)
log(`Loaded ${userPortfolios.length} portfolios`) log(`Loaded ${userPortfolios.length} portfolios`)
const portfolioByUser = keyBy(userPortfolios, (portfolio) => portfolio.userId) const portfolioByUser = keyBy(userPortfolios, (portfolio) => portfolio.userId)