Skip to content

Commit

Permalink
fix: Missing validation before looking for the last item before compl…
Browse files Browse the repository at this point in the history
…eting badge (#110)
  • Loading branch information
kevinszuchet authored Oct 7, 2024
1 parent d7ed26d commit 4decb17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion api/src/logic/backfill-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ export function createBackfillMergerComponent({
throw new InvalidRequestError('Invalid Badge ID')
}
} catch (error: any) {
logger.error('Failure while backfilling badge', { error: error.message, stack: JSON.stringify(error.stack) })
logger.error('Failure while backfilling badge for user', {
error: error.message,
stack: JSON.stringify(error.stack),
userAddress
})
throw new InvalidRequestError('Could not backfill this badge')
}
}
Expand Down
14 changes: 12 additions & 2 deletions api/src/logic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ export function tryToGetCompletedAt<T, TK extends KeyOfWithValue<T, number>>(
!userProgress.achieved_tiers ||
userProgress.achieved_tiers.length === 0 ||
!badge.tiers ||
badge.tiers?.length === 0
badge.tiers?.length === 0 ||
badge.tiers.length !== userProgress.achieved_tiers.length
) {
return undefined
}

const [lastTier] = badge.tiers.slice(-1)
const { [timestampKey]: lastTierAchievedAt } = sortedItems[lastTier?.criteria.steps - 1]
const itemThatAchievedLastTier = sortedItems[lastTier.criteria.steps - 1]

// Should always be found, because we are using all registries from backfill and database
if (!itemThatAchievedLastTier) {
throw new Error(
`Could not find the item related to the last tier ${lastTier?.tierId}. Stopping the backfill process...`
)
}

const { [timestampKey]: lastTierAchievedAt } = itemThatAchievedLastTier

return getCompletedAt(userProgress, lastTierAchievedAt as number)
}
Expand Down

0 comments on commit 4decb17

Please sign in to comment.