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,16 +31,18 @@ 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(
users.map(async (user) => { await Promise.all(
const portfolio = await getValues<PortfolioMetrics>( users.map(async (user) => {
firestore const portfolio = await getValues<PortfolioMetrics>(
.collection(`users/${user.id}/portfolioHistory`) firestore
.orderBy('timestamp', 'desc') .collection(`users/${user.id}/portfolioHistory`)
.limit(1) .orderBy('timestamp', 'desc')
) .limit(1)
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)