The bug was in field mapping when saving AI-generated templates:
- Changed 'type' to 'template_type' (correct column name)
- Changed 'body_template' to 'body_html' (correct column name)
This fixes the 400 Bad Request validation error:
'notNull Violation: MailTemplate.template_type cannot be null'
Now AI template generation works correctly.
Database Changes:
- Remove UNIQUE constraint from mail_templates.template_type
- Allow multiple templates with same type (e.g., multiple bank templates)
Ollama Service Improvements:
- Add detailed logging for AI responses (first 500 chars)
- Check multiple field name variations (subject/Subject/konu/title, body/Body/html/content)
- Improve error messages with actual field names
- Better JSON parsing with fallback
- Clearer prompt instructions to AI
This fixes:
- 400 Validation Error when creating templates with existing type
- AI response parsing issues with different field names
- Auto-save settings before testing connection
- Make model list clickable for easy selection
- Models auto-populate when clicked
- Better user experience for Ollama configuration
Now users can click on a model from the list to select it instantly.
✨ New Features:
- 🤖 AI-powered mail template generation with Ollama
- 📧 Test mail sending with preview
- 🔧 Ollama server and model management
- 🎨 Beautiful AI generation dialog in Templates page
- ⚙️ Ollama settings panel with connection test
Backend:
- Add ollama.service.js - Ollama API integration
- Add ollama.controller.js - Template generation endpoint
- Add ollama.routes.js - /api/ollama/* routes
- Support for multiple Ollama models (llama3.2, mistral, gemma)
- JSON-formatted AI responses with subject + HTML body
- Configurable server URL and model selection
Frontend:
- Settings: Ollama configuration panel
- Server URL input
- Model selection
- Connection test with model listing
- Templates: AI generation dialog
- Company name, scenario, employee info inputs
- Custom prompt for AI instructions
- Auto-save to database
- Test mail sending functionality
Documentation:
- OLLAMA_SETUP.md - Comprehensive setup guide
- Installation instructions
- Model recommendations
- Usage examples
- Troubleshooting
Tech Stack:
- Ollama API integration (REST)
- Axios HTTP client
- React dialogs with MUI
- Self-hosted AI (privacy-friendly)
- Zero external API dependencies
Example Usage:
Company: Garanti Bankası
Scenario: Account security warning
→ AI generates professional phishing test mail in seconds!
- testConnection now sends a real test email to the configured Gmail address
- User receives actual email confirmation that setup works
- Email contains success message and system info
- Google provides App Password with spaces (e.g. 'abcd efgh ijkl mnop')
- Backend now automatically removes all spaces before saving
- Users can paste password directly from Google without manual cleanup
- Also trim gmail_user for safety
- Changed gmail_password to gmail_app_password in request body destructuring
- Matches frontend Settings.jsx which sends gmail_app_password
- Backend still stores as 'gmail_password' key in database (for backward compatibility)
- Fixed handleSave to use separate /gmail and /telegram endpoints
- Changed /test-mail to /test-gmail endpoint
- Added proper null checks in loadSettings to prevent uncontrolled input warnings
- Settings now properly saved to backend with correct endpoint structure
- Updated templateService to return response.data like other services
- Added fallback to empty array in Templates.jsx to prevent map errors
- Ensures consistency across all service modules
- Fixed AuthContext to properly set user to null on auth check failure
- Added loading spinner in PrivateRoute instead of returning null
- Removed automatic redirect from API interceptor (let React Router handle it)
- Improved error handling in checkAuth to prevent stuck loading state
- Added full CRUD endpoints for mail templates (create, update, delete, preview)
- Introduced Joi validators for template create/update/preview
- Updated routes/controller to support ID and type lookups
- Built React Templates page with HTML editor, preview, and clipboard helpers
- Added navigation entry and route for /templates
- Enhanced documentation (README, QUICKSTART, KULLANIM, frontend/backend README)
- Created CompanyDetail page with stats, info, and tokens list
- Created TokenDetail page with click history and full tracking info
- Added routes for /companies/:id and /tokens/:id
- Made table rows clickable to navigate to detail pages
- Added edit, delete, and mail resend functionality
- Shows IP addresses, GeoIP location, device and browser info in click logs
- Fixed dotenv path in app.js to properly load .env from backend root
- Updated CORS to allow http://localhost:5173 (frontend URL)
- Updated README.md with accurate project statistics (58 files, 6677+ lines)
- Updated backend/README.md - marked as Production Ready
- Updated frontend/README.md with complete feature list
- All systems tested and working (backend + frontend)