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
welcome2024findsWELCOME2024. - 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. AddsPhoenixKitReferrals.top_codes/1,list_recent_usage/1, and a:userassociation onReferralCodeUsage(via the existingused_by_uuidcolumn, no migration). - Admin nav: “Referrals” is now its own top-level section (
:admin_referrals, mirroringphoenix_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/overviewand/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_codeandsave_coderead 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". Everyphx-change/phx-submittherefore rebuilt the changeset from an empty map, silently discarding the generated code and anything typed, and (once:actionwas set) surfacing “can’t be blank” on every required field. -
use_code/2could crash user registration. Recording a use ran the code through the fullchangeset/2, which re-validatesmax_usesagainst the current system limit. Loweringmax_number_of_uses_per_codetherefore made every pre-existing code with a largermax_usesfail its usage update, and the{:ok, _} = update_code(...)match raised. The counter is now incremented with a conditionalUPDATEthat touches no changeset. -
The usage counter raced.
valid_for_use?/1readnumber_of_usesand a laterupdate_code/2wroteread + 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/2validatedexpiration_dateandmax_usesviaget_field/2, so a persisted past expiration — or amax_usespredating a lowered system limit — failed validation on every update, including one that only flippedstatus. Both now validate viaget_change/2and fire only when the field is actually being set. -
A user could use the same code repeatedly.
use_code/2now rejects a repeat with{:error, :already_used}, serialized by the row lock the counter increment takes. -
unique_constraint(:code)andforeign_key_constraint(:code_uuid)never matched. Core names thesephoenix_kit_referral_codes_code_uidxandfk_referral_code_usage_code_uuid, not Ecto’s defaults, so violations escaped asPostgrex.Errorinstead of changeset errors. Both now pass:name. -
list_valid_codes/0omitted never-expiring codes. Itsexpiration_date > nowfilter is NULL-false, contradictingvalid_for_use?/1, which treats anilexpiration as valid. -
Code lookup is now case-insensitive. A user typing
welcome2024forWELCOME2024was told the code was invalid. Codes are also trimmed and upcased on write, so they are stored in one canonical case. -
generate_random_code/0could not produce a repeated character. It sampled the alphabet withEnum.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_usesis no longer castable throughchangeset/2. The counter is owned byuse_code/2.use_code/2reports 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.