Add leads handlers, WhatsApp integration, and test files

This commit is contained in:
2026-05-02 17:40:38 +00:00
parent 8833e38b80
commit 57e6e5a6fd
24 changed files with 2305 additions and 115 deletions

View File

@@ -5,14 +5,17 @@ import (
)
type Client struct {
ClientID int64 `json:"client_id"`
AccountID int64 `json:"account_id"`
Name string `json:"name"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Address string `json:"address,omitempty"`
Notes string `json:"notes,omitempty"`
CreatedAt int64 `json:"created_at"`
ClientID int64 `json:"client_id"`
AccountID int64 `json:"account_id"`
Name string `json:"name"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Address string `json:"address,omitempty"`
Notes string `json:"notes,omitempty"`
WhatsAppNumber string `json:"whatsapp_number,omitempty"`
WhatsAppConnected int `json:"whatsapp_connected"`
WhatsAppJID string `json:"whatsapp_jid,omitempty"`
CreatedAt int64 `json:"created_at"`
}
type Customer struct {
@@ -60,8 +63,8 @@ type Payment struct {
func (c *Client) Create(db *sql.DB) error {
result, err := db.Exec(
"INSERT INTO clients (account_id, name, phone, email, address, notes, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
c.AccountID, c.Name, c.Phone, c.Email, c.Address, c.Notes, c.CreatedAt,
"INSERT INTO clients (account_id, name, phone, email, address, notes, whatsapp_number, whatsapp_connected, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
c.AccountID, c.Name, c.Phone, c.Email, c.Address, c.Notes, c.WhatsAppNumber, c.WhatsAppConnected, c.CreatedAt,
)
if err != nil {
return err
@@ -76,15 +79,15 @@ func (c *Client) Create(db *sql.DB) error {
func (c *Client) Read(db *sql.DB, id int64) error {
return db.QueryRow(
"SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), created_at FROM clients WHERE client_id = ?",
"SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), COALESCE(whatsapp_number,''), COALESCE(whatsapp_connected,0), COALESCE(whatsapp_jid,''), created_at FROM clients WHERE client_id = ?",
id,
).Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.CreatedAt)
).Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.WhatsAppNumber, &c.WhatsAppConnected, &c.WhatsAppJID, &c.CreatedAt)
}
func (c *Client) Update(db *sql.DB) error {
_, err := db.Exec(
"UPDATE clients SET name = ?, phone = ?, email = ?, address = ?, notes = ? WHERE client_id = ? AND account_id = ?",
c.Name, c.Phone, c.Email, c.Address, c.Notes, c.ClientID, c.AccountID,
"UPDATE clients SET name = ?, phone = ?, email = ?, address = ?, notes = ?, whatsapp_number = ?, whatsapp_connected = ?, whatsapp_jid = ? WHERE client_id = ? AND account_id = ?",
c.Name, c.Phone, c.Email, c.Address, c.Notes, c.WhatsAppNumber, c.WhatsAppConnected, c.WhatsAppJID, c.ClientID, c.AccountID,
)
return err
}
@@ -95,7 +98,7 @@ func (c *Client) Delete(db *sql.DB) error {
}
func ListClients(db *sql.DB, accountID int64, limit, offset int) ([]Client, error) {
query := "SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), created_at FROM clients"
query := "SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), COALESCE(whatsapp_number,''), COALESCE(whatsapp_connected,0), COALESCE(whatsapp_jid,''), created_at FROM clients"
args := []interface{}{}
if accountID > 0 {
query += " WHERE account_id = ?"
@@ -113,7 +116,7 @@ func ListClients(db *sql.DB, accountID int64, limit, offset int) ([]Client, erro
var clients []Client
for rows.Next() {
var c Client
if err := rows.Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.CreatedAt); err != nil {
if err := rows.Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.WhatsAppNumber, &c.WhatsAppConnected, &c.WhatsAppJID, &c.CreatedAt); err != nil {
return nil, err
}
clients = append(clients, c)
@@ -124,9 +127,9 @@ func ListClients(db *sql.DB, accountID int64, limit, offset int) ([]Client, erro
func GetClientByID(db *sql.DB, accountID, id int64) (*Client, error) {
var c Client
err := db.QueryRow(
"SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), created_at FROM clients WHERE client_id = ? AND account_id = ?",
"SELECT client_id, account_id, name, COALESCE(phone,''), COALESCE(email,''), COALESCE(address,''), COALESCE(notes,''), COALESCE(whatsapp_number,''), COALESCE(whatsapp_connected,0), COALESCE(whatsapp_jid,''), created_at FROM clients WHERE client_id = ? AND account_id = ?",
id, accountID,
).Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.CreatedAt)
).Scan(&c.ClientID, &c.AccountID, &c.Name, &c.Phone, &c.Email, &c.Address, &c.Notes, &c.WhatsAppNumber, &c.WhatsAppConnected, &c.WhatsAppJID, &c.CreatedAt)
if err != nil {
return nil, err
}