Skip to content

Record repeatable challenge completions in a ledger - #175

Merged
cjmellor merged 2 commits into
mainfrom
feat/v3-repeatable-challenge-completions
Jun 16, 2026
Merged

cjmellor merged 2 commits into
mainfrom
feat/v3-repeatable-challenge-completions

Conversation

@cjmellor

Copy link
Copy Markdown
Owner

What

Repeatable challenges are now counted correctly on the challenges leaderboard and by completedChallenges().

Previously, completing a repeatable challenge stamped completed_at on the challenge_user pivot and then immediately nulled it again to re-arm the next run. Because the challenges metric and completedChallenges() both keyed off a non-null completed_at, a repeatable challenge completed any number of times counted as 0. Non-repeatable challenges were unaffected.

How

A new challenge_completions ledger table records one row per completion and becomes the source of truth for the metric:

  • challenges leaderboard 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.
  • New challengeCompletions() relation exposes the raw per-completion rows (with timestamps) for anyone who wants the total-with-repeats count or a completion history.
  • Completions are recorded for both repeatable and non-repeatable challenges, keeping non-repeatable behaviour identical.

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.php gains two entries (publish the config to pick them up, or rely on the package defaults):

  • models.challenge_completion => LevelUp\Experience\Models\ChallengeCompletion::class
  • tables.challenge_completions => 'challenge_completions'

Notes

  • No public API removed; completedChallenges() keeps its BelongsToMany<Challenge> shape.
  • Verified across the bigint / ulid / uuid key-type matrix, with 100% line and type coverage held.

cjmellor added 2 commits June 16, 2026 20:13
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.
@cjmellor
cjmellor merged commit 3238d4d into main Jun 16, 2026
12 checks passed
@cjmellor
cjmellor deleted the feat/v3-repeatable-challenge-completions branch June 16, 2026 21:18
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant