Repository navigation
Record repeatable challenge completions in a ledger - #175
Merged
Merged
Conversation
Repeatable challenges re-arm by nulling the challenge_user pivot's completed_at, so every repeatable completion was invisible to the challenges leaderboard metric and to completedChallenges() — a repeatable completed N times scored 0. Add a challenge_completions ledger (one row per completion) as the source of truth for the challenges metric: - completeChallenge() records a row per completion (repeatable and non-repeatable), before the re-arm reset - ChallengeMetric score/windowed/constrain read the ledger - completedChallenges() stays distinct (challenges with >=1 ledger row); new challengeCompletions() relation exposes raw events - migration backfills one row per existing completed pivot so existing installs keep their counts on upgrade Verified across the bigint/ulid/uuid key-type matrix; 100% line + type coverage held.
Address two findings from code review: - recordCompletion() typed its $completedAt as the concrete Illuminate\Support\Carbon, which a host app running global immutable dates (Date::use(CarbonImmutable::class)) would violate — now() would return a CarbonImmutable and throw a TypeError. Widen to Carbon\CarbonInterface, matching the Windowable contract and the rest of the package. - The challenge_completions ledger only indexed completed_at, but the leaderboard metric's correlated subqueries filter by the user FK first (the all-time COUNT(*) has no completed_at predicate at all). Replace the single-column index with a composite (user_id, completed_at) that serves both the all-time and windowed reads. No behaviour change. 100% line + type coverage held across the bigint/ulid/uuid matrix.
midhlajnazar
pushed a commit
to midhlajnazar/level-up
that referenced
this pull request
Oct 20, 2026
…ills The challenge_completions ledger (cjmellor#175) merged after the v3 docs roll-up, so it was undocumented in the README, the v2->v3 UPGRADE guide, and the Boost skills. This pass closes that gap and corrects the upgrade path for the shallow config-merge of the models map. README: - Document the challengeCompletions() relation and repeatable-challenge ledger semantics; clarify the challenges metric counts completions (not distinct challenges); sync the published-config snippet (models.challenge_completion, models.cohort_user, tables.challenge_completions). UPGRADE (v2 -> v3): - List create_challenge_completions_table and its auto-backfill. - Add a "re-publish/merge config" step covering the models-map shallow-merge footgun (new model bindings resolve to null on a stale published config; tables fall back to defaults; the leaderboard block merges in fresh). - Add a scheduler snippet for level-up:snapshot-boards / league-rollover. level-up-development skill: - Fix the challenges metric description (reads the challenge_completions ledger, not the pivot completed_at); add a Completion Ledger section; add the four missing models keys (multiplier_user/_tier, challenge_completion, cohort_user); add "challenges" to the description. level-up-upgrade-v3 skill: - Add a model-bindings step for published configs and the challenge_completions migration + backfill to the publish step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Repeatable challenges are now counted correctly on the
challengesleaderboard and bycompletedChallenges().Previously, completing a repeatable challenge stamped
completed_aton thechallenge_userpivot and then immediately nulled it again to re-arm the next run. Because the challenges metric andcompletedChallenges()both keyed off a non-nullcompleted_at, a repeatable challenge completed any number of times counted as 0. Non-repeatable challenges were unaffected.How
A new
challenge_completionsledger table records one row per completion and becomes the source of truth for the metric:challengesleaderboard metric now counts ledger rows, so each completion counts — including repeats — and periodic (day/week/month) boards window correctly on completion time.completedChallenges()still returns the distinct set of challenges you've completed (so a repeatable appears once), but now reflects repeatables that have been re-armed.challengeCompletions()relation exposes the raw per-completion rows (with timestamps) for anyone who wants the total-with-repeats count or a completion history.Upgrading
Publish and run migrations as usual. The new migration backfills one ledger row for every challenge that is currently in a completed state, so existing installs keep their challenge counts on upgrade. (History for repeatables that were already re-armed before this release can't be recovered — it was never stored.)
New config
config/level-up.phpgains two entries (publish the config to pick them up, or rely on the package defaults):models.challenge_completion => LevelUp\Experience\Models\ChallengeCompletion::classtables.challenge_completions => 'challenge_completions'Notes
completedChallenges()keeps itsBelongsToMany<Challenge>shape.