Referrals
The referral programme. Guardians and guests get a four-character code, earn a commission when a family they referred is admitted, and staff record the payouts.
Read this page when you change the referral programme: the code a family shares, when a commission is earned, or how staff pay it out. The module is apps/server/src/app/referral, read at API fbb63d51. Nothing here was run against a live app.
Both controllers carry @RequireFeature(Feature.REFERRALS), so every route is refused when the school's referrals flag is off. See Feature flags.
How a referral becomes a payout
- Staff set a commission rule. Until a rule exists, nobody can join.
- A guardian or guest joins and gets a code.
- A family uses the code, either from a referral link or by typing it on their admission form.
- When that family's application is admitted, the referrer earns a commission. Each application earns at most one.
- Staff pay the referrer outside Naalya, then record the payout with proof. The commissions it covers are marked paid.
The commission rule
PUT /referrals/settings (saveReferralSettings) stores one rule per school, in referral_setting. commissionType is one of three values.
| Type | Commission per learner |
|---|---|
fixed | commissionValue, a UGX amount, rounded to a whole shilling |
percent | commissionValue per cent of the application's admission fee, rounded. A percentage over 100 is refused with A percentage cannot be more than 100. With no admission fee, the amount is 0 |
anonymous | 0. The value is stored as 0 |
With applyToUnpaid: true, the save recalculates every earned commission that is not paid yet, inside the same transaction. Saving an anonymous rule always recalculates, so unpaid amounts become 0. Paid commissions never change.
GET /referrals/settings/preview (previewReferralSettings) takes a proposed type and value, and returns how many unpaid commissions exist, their total now, and their total under the proposal. It writes nothing. GET /referrals/settings (getReferralSettings) returns the rule and its wording, or nulls when there is none.
Codes and joining
POST /referrals/me (joinReferralProgram) is for guardians and guests. It returns the caller's existing membership if they already joined. Otherwise it refuses with The school has not set up referrals yet when there is no rule, then creates a referrer row with a new code.
A code is four characters from the upper-case half of SLUG_CHARSET, which has no 0, O, 1, I, or L, so it survives being read aloud or typed on a basic phone (referral-code.ts). On a clash the service tries again, up to five times, then refuses with 409 Could not make a referral code. Try again.. Codes are matched after removing spaces and upper-casing.
PATCH /referrals/me/payout-details (updateMyReferralPayoutDetails) sets where the money goes: mtn, airtel, or bank, with the account number and the name on the account. A bank name is kept only for bank. A caller who has not joined gets 404 Join the referral program first.
Crediting a family
A code reaches an application in three ways.
- A referral link. A signed-in family that opens it calls
POST /referrals/claim(claimReferral). If the caller's account has no referrer yet, it gets this one, and so does each of their draft applications that has none. An unknown code, or an account that already has a referrer, returns{ "claimed": false }with no error. The service does not check that the code belongs to someone else. - The code box on the form.
POST /referrals/me/applications/:applicationId/code(addApplicationReferralCode) credits the caller's own draft application, and their account if it has no referrer. A sent form is refused withThe referral code can only be added before the form is sent, and an application that has a code already withThis application already has a referral code. An unknown code getsWe could not find that code. If the form was sent or given a code in another tab meanwhile, the write refuses with 409This application changed. Reload and try again.. - Staff.
PUT /referrals/applications/:applicationId(setApplicationReferral) sets, changes, or removes an application's referrer by code, at any status. An unpaid commission moves to the new referrer, or is deleted when the referrer is removed. Once the commission is paid, the change is refused withCommission already paid.
When a commission is earned
ReferralEarningService.check records the commission. It never throws: a failure is logged as Failed to check a referral commission. An application earns when it has a referrer, no commission yet, the school has a rule, and either of these is true:
- It is
admittedorenrolled. Admitting already requires the fee. - It is
approved, its admission fee charge is fully covered by payments, waivers, and credits, and the bursary feature is on for its campus.
The check runs after approve and admit in AdmissionApplicationService, after each of the three crediting routes above, and on three ledger events: paymentSettled, chargeAdjusted, and creditApplied (ReferralLedgerListener). A ledger event checks every approved, referred application of that account that has not earned yet.
The insert uses ON CONFLICT (admission_application_id) DO NOTHING, so two checks at once still record one commission. The earning queries are plain SQL; the comment in referral.sql.ts says this is so the check can also run in the worker.
Paying a referrer
Staff pay outside Naalya, then record it. POST /referrals/referrers/:referrerId/payouts (createReferralPayout) takes the commission ids and total the staff member was shown, and an uploaded proof.
The service locks the referrer's unpaid commissions (FOR UPDATE) and refuses with 409 This changed. Reload and check the amount again. unless the ids and the total match exactly. It also refuses when the referrer has no payout details (This referrer has not added how they get paid), when nothing is owed (Nothing is owed to this referrer), and when the proof belongs to another school or is already attached to something (Upload the proof of payment again). The payout keeps a copy of the payout details as they were when it was paid.
GET /referrals/payouts/:payoutId/proof and, for the referrer, GET /referrals/me/payouts/:payoutId/proof return a short-lived link to the proof.
Read routes for staff and families
| Route | Operation id | Who | Returns |
|---|---|---|---|
GET /referrals/referrers | listReferrers | list on referral | Each referrer with their code, people referred, learners earned, owed, and paid |
GET /referrals/referrers/:referrerId | getReferrer | read on referral | Payout details, each learner and where its commission stands, past payouts |
GET /referrals/applications/:applicationId | getApplicationReferral | read on referral | Who referred the application, and its commission |
GET /referrals/me | getMyReferral | Guardian or guest | The rule in words, and the caller's code, details, and totals once joined |
GET /referrals/me/people | listMyReferredPeople | Guardian or guest | Each person referred, their enquiries, and each application's state |
GET /referrals/me/payouts | listMyReferralPayouts | Guardian or guest | The caller's payouts, newest first |
An application's state, as a referrer sees it, comes from applicationState in referral-format.ts: filling, reviewing, awaiting_fee, admitted, earned, paid, or closed.
Writes are audited on resource referral: the rule, a payout, an application's referrer, and payout details. Each write also sends the REFERRAL_CHANGE realtime invalidation.
In the Education Hub
Guardians use /guardian/referrals and its payment details page. Staff screens are components/referrals/staff/referral-program.page.tsx and referrer-detail.page.tsx, with the rule in surfaces/referral/commission.surface.tsx. These were found by name at Hub 9e84f4c3, not read line by line.