feat(leads): add lead pipeline, keyword/status mapping, and encoding fixes

- Add lead ingestion, review queue, and keyword/status management
- Add DB schema: leads, service_keywords, lead_statuses, processed_messages
- Add migration with default keyword/status seeding per client
- Fix SQLite read/write deadlock in sanitizeEncoding
- Fix UTF-8 corruption: replace byte-iterating replaceAll with strings.ReplaceAll
- Add utf8.ValidString guard to decodeLatin1 to avoid double-encoding
- Remove hardcoded internal-secret; use config.InternalSecret() everywhere
- Add .gitignore for binaries, SQLite DBs, build artifacts, WhatsApp sessions
This commit is contained in:
2026-05-23 16:49:43 -03:00
parent 57e6e5a6fd
commit 57920d45d6
7 changed files with 1437 additions and 42 deletions

View File

@@ -5,13 +5,14 @@ import (
"strconv"
"time"
"go-crm/config"
"go-crm/internal/db"
"github.com/go-chi/chi/v5"
)
func ListCustomers(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
func (a *App) ListCustomers(w http.ResponseWriter, r *http.Request) {
accountID, ok := a.requireAuth(w, r)
if !ok {
return
}
@@ -23,13 +24,13 @@ func ListCustomers(w http.ResponseWriter, r *http.Request) {
limit = 20
}
customers, err := db.ListCustomers(DB, accountID, clientID, limit, offset)
customers, err := db.ListCustomers(a.DB, accountID, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
clients, err := db.ListClients(DB, accountID, limit, offset)
clients, err := db.ListClients(a.DB, accountID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -40,7 +41,7 @@ func ListCustomers(w http.ResponseWriter, r *http.Request) {
clientOptions += `<option value="` + strconv.FormatInt(c.ClientID, 10) + `">` + c.Name + `</option>`
}
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<!DOCTYPE html><html><head><script src="https://unpkg.com/htmx.org@1.9.10"></script><style>.edit-form{display:none;margin-top:0.5rem;padding:0.5rem;border:1px solid #ccc}</style></head><body><h1>Customers</h1><button class="btn" onclick="document.getElementById('customerForm').style.display='block'">Add Customer</button><div id="customerForm" style="display:none; margin-top:1rem;"><form hx-post="/customers" hx-target="#customerList"><input type="text" name="name" placeholder="Name" required><input type="tel" name="phone" placeholder="Phone"><input type="date" name="birth_date" placeholder="Birth Date"><input type="text" name="instagram" placeholder="Instagram"><select name="client_id" required><option value="">Select Client</option>` + clientOptions + `</select><button type="submit">Add</button></form></div><table><thead><tr><th>Name</th><th>Phone</th><th>Birth Date</th><th>Instagram</th><th>Actions</th></tr></thead><tbody id="customerList">`))
for _, c := range customers {
w.Write([]byte(`<tr><td>` + c.Name + `</td><td>` + c.Phone + `</td><td>` + c.BirthDate + `</td><td>` + c.Instagram + `</td><td><a href="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `">View</a><button type="button" onclick="document.getElementById('editCust` + strconv.FormatInt(c.CustomerID, 10) + `').style.display='block'">Edit</button><form method="DELETE" style="display:inline" hx-delete="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `" hx-target="closest tr"><button type="submit">Delete</button></form></td></tr><tr id="editCust` + strconv.FormatInt(c.CustomerID, 10) + `" style="display:none"><td colspan="5"><form hx-put="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `" hx-target="#customerList" hx-swap="innerHTML"><input type="text" name="name" value="` + c.Name + `"><input type="tel" name="phone" value="` + c.Phone + `"><input type="date" name="birth_date" value="` + c.BirthDate + `"><input type="text" name="instagram" value="` + c.Instagram + `"><select name="client_id"><option value="` + strconv.FormatInt(c.ClientID, 10) + `">` + c.Name + `</option>` + clientOptions + `</select><button type="submit">Save</button></form></td></tr>`))
@@ -48,24 +49,24 @@ func ListCustomers(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`</tbody></table></body></html>`))
}
func CreateCustomer(w http.ResponseWriter, r *http.Request) {
isInternal := r.Header.Get("X-Internal-Secret") == "internal-secret"
func (a *App) CreateCustomer(w http.ResponseWriter, r *http.Request) {
isInternal := r.Header.Get("X-Internal-Secret") == config.InternalSecret()
var accountID int64
var clientID int64
var ok bool
if isInternal {
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
accountID = clientID
ok = true
} else {
accountID, ok = requireAuth(w, r)
accountID, ok = a.requireAuth(w, r)
if ok {
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
}
}
if !ok {
return
}
@@ -74,10 +75,10 @@ func CreateCustomer(w http.ResponseWriter, r *http.Request) {
if clientID == 0 {
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
}
if !isInternal {
var checkID int64
err := DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID)
err := a.DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID)
if err != nil {
http.Error(w, "Invalid client", http.StatusBadRequest)
return
@@ -85,22 +86,22 @@ func CreateCustomer(w http.ResponseWriter, r *http.Request) {
}
customer := db.Customer{
ClientID: clientID,
Name: r.FormValue("name"),
Phone: r.FormValue("phone"),
BirthDate: r.FormValue("birth_date"),
Instagram: r.FormValue("instagram"),
CreatedAt: time.Now().Unix(),
ClientID: clientID,
Name: r.FormValue("name"),
Phone: r.FormValue("phone"),
BirthDate: r.FormValue("birth_date"),
Instagram: r.FormValue("instagram"),
CreatedAt: time.Now().Unix(),
}
if phone := r.FormValue("phone"); phone != "" {
var existingID int64
err := DB.QueryRow(
err := a.DB.QueryRow(
"SELECT customer_id FROM customers WHERE client_id = ? AND phone = ?",
clientID, phone,
).Scan(&existingID)
if err == nil {
_, err = DB.Exec(
_, err = a.DB.Exec(
"UPDATE customers SET name = ?, birth_date = ?, instagram = ? WHERE customer_id = ?",
r.FormValue("name"), r.FormValue("birth_date"), r.FormValue("instagram"), existingID,
)
@@ -111,7 +112,7 @@ func CreateCustomer(w http.ResponseWriter, r *http.Request) {
}
}
if err := customer.Create(DB); err != nil {
if err := customer.Create(a.DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
@@ -119,26 +120,26 @@ func CreateCustomer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Refresh", "true")
}
func ViewCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) ViewCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
var c db.Customer
err := DB.QueryRow("SELECT customer_id, client_id, name, phone, birth_date, instagram, created_at FROM customers WHERE customer_id = ?", id).Scan(&c.CustomerID, &c.ClientID, &c.Name, &c.Phone, &c.BirthDate, &c.Instagram, &c.CreatedAt)
err := a.DB.QueryRow("SELECT customer_id, client_id, name, phone, birth_date, instagram, created_at FROM customers WHERE customer_id = ?", id).Scan(&c.CustomerID, &c.ClientID, &c.Name, &c.Phone, &c.BirthDate, &c.Instagram, &c.CreatedAt)
if err != nil {
http.Error(w, "Customer not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<!DOCTYPE html><body><h1>` + c.Name + `</h1><p>Phone: ` + c.Phone + `</p><p>Birth Date: ` + c.BirthDate + `</p><p>Instagram: ` + c.Instagram + `</p><a href="/customers">Back</a></body></html>`))
}
func UpdateCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) UpdateCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
@@ -146,7 +147,7 @@ func UpdateCustomer(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
r.ParseForm()
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
_, err := DB.Exec("UPDATE customers SET client_id=?, name=?, phone=?, birth_date=?, instagram=? WHERE customer_id=?", clientID, r.FormValue("name"), r.FormValue("phone"), r.FormValue("birth_date"), r.FormValue("instagram"), id)
_, err := a.DB.Exec("UPDATE customers SET client_id=?, name=?, phone=?, birth_date=?, instagram=? WHERE customer_id=?", clientID, r.FormValue("name"), r.FormValue("phone"), r.FormValue("birth_date"), r.FormValue("instagram"), id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -154,12 +155,30 @@ func UpdateCustomer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Refresh", "true")
}
func DeleteCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) DeleteCustomer(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
DB.Exec("DELETE FROM customers WHERE customer_id = ?", id)
}
a.DB.Exec("DELETE FROM customers WHERE customer_id = ?", id)
}
// --- package-level shims kept for existing tests ---
func ListCustomers(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).ListCustomers(w, r)
}
func CreateCustomer(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).CreateCustomer(w, r)
}
func ViewCustomer(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).ViewCustomer(w, r)
}
func UpdateCustomer(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).UpdateCustomer(w, r)
}
func DeleteCustomer(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).DeleteCustomer(w, r)
}