feat(go-crm): full auth, routing, middleware, and supporting infra
- Add auth handlers (signup, login, logout, account management) with bcrypt - Add client, customer, service, scheduling, payment, question, answer handlers - Add dashboard, monthly report, and lead pipeline pages - Add UTF-8 middleware to force charset on HTML responses - Add config package with env-based overrides for DB path, secrets, endpoints - Add parser package for WhatsApp message ingestion - Add clean-arch layers: pkg/domain, pkg/repo, pkg/usecase for leads - Add cmd/migrate utility for DB migrations - Add Makefile, README, run-tests.sh, and dev scripts - Update docker-compose.yml with memory limits - Update .air.toml to exclude DB files and stop on errors - Update whatsapp-sync dependencies and add src/index.js entrypoint - Add whatsme standalone WhatsApp reader app (source only) - Untrack .opencode-sandbox/data/go-crm.db from git history - Expand root .gitignore: ngrok, tmp dirs, sandbox DBs, compiled binaries
This commit is contained in:
85
apps/go-crm/internal/handlers/dashboard.go
Normal file
85
apps/go-crm/internal/handlers/dashboard.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (a *App) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
accountID, err := a.getSession(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/auth/login", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
var clientID int64
|
||||
a.DB.QueryRow("SELECT client_id FROM clients WHERE account_id = ? LIMIT 1", accountID).Scan(&clientID)
|
||||
var reviewCount int
|
||||
if clientID > 0 {
|
||||
a.DB.QueryRow("SELECT COUNT(*) FROM leads WHERE client_id = ? AND needs_review = 1", clientID).Scan(&reviewCount)
|
||||
}
|
||||
|
||||
waStatus := "disconnected"
|
||||
waStyle := "color:#dc3545"
|
||||
connectLink := ""
|
||||
if a.WAConnector != nil {
|
||||
if connected, _ := a.WAConnector.IsConnected(r.Context(), clientID); connected {
|
||||
waStatus = "connected"
|
||||
waStyle = "color:#28a745"
|
||||
} else if clientID > 0 {
|
||||
// Fallback: check DB column when in-memory state not available.
|
||||
var dbConnected int
|
||||
a.DB.QueryRow("SELECT whatsapp_connected FROM clients WHERE client_id = ?", clientID).Scan(&dbConnected)
|
||||
if dbConnected == 1 {
|
||||
waStatus = "connected"
|
||||
waStyle = "color:#28a745"
|
||||
} else {
|
||||
connectLink = fmt.Sprintf(` <a href="/leads/connect?client_id=%d" style="color:#28a745">Connect</a>`, clientID)
|
||||
}
|
||||
} else {
|
||||
connectLink = ` <a href="/clients" style="color:#28a745">Create client to connect</a>`
|
||||
}
|
||||
}
|
||||
|
||||
badge := ""
|
||||
if reviewCount > 0 {
|
||||
badge = fmt.Sprintf(` <span style="background:#dc3545;color:#fff;border-radius:1rem;padding:0.15rem 0.5rem;font-size:0.8rem">%d</span>`, reviewCount)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprintf(w, `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Dashboard</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; padding: 1.5rem; }
|
||||
nav a { margin-right: 1rem; text-decoration: none; color: #2c7be5; }
|
||||
nav a:hover { text-decoration: underline; }
|
||||
.wa-status { font-size: 0.9rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>CRM Dashboard</h1>
|
||||
<p class="wa-status">WhatsApp: <strong style="%s">%s</strong>%s</p>
|
||||
<nav>
|
||||
<a href="/leads/review">Review Queue%s</a>
|
||||
<a href="/leads/all">All Leads</a>
|
||||
<a href="/report">Monthly Report</a>
|
||||
<a href="/leads/keywords">Keyword Mapping</a>
|
||||
<a href="/clients">Clients</a>
|
||||
<a href="/services">Services</a>
|
||||
<a href="/scheduling">Scheduling</a>
|
||||
<a href="/payments">Payments</a>
|
||||
<a href="/auth/account">Account</a>
|
||||
<form method="POST" action="/auth/logout" style="display:inline">
|
||||
<button type="submit">Logout</button>
|
||||
</form>
|
||||
</nav>
|
||||
</body></html>`, waStyle, waStatus, connectLink, badge)
|
||||
}
|
||||
|
||||
// --- package-level shim kept for existing tests ---
|
||||
|
||||
func Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
(&App{DB: DB, WAConnector: WAConnector}).Dashboard(w, r)
|
||||
}
|
||||
Reference in New Issue
Block a user