freealberta/influence/BUGFIX_VERIFICATION_FIELDS.md

140 lines
5.3 KiB
Markdown

# Bug Fix: Response Verification Data Not Populating
## Issue Description
Verification fields were not being saved to the database when submitting responses through the Response Wall. The fields (`Representative Email`, `Verification Token`, etc.) were being created as `null` values.
## Root Causes
### 1. Missing Field Mapping in NocoDB Service
**File:** `app/services/nocodb.js`
**Problem:** The `createRepresentativeResponse()` and `updateRepresentativeResponse()` functions were missing the mappings for the new verification fields.
**Solution:** Added proper Column Title mappings following NocoDB conventions:
```javascript
// Added to createRepresentativeResponse
'Representative Email': responseData.representative_email,
'Verification Token': responseData.verification_token,
'Verification Sent At': responseData.verification_sent_at,
'Verified At': responseData.verified_at,
'Verified By': responseData.verified_by,
// Added to updateRepresentativeResponse
if (updates.representative_email !== undefined) data['Representative Email'] = updates.representative_email;
if (updates.verification_token !== undefined) data['Verification Token'] = updates.verification_token;
if (updates.verification_sent_at !== undefined) data['Verification Sent At'] = updates.verification_sent_at;
if (updates.verified_at !== undefined) data['Verified At'] = updates.verified_at;
if (updates.verified_by !== undefined) data['Verified By'] = updates.verified_by;
```
### 2. Representative Email Coming as Array
**File:** `app/public/js/response-wall.js`
**Problem:** The Represent API sometimes returns email as an array `["email@example.com"]` instead of a string. This caused the form to submit an array value.
**Solution:** Added array handling in `handleRepresentativeSelect()`:
```javascript
// Handle email being either string or array
const emailValue = Array.isArray(rep.email) ? rep.email[0] : rep.email;
document.getElementById('representative-email').value = emailValue;
```
### 3. Anonymous Checkbox Value as String
**File:** `app/controllers/responses.js`
**Problem:** HTML checkboxes send the value "on" when checked, not boolean `true`. This was being stored as the string "on" in the database.
**Solution:** Added proper checkbox normalization:
```javascript
// Normalize is_anonymous checkbox value
const isAnonymous = responseData.is_anonymous === true ||
responseData.is_anonymous === 'true' ||
responseData.is_anonymous === 'on';
```
### 4. Backend Email Handling
**File:** `app/controllers/responses.js`
**Problem:** The backend wasn't handling the case where `representative_email` might come as an array from the form.
**Solution:** Added array handling in backend:
```javascript
// Handle representative_email - could be string or array from form
let representativeEmail = responseData.representative_email;
if (Array.isArray(representativeEmail)) {
representativeEmail = representativeEmail[0]; // Take first email if array
}
representativeEmail = representativeEmail || null;
```
Also added support for "on" value in verification checkbox:
```javascript
const sendVerification = responseData.send_verification === 'true' ||
responseData.send_verification === true ||
responseData.send_verification === 'on';
```
## Files Modified
1.`app/services/nocodb.js` - Added verification field mappings
2.`app/public/js/response-wall.js` - Fixed email array handling
3.`app/controllers/responses.js` - Fixed checkbox values and email array handling
## Testing Performed
### Before Fix
- Representative Email: `null` in database
- Verification Token: `null` in database
- Is Anonymous: String `"on"` instead of boolean
- No verification emails sent
### After Fix
- ✅ Representative Email: Correctly stored as string
- ✅ Verification Token: 64-character hex string generated
- ✅ Verification Sent At: ISO timestamp
- ✅ Is Anonymous: Boolean `true` or `false`
- ✅ Verification email sent successfully
## NocoDB Best Practices Applied
Following the guidelines from `instruct.md`:
1. **Use Column Titles, Not Column Names:** All field mappings use NocoDB Column Titles (e.g., "Representative Email" not "representative_email")
2. **Consistent Mapping:** Service layer properly maps between application field names and NocoDB column titles
3. **System Field Awareness:** Avoided conflicts with NocoDB system fields
## Deployment
No database schema changes required - the columns already exist from the previous deployment. Only code changes needed:
```bash
# Rebuild Docker container
docker compose build && docker compose up -d
```
## Verification Checklist
After deployment, verify:
- [ ] Submit a response with postal code lookup
- [ ] Select representative with email address
- [ ] Check "Send verification request" checkbox
- [ ] Submit form
- [ ] Verify in database:
- [ ] Representative Email is populated (string, not array)
- [ ] Verification Token is 64-char hex string
- [ ] Verification Sent At has ISO timestamp
- [ ] Is Anonymous is boolean
- [ ] Check MailHog/email for verification email
- [ ] Click verification link and confirm it works
## Related Documentation
- `IMPLEMENTATION_SUMMARY.md` - Full feature implementation
- `DEPLOYMENT_GUIDE.md` - Deployment instructions
- `instruct.md` - NocoDB best practices
---
**Date Fixed:** October 16, 2025
**Status:** ✅ Resolved