From 0950a281f221ccd4812f0a5acfbe501765f77fd2 Mon Sep 17 00:00:00 2001 From: jahooma Date: Sat, 1 Jan 2022 19:13:30 -0600 Subject: [PATCH] Fix build error from merge. Extract resolution emails to function --- functions/src/emails.ts | 4 +-- functions/src/resolve-market.ts | 43 ++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 14c030f3..adf65235 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -8,7 +8,7 @@ export const sendMarketResolutionEmail = async ( payout: number, creator: User, contract: Contract, - resolution: 'YES' | 'NO' | 'CANCEL' + resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT' ) => { const user = await getUser(userId) if (!user) return @@ -32,4 +32,4 @@ https://mantic.markets/${creator.username}/${contract.slug} await sendEmail(user.email, subject, body) } -const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A' } +const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A', MKT: 'MKT' } diff --git a/functions/src/resolve-market.ts b/functions/src/resolve-market.ts index 656c73b5..164551bd 100644 --- a/functions/src/resolve-market.ts +++ b/functions/src/resolve-market.ts @@ -16,7 +16,7 @@ export const resolveMarket = functions .https.onCall( async ( data: { - outcome: 'YES' | 'NO' | 'CANCEL' + outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT' contractId: string }, context @@ -82,25 +82,40 @@ export const resolveMarket = functions .catch((e) => ({ status: 'error', message: e })) .then(() => ({ status: 'success' })) - const activeBets = bets.filter((bet) => !bet.isSold && !bet.sale) - const nonWinners = _.difference( - _.uniq(activeBets.map(({ userId }) => userId)), - Object.keys(userPayouts) - ) - const emailPayouts = [ - ...Object.entries(userPayouts), - ...nonWinners.map((userId) => [userId, 0] as const), - ] - await Promise.all( - emailPayouts.map(([userId, payout]) => - sendMarketResolutionEmail(userId, payout, creator, contract, outcome) - ) + await sendResolutionEmails( + openBets, + userPayouts, + creator, + contract, + outcome ) return result } ) +const sendResolutionEmails = async ( + openBets: Bet[], + userPayouts: { [userId: string]: number }, + creator: User, + contract: Contract, + outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT' +) => { + const nonWinners = _.difference( + _.uniq(openBets.map(({ userId }) => userId)), + Object.keys(userPayouts) + ) + const emailPayouts = [ + ...Object.entries(userPayouts), + ...nonWinners.map((userId) => [userId, 0] as const), + ] + await Promise.all( + emailPayouts.map(([userId, payout]) => + sendMarketResolutionEmail(userId, payout, creator, contract, outcome) + ) + ) +} + const firestore = admin.firestore() const getCancelPayouts = (truePool: number, bets: Bet[]) => {