The working directory (/tmp/tmp.sEVljh8sOg) is empty, and I found no copy of the project anywhere else on this machine. I couldn't read any of the files, so this plan is built from your notes. Every point that depends on code I haven't seen is tagged [check in code]. Once the repo is in this directory, I'll read those files and correct the plan before making any edits.
Plan message
1. How it works now (from your notes, not yet verified)
fees/models.py: Term has a due_date, and Student.balance() computes what's owed from Invoice and Payment. I don't yet know whether balance() is per term or across all terms, or where the parent's phone number is stored. [check in code]payments/mpesa.py and payments/c2b.py: handle_callback() (STK) and the C2B confirmation view create a Payment, so the balance changes as soon as that row is saved. C2B matches on admission number. Neither file needs to change for this feature.accounts/otp.py: send_otp() wraps africastalking.SMS.send, using AT_USERNAME and AT_API_KEY from .env. School in schools/models.py already has sms_sender_id and swahili_enabled, so the per-school settings exist.core/celery.py: Celery and Redis already run the receipt task, and beat has one scheduled task. I'll copy the existing task-module pattern. [check in code: where the receipt task lives]templates/fees/student_detail.html: this is the admin student page. Its view, and how it limits records to the admin's school, are in a file I haven't located. [check in code]
2. Plan
Each step should leave the app working. I'll run python manage.py test after every step.
- Reminder record and migration (
fees/models.py, new migration).- Add a
FeeReminder model with: school, student, term, sequence (1 or 2), status (pending/sent/skipped_paid/failed), message text, Africa's Talking message id, and sent_at. - A unique constraint on (student, term, sequence) makes it impossible for the database to hold more than 2 reminders per term, even if a task runs twice.
- Tests: full suite, plus
makemigrations --check and migrate against a fresh database.
- Message builder (new
fees/reminders.py).- This is a pure function that builds the English text (and the Swahili text when
swahili_enabled is on) in your example's format, shortening the name to "Amani K." - It guarantees 160 characters or fewer by shortening the name or dropping "Thank you." when needed. It uses only GSM-7 characters, because a single accent switches the SMS to UCS-2, where the limit is 70 characters.
- Tests: new unit tests for the length limit, a long school name, Swahili, and number formatting ("KES 12,500").
- Send task with a balance check at send time (
fees/tasks.py or wherever the receipt task lives; fees/reminders.py).- The task locks the reminder row, checks that it's still pending, and recalculates
Student.balance() immediately before calling Africa's Talking. - If the balance is 0 or less, it records
skipped_paid and sends nothing. That covers an M-Pesa payment that lands between scheduling and sending. - SMS goes through the same Africa's Talking client setup as
send_otp(), using the school's sms_sender_id. send_otp() itself stays untouched. - The task never auto-retries after Africa's Talking has been called, so a parent can't get the same SMS twice. A failure is recorded as
failed. - Tests: new tests with Africa's Talking mocked the same way as
accounts/tests/test_otp.py. One test creates a Payment after scheduling and asserts that nothing is sent.
- Daily scheduler and beat entry (
fees/tasks.py, core/celery.py).- Runs once a day in the morning, Nairobi time. For each term with a due date, it schedules reminder 1 when today is at least the due date plus 3 days and no reminder 1 exists. It schedules reminder 2 when reminder 1 was sent at least 7 days ago and no reminder 2 exists.
- Every query is filtered by school.
- The whole feature sits behind a
FEE_REMINDERS_ENABLED environment variable that defaults to off, so deploying doesn't send anything until you switch it on. - Tests:
- day 2: nothing; day 3: reminder 1
- day 9: nothing; day 10: reminder 2
- day 20: nothing more
- a paid-up student is skipped
- a second school's students are untouched
- Admin student page (the student detail view [check in code],
templates/fees/student_detail.html).- Adds a small "SMS reminders" table showing date, number, status and message. It's read-only and filtered to this student and the admin's school.
- Tests: a view test checks that the list appears, and that an admin from another school can't see it.
- Final check: full suite,
manage.py check, and makemigrations --check. I'll also confirm that the deploy script runs migrate and restarts celery beat. [check in code]
No new packages are needed: Celery, Redis and the Africa's Talking SDK already cover this. Environment variables: the existing AT_USERNAME and AT_API_KEY, plus the new FEE_REMINDERS_ENABLED=true once you're ready to send.
3. Risks to "must keep working"
- STK push and Daraja callback; C2B Paybill matching: I won't edit
payments/mpesa.py or payments/c2b.py. The reminder task only reads Payment and Student.balance(). The existing payments tests must stay green. - Login OTP via Africa's Talking:
send_otp() is unchanged, and I'll reuse its setup rather than rewrite it. accounts/tests/test_otp.py stays as it is and must pass. Reminder SMS run in Celery, so they can't slow down an OTP request. - PDF receipts: these share the Celery workers. The reminder tasks are small and run once a day, and the receipt task code isn't touched. The existing receipt tests must pass.
- Term balance Excel report: this reads
Invoice/Payment/balance(), which I don't change. Adding a new model doesn't alter the report's queries. - Multi-school separation: each reminder row stores its school. The scheduler filters by
term.school, and the student page filters by the admin's school. Steps 4 and 5 include tests with two schools. - Mass send on first deploy (a new risk): terms whose due dates passed long ago would all qualify at once. The off-by-default flag and the backfill rule (assumption 3) stop that.
4. Questions or assumptions
- Swahili: a combined English and Swahili message can't fit in 160 characters. I assume Swahili goes as a separate SMS straight after the English one, and that the pair counts as a single reminder towards the limit of 2.
- Paybill number in the message: is it the single
MPESA_SHORTCODE from .env, or does each school have its own? I'll use whatever payments/c2b.py uses. [check in code] - Old terms: I assume reminders only go out for terms that are currently running, and only up to 14 days after the due date. Anything older is skipped, so going live doesn't message parents about old debts.
Files I could not read: all of them. The working directory is empty. That includes fees/models.py, payments/mpesa.py, payments/c2b.py, accounts/otp.py, schools/models.py, core/celery.py, templates/fees/student_detail.html, the student detail view, the receipt task module, the tests, and the deploy script.
I'll stop here and wait for your go. Please also add the project to /tmp/tmp.sEVljh8sOg or point me to it, so I can check the tagged points before step 1.