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:
2026-05-23 16:55:55 -03:00
parent 57920d45d6
commit 744868caa1
52 changed files with 4868 additions and 1068 deletions

View File

@@ -10,8 +10,8 @@ import (
"github.com/go-chi/chi/v5"
)
func ListSchedules(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
func (a *App) ListSchedules(w http.ResponseWriter, r *http.Request) {
accountID, ok := a.requireAuth(w, r)
if !ok {
return
}
@@ -24,25 +24,25 @@ func ListSchedules(w http.ResponseWriter, r *http.Request) {
limit = 20
}
schedules, err := db.ListSchedules(DB, clientID, customerID, limit, offset)
schedules, err := db.ListSchedules(a.DB, clientID, customerID, 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
}
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
}
services, err := db.ListServices(DB, accountID, clientID, limit, offset)
services, err := db.ListServices(a.DB, accountID, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -65,7 +65,7 @@ func ListSchedules(w http.ResponseWriter, r *http.Request) {
serviceNames[s.ServiceID] = s.Name
}
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-row{display:none}</style></head><body><h1>Scheduling</h1><button class="btn" onclick="document.getElementById('scheduleForm').style.display='block'">Add Schedule</button><div id="scheduleForm" style="display:none; margin-top:1rem;"><form hx-post="/scheduling" hx-target="#scheduleList"><select name="client_id" required><option value="">Select Client</option>` + clientOptions + `</select><select name="customer_id" required><option value="">Select Customer</option>` + customerOptions + `</select><select name="service_id" required><option value="">Select Service</option>` + serviceOptions + `</select><input type="date" name="plan_date"><input type="time" name="time"><select name="status"><option value="pending">Pending</option><option value="confirmed">Confirmed</option><option value="cancelled">Cancelled</option></select><button type="submit">Add</button></form></div><table><thead><tr><th>Client</th><th>Customer</th><th>Service</th><th>Date</th><th>Hour</th><th>Status</th><th>Actions</th></tr></thead><tbody id="scheduleList">`))
for _, s := range schedules {
clientName := clientNames[s.ClientID]
@@ -85,8 +85,8 @@ func ListSchedules(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`</tbody></table></body></html>`))
}
func CreateSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) CreateSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
@@ -96,16 +96,16 @@ func CreateSchedule(w http.ResponseWriter, r *http.Request) {
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
serviceID, _ := strconv.ParseInt(r.FormValue("service_id"), 10, 64)
schedule := db.Schedule{
ClientID: clientID,
ClientID: clientID,
CustomerID: customerID,
ServiceID: serviceID,
PlanDate: r.FormValue("plan_date"),
Time: r.FormValue("time"),
Status: r.FormValue("status"),
CreatedAt: time.Now().Unix(),
Time: r.FormValue("time"),
Status: r.FormValue("status"),
CreatedAt: time.Now().Unix(),
}
if err := schedule.Create(DB); err != nil {
if err := schedule.Create(a.DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
@@ -113,26 +113,26 @@ func CreateSchedule(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Refresh", "true")
}
func ViewSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) ViewSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
var sch db.Schedule
err := DB.QueryRow("SELECT schedule_id, client_id, customer_id, service_id, plan_date, time, status, created_at FROM scheduling WHERE schedule_id = ?", id).Scan(&sch.ScheduleID, &sch.ClientID, &sch.CustomerID, &sch.ServiceID, &sch.PlanDate, &sch.Time, &sch.Status, &sch.CreatedAt)
err := a.DB.QueryRow("SELECT schedule_id, client_id, customer_id, service_id, plan_date, time, status, created_at FROM scheduling WHERE schedule_id = ?", id).Scan(&sch.ScheduleID, &sch.ClientID, &sch.CustomerID, &sch.ServiceID, &sch.PlanDate, &sch.Time, &sch.Status, &sch.CreatedAt)
if err != nil {
http.Error(w, "Schedule 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>Schedule</h1><p>Client ID: ` + strconv.FormatInt(sch.ClientID, 10) + `</p><p>Customer ID: ` + strconv.FormatInt(sch.CustomerID, 10) + `</p><p>Service ID: ` + strconv.FormatInt(sch.ServiceID, 10) + `</p><p>Date: ` + sch.PlanDate + `</p><p>Time: ` + sch.Time + `</p><p>Status: ` + sch.Status + `</p><a href="/scheduling">Back</a></body></html>`))
}
func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) UpdateSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
@@ -142,7 +142,7 @@ func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
serviceID, _ := strconv.ParseInt(r.FormValue("service_id"), 10, 64)
_, err := DB.Exec("UPDATE scheduling SET client_id=?, customer_id=?, service_id=?, plan_date=?, time=?, status=? WHERE schedule_id=?", clientID, customerID, serviceID, r.FormValue("plan_date"), r.FormValue("time"), r.FormValue("status"), id)
_, err := a.DB.Exec("UPDATE scheduling SET client_id=?, customer_id=?, service_id=?, plan_date=?, time=?, status=? WHERE schedule_id=?", clientID, customerID, serviceID, r.FormValue("plan_date"), r.FormValue("time"), r.FormValue("status"), id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -150,12 +150,30 @@ func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Refresh", "true")
}
func DeleteSchedule(w http.ResponseWriter, r *http.Request) {
_, ok := requireAuth(w, r)
func (a *App) DeleteSchedule(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 scheduling WHERE schedule_id = ?", id)
}
a.DB.Exec("DELETE FROM scheduling WHERE schedule_id = ?", id)
}
// --- package-level shims kept for existing tests ---
func ListSchedules(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).ListSchedules(w, r)
}
func CreateSchedule(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).CreateSchedule(w, r)
}
func ViewSchedule(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).ViewSchedule(w, r)
}
func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).UpdateSchedule(w, r)
}
func DeleteSchedule(w http.ResponseWriter, r *http.Request) {
(&App{DB: DB, WAConnector: WAConnector}).DeleteSchedule(w, r)
}