# CRM App Improvement Specification ## Current State Analysis The CRM app (`botui/ui/suite/crm/crm.html`) provides basic customer relationship management with: - Pipeline view (Lead → Qualified → Proposal → Negotiation → Won/Lost) - Deals, Accounts, Contacts, Campaigns tabs - Lead form with basic fields - Drag-and-drop pipeline cards - HTMX-based data loading ## Critical Issues to Fix ### 1. Broken API Endpoints **Problem:** Mixed endpoint patterns causing 404s - Pipeline uses `/api/ui/crm/pipeline` (correct) - Counts use `/api/ui/crm/count` AND `/api/crm/count` (inconsistent) - Deals use `/api/ui/crm/deals` (correct) - Stage updates use `/api/crm/opportunity/{id}/stage` (missing `/ui/`) **Fix:** ```html 0 0 0 0 ``` **Action:** Standardize ALL endpoints to `/api/ui/crm/*` pattern. ### 2. Missing Deal Card Template **Problem:** Pipeline loads empty - no HTML template for deal cards returned by API **Fix:** Backend must return HTML fragments like: ```html

{{title}}

${{value}}

{{company}}

{{contact_name}}
``` **Action:** Create `botserver/src/ui/crm/templates/deal_card.html` and render in API response. ### 3. Drag-and-Drop Not Working **Problem:** Cards need `draggable="true"` and proper event handlers **Fix:** ```javascript // Add to pipeline card template
// Update drop handler column.addEventListener('drop', async (e) => { e.preventDefault(); const cardId = e.dataTransfer.getData('text/plain'); const newStage = column.closest('.pipeline-column').dataset.stage; const token = localStorage.getItem('gb_token'); await fetch(`/api/ui/crm/opportunity/${cardId}/stage`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify({ stage: newStage }) }); // Refresh pipeline htmx.trigger('#crm-pipeline-view', 'refresh'); }); ``` ### 4. Form Validation Missing **Problem:** Lead form submits incomplete data **Fix:** ```html ``` ## Major Improvements Needed ### 5. Activity Timeline **What:** Show interaction history for each deal/contact **Implementation:** ```html
📧
Email sent

Proposal sent to john@company.com

2 hours ago
📞
Call completed

Discussed pricing and timeline

Yesterday
``` **Backend:** Add `GET /api/ui/crm/opportunity/{id}/activities` endpoint. ### 6. Quick Actions Menu **What:** Right-click context menu on pipeline cards **Implementation:** ```javascript document.addEventListener('contextmenu', (e) => { const card = e.target.closest('.pipeline-card'); if (!card) return; e.preventDefault(); showContextMenu(e.clientX, e.clientY, [ { label: 'Edit', action: () => editDeal(card.dataset.id) }, { label: 'Send Email', action: () => sendEmail(card.dataset.id) }, { label: 'Schedule Call', action: () => scheduleCall(card.dataset.id) }, { label: 'Mark as Won', action: () => updateStage(card.dataset.id, 'won') }, { label: 'Mark as Lost', action: () => updateStage(card.dataset.id, 'lost') }, { label: 'Delete', action: () => deleteDeal(card.dataset.id), danger: true } ]); }); ``` ### 7. Bulk Operations **What:** Select multiple deals and perform batch actions **Implementation:** ```html
``` ### 8. Advanced Filters **What:** Filter deals by owner, date range, value, probability **Implementation:** ```html
``` ### 9. Deal Detail Panel **What:** Slide-out panel with full deal information **Implementation:** ```html

``` ### 10. Email Integration **What:** Send emails directly from CRM **Implementation:** ```html ``` **Backend:** Add `POST /api/ui/crm/opportunity/{id}/email` endpoint. ## Performance Optimizations ### 11. Lazy Loading **Problem:** Loading all pipeline cards at once is slow **Fix:** ```html
``` ### 12. Optimistic Updates **Problem:** Drag-and-drop feels slow waiting for server response **Fix:** ```javascript column.addEventListener('drop', async (e) => { e.preventDefault(); const cardId = e.dataTransfer.getData('text/plain'); const card = document.querySelector(`[data-id="${cardId}"]`); const newStage = column.closest('.pipeline-column').dataset.stage; // Optimistic update - move card immediately column.querySelector('.pipeline-cards').appendChild(card); // Update server in background try { await fetch(`/api/ui/crm/opportunity/${cardId}/stage`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ stage: newStage }) }); } catch (err) { // Revert on error console.error('Failed to update stage:', err); // Move card back to original column } }); ``` ## UI/UX Enhancements ### 13. Keyboard Shortcuts ```javascript document.addEventListener('keydown', (e) => { // Ctrl+N: New lead if (e.ctrlKey && e.key === 'n') { e.preventDefault(); document.getElementById('crm-new-btn').click(); } // Ctrl+F: Focus search if (e.ctrlKey && e.key === 'f') { e.preventDefault(); document.querySelector('.crm-search input').focus(); } // Escape: Close modal if (e.key === 'Escape') { closeCrmModal(); closeDetailPanel(); } }); ``` ### 14. Empty States ```html

No deals in this stage

Drag deals here or create a new one

``` ### 15. Loading States ```html
``` ## Backend Requirements ### API Endpoints Needed ``` GET /api/ui/crm/pipeline?stage={stage}&owner={owner}&date_from={date}&date_to={date} GET /api/ui/crm/count?stage={stage} GET /api/ui/crm/opportunity/{id} GET /api/ui/crm/opportunity/{id}/activities POST /api/ui/crm/opportunity/{id}/stage POST /api/ui/crm/opportunity/{id}/email POST /api/ui/crm/leads GET /api/ui/crm/deals?filter={filter} GET /api/ui/crm/accounts GET /api/ui/crm/contacts GET /api/ui/crm/search?q={query} GET /api/ui/crm/stats/pipeline-value GET /api/ui/crm/stats/conversion-rate GET /api/ui/crm/stats/avg-deal GET /api/ui/crm/stats/won-month ``` ### Database Schema Additions ```sql -- Activity tracking CREATE TABLE crm_activities ( id UUID PRIMARY KEY, opportunity_id UUID REFERENCES crm_opportunities(id), activity_type VARCHAR(50), -- email, call, meeting, note subject TEXT, description TEXT, created_at TIMESTAMP, created_by UUID ); -- Email templates CREATE TABLE crm_email_templates ( id UUID PRIMARY KEY, name VARCHAR(255), subject TEXT, body TEXT, created_at TIMESTAMP ); ``` ## Implementation Priority 1. **Critical (Do First):** - Fix API endpoint inconsistencies - Add deal card HTML template - Fix drag-and-drop functionality - Add form validation 2. **High Priority:** - Activity timeline - Deal detail panel - Advanced filters - Email integration 3. **Medium Priority:** - Bulk operations - Quick actions menu - Keyboard shortcuts - Empty/loading states 4. **Nice to Have:** - Lazy loading - Optimistic updates - Advanced analytics dashboard ## Testing Checklist - [ ] Create new lead via form - [ ] Drag deal between pipeline stages - [ ] Search for deals/accounts - [ ] Filter deals by owner/date/value - [ ] View deal details in side panel - [ ] Send email from deal - [ ] Bulk update multiple deals - [ ] Keyboard shortcuts work - [ ] Mobile responsive layout - [ ] All API endpoints return correct data