Referral Status Update Implementation - Testing Guide
Source: `docs/operations/REFERRAL_STATUS_UPDATE_TESTING.md`View on GitHub
Referral Status Update Implementation - Testing Guide
What Was Implemented
1. API Route: /api/referrals/[id]/update-status
- Location:
app/api/referrals/[id]/update-status/route.ts - Method: POST
- Features:
- Validates status values
- Checks user authentication
- Verifies permissions (admin, caseworker org, or provider org)
- Updates referral status and timestamps
- Creates activity log entries
- Returns success/error responses
2. Client Component: ReferralStatusButtons
- Location:
app/components/ReferralStatusButtons.tsx - Features:
- Four status update buttons: In Review, Accept, Deny, Complete
- Loading states during API calls
- Success/error message display
- Disables buttons for current status
- Auto-refreshes page data after successful update
3. Integration
- Location:
app/(crm)/referrals/[id]/page.tsx - Replaced static buttons with interactive
ReferralStatusButtonscomponent - Maintains existing permission logic (
canUpdateStatus)
Permissions
Users can update referral status if they are:
- Admin - Can update any referral
- Caseworker Organization - Members of the organization that created the referral
- Provider Organization - Members of the organization that owns the resource
Status Workflow
Available statuses:
draft- Initial statesubmitted- Referral submittedin_review- Provider reviewingaccepted- Provider accepted referraldenied- Provider denied referralcompleted- Service completedcancelled- Referral cancelled
How to Test
Prerequisites
- Ensure your database has the Resource Library migration applied
- Have at least one test organization, resource, household, and referral
- Have a user account with appropriate permissions
Step 1: Create Test Data (if not already done)
-- Create organization
INSERT INTO organizations (name, org_type, city, state)
VALUES ('Test Provider', 'service_provider', 'Medford', 'Oregon');
-- Link your user to the organization
UPDATE profiles
SET org_id = (SELECT id FROM organizations WHERE name = 'Test Provider'),
role = 'org_admin'
WHERE email = 'your-email@example.com';
Step 2: Login and Navigate
- Go to
http://localhost:3000/crm/login - Login with your credentials
- Navigate to
/referrals - Click on any referral to view details
Step 3: Test Status Updates
On the referral detail page, you should see the "Update Status" section with four buttons:
-
Test "Mark as In Review"
- Click the button
- Button should show "Updating..." while processing
- Success message should appear
- Page should refresh
- Status badge at top should update to "in_review"
- Activity timeline should show new status change entry
-
Test "Accept Referral"
- Click the "Accept Referral" button
- Verify same success flow as above
- Status should change to "accepted"
-
Test "Deny Referral"
- Click the "Deny Referral" button
- Status should change to "denied"
-
Test "Mark as Completed"
- Click the "Mark as Completed" button
- Status should change to "completed"
Step 4: Verify Database Changes
After each status update, check the database:
-- Verify referral was updated
SELECT id, status, status_updated_at, status_updated_by
FROM referrals
WHERE id = 'your-referral-id';
-- Verify activity log was created
SELECT *
FROM referral_activities
WHERE referral_id = 'your-referral-id'
ORDER BY created_at DESC;
Step 5: Test Permission Restrictions
-
Test with different user types:
- Login as an admin → Should see buttons
- Login as caseworker from same org → Should see buttons
- Login as provider org member → Should see buttons
- Login as user from different org → Should NOT see buttons
-
Test disabled states:
- When a referral is already "accepted", the "Accept Referral" button should be disabled
- Same for other statuses
Step 6: Test Error Scenarios
-
Invalid status (via API directly):
curl -X POST http://localhost:3000/api/referrals/[id]/update-status \ -H "Content-Type: application/json" \ -d '{"status":"invalid_status"}'Should return 400 error
-
Unauthorized access:
- Try accessing referral from organization you don't belong to
- Should return 403 error
-
Nonexistent referral:
- Try updating a fake UUID
- Should return 404 error
Expected Behavior
Success Flow
- User clicks button
- Button shows "Updating..." text
- Button is disabled
- API call is made
- Success message appears in green box
- Page refreshes automatically
- Updated status appears in status badge
- New activity appears in timeline
- Current status text updates at bottom
Error Flow
- User clicks button
- Button shows "Updating..."
- API returns error
- Red error message box appears
- Button re-enables
- User can retry
Troubleshooting
Buttons Don't Appear
- Check user has correct permissions
- Verify
canUpdateStatusevaluates to true - Check browser console for errors
Status Doesn't Update
- Check browser Network tab for API errors
- Verify database RLS policies allow the update
- Check server logs for error messages
Activity Log Not Created
- Check if referral_activities table exists
- Verify user permissions on that table
- Activity log errors don't fail the request
API Response Examples
Success
{
"success": true,
"message": "Status updated successfully"
}
Error - Unauthorized
{
"error": "Unauthorized - not logged in"
}
Error - Forbidden
{
"error": "Forbidden - insufficient permissions"
}
Error - Invalid Status
{
"error": "Invalid status"
}
Files Modified
- ✅
app/api/referrals/[id]/update-status/route.ts(created) - ✅
app/components/ReferralStatusButtons.tsx(created) - ✅
app/(crm)/referrals/[id]/page.tsx(modified)
Next Steps (Future Enhancements)
- Add Confirmation Dialogs: Especially for "Deny" and "Completed"
- Add Notes Field: Allow users to add notes when updating status
- Add Denial Reason: Required field when denying referrals
- Email Notifications: Notify relevant parties when status changes
- Validation Rules: Prevent invalid status transitions (e.g., can't go from "completed" back to "in_review")
- Optimistic Updates: Update UI immediately before API call completes
- Undo Functionality: Allow reverting status changes
- Batch Updates: Update multiple referrals at once
Use links in each imported doc to open its source.