diff --git a/backend/supabase/user_contract_metrics.sql b/backend/supabase/user_contract_metrics.sql index e102c1300e..a95247ad04 100644 --- a/backend/supabase/user_contract_metrics.sql +++ b/backend/supabase/user_contract_metrics.sql @@ -47,6 +47,7 @@ BEGIN -- Update the row where answer_id is null with the aggregated metrics UPDATE user_contract_metrics SET + data = data || jsonb_build_object('hasYesShares', sum_has_yes_shares, 'hasNoShares', sum_has_no_shares, 'hasShares', sum_has_shares), has_yes_shares = sum_has_yes_shares, has_no_shares = sum_has_no_shares, has_shares = sum_has_shares diff --git a/common/src/calculate-metrics.ts b/common/src/calculate-metrics.ts index 84fcca76ce..18f62d0087 100644 --- a/common/src/calculate-metrics.ts +++ b/common/src/calculate-metrics.ts @@ -1,4 +1,13 @@ -import { Dictionary, groupBy, min, orderBy, sum, sumBy, uniq } from 'lodash' +import { + cloneDeep, + Dictionary, + groupBy, + min, + orderBy, + sum, + sumBy, + uniq, +} from 'lodash' import { calculatePayout, calculateTotalSpentAndShares, @@ -415,11 +424,13 @@ export const calculateAnswerMetricsWithNewBetsOnly = ( return Object.entries(betsByUser).flatMap(([userId, bets]) => { // If it's a multi market, we need to summarize the stats for the null answer - const oldSummary = userMetrics.find( - (m) => - m.answerId === null && - m.userId === userId && - m.contractId === contractId + const oldSummary = cloneDeep( + userMetrics.find( + (m) => + m.answerId === null && + m.userId === userId && + m.contractId === contractId + ) ) const userBetsByAnswer = groupBy(bets, 'answerId') const newMetrics = Object.entries(userBetsByAnswer).map(