From 4215821f355e4e299cf79feb93f5049c30ca90e2 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Tue, 11 Oct 2022 16:59:47 -0600 Subject: [PATCH] Fix firebase query for market creators --- functions/src/scripts/backfill-badges.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/functions/src/scripts/backfill-badges.ts b/functions/src/scripts/backfill-badges.ts index a3776eb0..8205735c 100644 --- a/functions/src/scripts/backfill-badges.ts +++ b/functions/src/scripts/backfill-badges.ts @@ -32,6 +32,9 @@ async function main() { user.achievements = await awardBettingStreakBadges(user) console.log('Added achievements to user', user.id) // going to ignore backfilling the proven correct badges for now + } else { + // Make corrections to existing achievements + await awardMarketCreatorBadges(user) } }) ) @@ -67,12 +70,11 @@ async function removeErrorBadges(user: User) { async function awardMarketCreatorBadges(user: User) { // Award market maker badges - const contracts = await getValues( - firestore - .collection(`contracts`) - .where('creatorId', '==', user.id) - .where('resolution', '!=', 'CANCEL') - ) + const contracts = ( + await getValues( + firestore.collection(`contracts`).where('creatorId', '==', user.id) + ) + ).filter((c) => !c.resolution || c.resolution != 'CANCEL') const achievements = { ...user.achievements, @@ -81,7 +83,12 @@ async function awardMarketCreatorBadges(user: User) { }, } for (const threshold of marketCreatorBadgeRarityThresholds) { + const alreadyHasBadge = user.achievements.marketCreator?.badges.some( + (b) => b.data.totalContractsCreated === threshold + ) + if (alreadyHasBadge) continue if (contracts.length >= threshold) { + console.log(`User ${user.id} has at least ${threshold} contracts`) const badge = { type: 'MARKET_CREATOR', name: 'Market Creator',