Add leads handlers, WhatsApp integration, and test files
This commit is contained in:
@@ -49,19 +49,39 @@ func ListCustomers(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func CreateCustomer(w http.ResponseWriter, r *http.Request) {
|
||||
accountID, ok := requireAuth(w, r)
|
||||
isInternal := r.Header.Get("X-Internal-Secret") == "internal-secret"
|
||||
|
||||
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)
|
||||
if ok {
|
||||
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
||||
|
||||
var checkID int64
|
||||
err := 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
|
||||
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)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid client", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
customer := db.Customer{
|
||||
@@ -73,6 +93,24 @@ func CreateCustomer(w http.ResponseWriter, r *http.Request) {
|
||||
CreatedAt: time.Now().Unix(),
|
||||
}
|
||||
|
||||
if phone := r.FormValue("phone"); phone != "" {
|
||||
var existingID int64
|
||||
err := DB.QueryRow(
|
||||
"SELECT customer_id FROM customers WHERE client_id = ? AND phone = ?",
|
||||
clientID, phone,
|
||||
).Scan(&existingID)
|
||||
if err == nil {
|
||||
_, err = DB.Exec(
|
||||
"UPDATE customers SET name = ?, birth_date = ?, instagram = ? WHERE customer_id = ?",
|
||||
r.FormValue("name"), r.FormValue("birth_date"), r.FormValue("instagram"), existingID,
|
||||
)
|
||||
if err == nil {
|
||||
w.Header().Set("HX-Refresh", "true")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := customer.Create(DB); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user