|
| 1 | +import { internalMutation } from "../_generated/server"; |
| 2 | +import { v } from "convex/values"; |
| 3 | +import { calculateActivityPoints, calculateThresholdBonuses, calculateMediaBonus } from "../lib/scoring"; |
| 4 | +import { notDeleted } from "../lib/activityFilters"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Re-score Strava activities with 0 points that have valid metrics. |
| 8 | + * Fixes activities scored before metric alias resolution was added. |
| 9 | + */ |
| 10 | +export const rescoreZeroPointActivities = internalMutation({ |
| 11 | + args: { |
| 12 | + challengeId: v.id("challenges"), |
| 13 | + dryRun: v.boolean(), |
| 14 | + }, |
| 15 | + handler: async (ctx, args) => { |
| 16 | + const challenge = await ctx.db.get(args.challengeId); |
| 17 | + if (!challenge) throw new Error("Challenge not found"); |
| 18 | + |
| 19 | + // Get all Strava activities with 0 points |
| 20 | + const activities = await ctx.db |
| 21 | + .query("activities") |
| 22 | + .withIndex("challengeId", (q) => q.eq("challengeId", args.challengeId)) |
| 23 | + .filter((q) => |
| 24 | + q.and( |
| 25 | + q.eq(q.field("source"), "strava"), |
| 26 | + q.eq(q.field("pointsEarned"), 0), |
| 27 | + notDeleted(q) |
| 28 | + ) |
| 29 | + ) |
| 30 | + .collect(); |
| 31 | + |
| 32 | + const results: Array<{ |
| 33 | + activityId: string; |
| 34 | + userId: string; |
| 35 | + activityType: string; |
| 36 | + metrics: Record<string, unknown>; |
| 37 | + oldPoints: number; |
| 38 | + newPoints: number; |
| 39 | + }> = []; |
| 40 | + |
| 41 | + // Track per-user point adjustments |
| 42 | + const userPointAdjustments = new Map<string, number>(); |
| 43 | + |
| 44 | + for (const activity of activities) { |
| 45 | + const activityType = await ctx.db.get(activity.activityTypeId); |
| 46 | + if (!activityType) continue; |
| 47 | + |
| 48 | + const metrics = (activity.metrics ?? {}) as Record<string, unknown>; |
| 49 | + const loggedDate = new Date(activity.loggedDate); |
| 50 | + |
| 51 | + const basePoints = await calculateActivityPoints(activityType, { |
| 52 | + ctx, |
| 53 | + metrics, |
| 54 | + userId: activity.userId, |
| 55 | + challengeId: args.challengeId, |
| 56 | + loggedDate, |
| 57 | + }); |
| 58 | + |
| 59 | + const { totalBonusPoints: thresholdBonusPoints, triggeredBonuses } = |
| 60 | + calculateThresholdBonuses(activityType, metrics); |
| 61 | + |
| 62 | + const hasMedia = !!(activity.mediaIds?.length || activity.imageUrl); |
| 63 | + const { totalBonusPoints: mediaBonusPoints, triggeredBonus: mediaTriggered } = |
| 64 | + calculateMediaBonus(hasMedia); |
| 65 | + |
| 66 | + const newPoints = basePoints + thresholdBonusPoints + mediaBonusPoints; |
| 67 | + |
| 68 | + if (newPoints > 0) { |
| 69 | + const allBonuses = [ |
| 70 | + ...triggeredBonuses, |
| 71 | + ...(mediaTriggered ? [mediaTriggered] : []), |
| 72 | + ]; |
| 73 | + |
| 74 | + results.push({ |
| 75 | + activityId: activity._id, |
| 76 | + userId: activity.userId, |
| 77 | + activityType: activityType.name, |
| 78 | + metrics, |
| 79 | + oldPoints: 0, |
| 80 | + newPoints, |
| 81 | + }); |
| 82 | + |
| 83 | + if (!args.dryRun) { |
| 84 | + await ctx.db.patch(activity._id, { |
| 85 | + pointsEarned: newPoints, |
| 86 | + triggeredBonuses: allBonuses.length > 0 ? allBonuses : undefined, |
| 87 | + updatedAt: Date.now(), |
| 88 | + }); |
| 89 | + |
| 90 | + const prev = userPointAdjustments.get(activity.userId) ?? 0; |
| 91 | + userPointAdjustments.set(activity.userId, prev + newPoints); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Update participation totals |
| 97 | + if (!args.dryRun) { |
| 98 | + for (const [userId, pointsToAdd] of userPointAdjustments) { |
| 99 | + const participation = await ctx.db |
| 100 | + .query("userChallenges") |
| 101 | + .withIndex("userChallengeUnique", (q) => |
| 102 | + q.eq("userId", userId as any).eq("challengeId", args.challengeId) |
| 103 | + ) |
| 104 | + .first(); |
| 105 | + |
| 106 | + if (participation) { |
| 107 | + await ctx.db.patch(participation._id, { |
| 108 | + totalPoints: participation.totalPoints + pointsToAdd, |
| 109 | + updatedAt: Date.now(), |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + totalScanned: activities.length, |
| 117 | + totalFixed: results.length, |
| 118 | + dryRun: args.dryRun, |
| 119 | + fixes: results, |
| 120 | + }; |
| 121 | + }, |
| 122 | +}); |
0 commit comments