|
| 1 | +# Applicant Dual-Status Issue - Resolution |
| 2 | + |
| 3 | +## Issue Summary |
| 4 | +Users with `application_status = "passed"` were appearing in both the **In-House** page and **Applicants** page simultaneously, causing confusion about their actual status. |
| 5 | + |
| 6 | +**Specific Case:** Johnrick Rabara (jrabarav2@gmail.com) appeared in both locations. |
| 7 | + |
| 8 | +## Root Cause Analysis |
| 9 | + |
| 10 | +### 1. Database Structure |
| 11 | +The system has two related tables: |
| 12 | +- `codev` table - Main user table with `application_status` field |
| 13 | +- `applicant` table - Supplementary table with onboarding data (linked via `codev_id`) |
| 14 | + |
| 15 | +### 2. The Problem |
| 16 | +When a user's status changed to "passed": |
| 17 | +- ✅ Their `codev.application_status` was updated to "passed" |
| 18 | +- ❌ Their `applicant` record remained in the database |
| 19 | +- ❌ Cached data wasn't cleared |
| 20 | + |
| 21 | +### 3. Why It Appeared in Both Pages |
| 22 | + |
| 23 | +**In-House Page** (`/home/in-house`): |
| 24 | +```typescript |
| 25 | +// Query: application_status = "passed" |
| 26 | +getCodevs({ filters: { application_status: "passed" } }) |
| 27 | +``` |
| 28 | +✅ Correctly showed users with "passed" status |
| 29 | + |
| 30 | +**Applicants Page** (`/home/applicants`): |
| 31 | +```typescript |
| 32 | +// Query: application_status != "passed" |
| 33 | +.not("application_status", "eq", "passed") |
| 34 | +``` |
| 35 | +✅ Query was correct - should exclude "passed" users |
| 36 | +❌ BUT: Stale cached data was being served |
| 37 | +❌ AND: Orphaned applicant records existed for passed users |
| 38 | + |
| 39 | +## Solution Implemented |
| 40 | + |
| 41 | +### 1. ✅ Cache Clearing |
| 42 | +**Script:** `scripts/clear-cache.ts` |
| 43 | +- Clears Redis cache for applicants, in-house, and dashboard |
| 44 | +- **Usage:** `pnpm cache:clear` |
| 45 | + |
| 46 | +```bash |
| 47 | +pnpm cache:clear |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. ✅ Database Cleanup |
| 51 | +**Script:** `scripts/archive-passed-applicants.ts` |
| 52 | +- Deleted 173 orphaned applicant records for users with "passed" status |
| 53 | +- Includes Johnrick Rabara's applicant record |
| 54 | +- **Usage:** `pnpm applicants:cleanup` |
| 55 | + |
| 56 | +```bash |
| 57 | +pnpm applicants:cleanup |
| 58 | +``` |
| 59 | + |
| 60 | +### 3. ✅ Automatic Prevention (Database Trigger) |
| 61 | +**Migration:** `supabase/migrations/20260220_auto_archive_applicant_on_passed.sql` |
| 62 | + |
| 63 | +Created database trigger that automatically: |
| 64 | +1. Archives applicant data to `applicant_archive` table when status changes to "passed" |
| 65 | +2. Deletes the original applicant record |
| 66 | +3. Prevents future dual-status issues |
| 67 | + |
| 68 | +**How it works:** |
| 69 | +```sql |
| 70 | +-- Trigger fires on UPDATE of application_status |
| 71 | +CREATE TRIGGER trigger_archive_applicant_on_passed |
| 72 | + AFTER UPDATE OF application_status ON codev |
| 73 | + FOR EACH ROW |
| 74 | + EXECUTE FUNCTION archive_applicant_on_passed(); |
| 75 | +``` |
| 76 | + |
| 77 | +## Verification |
| 78 | + |
| 79 | +### Before Fix |
| 80 | +```sql |
| 81 | +SELECT COUNT(*) FROM applicant a |
| 82 | +JOIN codev c ON a.codev_id = c.id |
| 83 | +WHERE c.application_status = 'passed'; |
| 84 | +-- Result: 173 orphaned records |
| 85 | +``` |
| 86 | + |
| 87 | +### After Fix |
| 88 | +```sql |
| 89 | +SELECT COUNT(*) FROM applicant a |
| 90 | +JOIN codev c ON a.codev_id = c.id |
| 91 | +WHERE c.application_status = 'passed'; |
| 92 | +-- Result: 0 records ✅ |
| 93 | +``` |
| 94 | + |
| 95 | +## Testing the Fix |
| 96 | + |
| 97 | +1. **Clear the cache:** |
| 98 | + ```bash |
| 99 | + pnpm cache:clear |
| 100 | + ``` |
| 101 | + |
| 102 | +2. **Visit the applicants page:** |
| 103 | + - Navigate to `/home/applicants` |
| 104 | + - Verify Johnrick Rabara does NOT appear |
| 105 | + |
| 106 | +3. **Visit the in-house page:** |
| 107 | + - Navigate to `/home/in-house` |
| 108 | + - Verify Johnrick Rabara DOES appear |
| 109 | + |
| 110 | +## Files Modified/Created |
| 111 | + |
| 112 | +### New Scripts |
| 113 | +- ✅ `scripts/clear-cache.ts` - Cache clearing utility |
| 114 | +- ✅ `scripts/archive-passed-applicants.ts` - One-time cleanup script |
| 115 | + |
| 116 | +### New Migrations |
| 117 | +- ✅ `supabase/migrations/20260220_auto_archive_applicant_on_passed.sql` - Auto-archiving trigger |
| 118 | +- ✅ `supabase/migrations/20260220_add_job_link_to_client_outreach.sql` - Client tracker job link |
| 119 | + |
| 120 | +### Updated Files |
| 121 | +- ✅ `package.json` - Added `cache:clear` and `applicants:cleanup` scripts |
| 122 | +- ✅ `constants/settings.ts` - Fixed client tracker icon (icon-chart → icon-clients) |
| 123 | +- ✅ `app/home/admin-controls/client-tracker/_components/ClientTrackerContent.tsx` - Dark mode fixes |
| 124 | + |
| 125 | +## Future Prevention |
| 126 | + |
| 127 | +### When Accepting an Applicant |
| 128 | +The database trigger will automatically handle cleanup, but to be safe: |
| 129 | + |
| 130 | +1. **Update application status:** |
| 131 | + ```typescript |
| 132 | + await supabase |
| 133 | + .from("codev") |
| 134 | + .update({ application_status: "passed" }) |
| 135 | + .eq("id", codevId); |
| 136 | + ``` |
| 137 | + |
| 138 | +2. **Clear cache (optional - recommended):** |
| 139 | + ```bash |
| 140 | + pnpm cache:clear |
| 141 | + ``` |
| 142 | + |
| 143 | +3. **The trigger handles the rest automatically** - No manual cleanup needed! |
| 144 | + |
| 145 | +## Additional Improvements Made |
| 146 | + |
| 147 | +### Client Tracker Enhancements |
| 148 | +1. ✅ Made plus icon smaller (40px) |
| 149 | +2. ✅ Added `job_link` field for job postings |
| 150 | +3. ✅ Fixed dark mode text colors for admin names and View button |
| 151 | +4. ✅ Created migration to add `job_link` column to database |
| 152 | + |
| 153 | +## Available Commands |
| 154 | + |
| 155 | +```bash |
| 156 | +# Clear all cache (applicants, in-house, dashboard) |
| 157 | +pnpm cache:clear |
| 158 | + |
| 159 | +# Clean up orphaned applicant records (one-time, already run) |
| 160 | +pnpm applicants:cleanup |
| 161 | +``` |
| 162 | + |
| 163 | +## Summary |
| 164 | + |
| 165 | +**Issue:** Users appearing in both Applicants and In-House pages |
| 166 | +**Cause:** Stale cache + orphaned applicant records |
| 167 | +**Solution:** |
| 168 | +- Cache clearing script ✅ |
| 169 | +- Database cleanup script ✅ |
| 170 | +- Automatic prevention via database trigger ✅ |
| 171 | + |
| 172 | +**Status:** ✅ **RESOLVED** |
| 173 | + |
| 174 | +All 173 affected users (including Johnrick Rabara) have been cleaned up, and future issues are prevented by the database trigger. |
0 commit comments