Skip to content

Referrals 0.4.0: Overview page, own admin section and a working code form

Referrals 0.4.0 gives referrals their own section in the admin with a new Overview page, fixes the new and edit code form, and makes code limits, expiry and case handling behave as an admin would expect.

Overview page and its own section

Referrals is now a top-level admin section with two pages, Overview and Codes, instead of a tab under Users. The addresses change to /admin/referral-codes/overview and /admin/referral-codes/codes. The old /admin/users/referral-codes addresses are gone, so update any bookmarks.

The read-only Overview page shows the same four counters as the code list, a Top Referrals list of the five most used codes, and Recent Activity: the last ten uses across all codes, with who used which code and when.

The code form saves what you type

The new and edit form read its fields under the wrong name, so every change was dropped: a generated code and anything typed vanished, and required fields showed “can’t be blank”. The form now keeps and saves its fields. Maximum Uses starts filled with the site default (100 unless changed). Generate now makes sure no other code already uses the result.

Codes behave as expected

  • Codes are stored in capitals, and looking one up ignores case, so welcome2024 finds WELCOME2024.
  • The same person can no longer use one code twice.
  • Two sign-ups on a code with one use left can no longer both succeed: the counter goes up in one step that rechecks status, expiry and the limit.
  • Lowering the site-wide maximum uses no longer breaks registration with older codes that have a higher limit.
  • Expired codes, and codes above a lowered limit, can be edited and switched off again.
  • Codes that never expire now count as valid in the list of valid codes.
  • A duplicate code shows a form error instead of crashing.
  • Generated codes can now repeat a character, which widens the set of possible codes.

An inactive code is reported as inactive before expiry or the usage limit is checked.

Upgrading

No migration: the new link from a use to its user reuses an existing column. Update the dependency and point bookmarks at the new admin addresses. Codes saved with lowercase letters before this release are still found, since lookups ignore case.

Full release notes: Referrals 0.4.0

0.4.0 · 11 July 2026

Added

  • Referral program overview page (/admin/referral-codes/overview, PhoenixKitReferrals.Web.Overview) — a read-only admin dashboard with system-wide stats, a top-codes-by-usage leaderboard, and a recent-activity feed across every code. Adds PhoenixKitReferrals.top_codes/1, list_recent_usage/1, and a :user association on ReferralCodeUsage (via the existing used_by_uuid column, no migration).
  • Admin nav: “Referrals” is now its own top-level section (:admin_referrals, mirroring phoenix_kit_ai‘s :admin_ai), no longer nested under Users, and its URLs no longer live under /admin/users/* either — Overview and Codes (formerly labeled “Referrals”) now live at /admin/referral-codes/overview and /admin/referral-codes/codes (+ /new, /edit/:code_uuid), true siblings so neither prefix-matches the other’s nav tab. Deliberate: unlike user management, this module is meant to eventually be reachable by non-Owner/Admin roles holding only the "referrals" permission, so it can’t live inside a section (or URL namespace) gated on full user-management access.
  • New-code form pre-fills “Maximum Uses” with the system default (get_max_uses_per_code/0, 100 unless configured otherwise) instead of leaving a required field blank — still freely editable.

Fixed

  • The new/edit form silently dropped every typed field. validate_code and save_code read submitted params under the key "referrals", but the form’s actual param key — derived from the %PhoenixKitReferrals{} changeset’s struct module — is "phoenix_kit_referrals". Every phx-change/phx-submit therefore rebuilt the changeset from an empty map, silently discarding the generated code and anything typed, and (once :action was set) surfacing “can’t be blank” on every required field.

  • use_code/2 could crash user registration. Recording a use ran the code through the full changeset/2, which re-validates max_uses against the current system limit. Lowering max_number_of_uses_per_code therefore made every pre-existing code with a larger max_uses fail its usage update, and the {:ok, _} = update_code(...) match raised. The counter is now incremented with a conditional UPDATE that touches no changeset.

  • The usage counter raced. valid_for_use?/1 read number_of_uses and a later update_code/2 wrote read + 1, so two concurrent sign-ups on a code with one slot left both succeeded. The increment is now atomic and re-checks status, expiration, and the limit in the database.

  • Expired codes could not be edited or deactivated. changeset/2 validated expiration_date and max_uses via get_field/2, so a persisted past expiration — or a max_uses predating a lowered system limit — failed validation on every update, including one that only flipped status. Both now validate via get_change/2 and fire only when the field is actually being set.

  • A user could use the same code repeatedly. use_code/2 now rejects a repeat with {:error, :already_used}, serialized by the row lock the counter increment takes.

  • unique_constraint(:code) and foreign_key_constraint(:code_uuid) never matched. Core names these phoenix_kit_referral_codes_code_uidx and fk_referral_code_usage_code_uuid, not Ecto’s defaults, so violations escaped as Postgrex.Error instead of changeset errors. Both now pass :name.

  • list_valid_codes/0 omitted never-expiring codes. Its expiration_date > now filter is NULL-false, contradicting valid_for_use?/1, which treats a nil expiration as valid.

  • Code lookup is now case-insensitive. A user typing welcome2024 for WELCOME2024 was told the code was invalid. Codes are also trimmed and upcased on write, so they are stored in one canonical case.

  • generate_random_code/0 could not produce a repeated character. It sampled the alphabet with Enum.take_random/2 (without replacement), cutting the keyspace from 32⁵ to 32·31·30·29·28.

Added

  • generate_unique_code/1, which retries until it finds a code no existing record holds. The admin form’s “generate” button now uses it.

Changed

  • number_of_uses is no longer castable through changeset/2. The counter is owned by use_code/2.
  • use_code/2 reports an inactive code as {:error, :code_inactive} before considering expiry or the usage limit, matching the precedence core’s registration LiveView already used for its own messages.

Referrals 0.4.0. Install or update: {:phoenix_kit_referrals, "~> 0.4"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.