The admin enables or disables each option (phone, address, Snapchat, Instagram, Telegram, SimpleX, WhatsApp, Signal, notes, preferred time) from a new "Checkout fields" page; the customer's name is always asked. Backend - checkoutfields module: single-row settings (CHECK id = 1), public GET and admin GET/PUT. - orders: answers stored as JSONB (contacts, info_sup); the customer phone moves from customer_phone to contacts.phone (migration 000034 copies existing values before dropping the column). - Orders are validated against the live settings: disabled options and unknown keys are dropped, enabled phone/address are required, and format, length and control characters are checked. - The Telegram notification includes the answers. Frontend - Checkout form generated from the enabled options; admin settings page; answers shown in the order list and detail. - API calls live in src/api (checkoutFields.ts, orders.ts); ApiError keeps the response body so the rejected field can be reported. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
101 lines
2.0 KiB
Go
101 lines
2.0 KiB
Go
package orders
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
ContactSnap = "snap"
|
|
ContactInsta = "insta"
|
|
ContactTelegram = "telegram"
|
|
ContactSimpleX = "simplex"
|
|
ContactWhatsapp = "whatsapp"
|
|
ContactSignal = "signal"
|
|
ContactPhone = "phone"
|
|
)
|
|
|
|
const (
|
|
ContactAddress = "address"
|
|
ContactHour = "hour"
|
|
)
|
|
|
|
// Contacts : valeurs saisies par le client, stockées en JSONB. Une colonne
|
|
// JSON évite une migration à chaque nouvelle option. Le téléphone et les
|
|
// notes gardent leurs colonnes dédiées (customer_phone, notes).
|
|
type Contacts map[string]string
|
|
|
|
type InfoSup map[string]string
|
|
|
|
// Value écrit la map en JSON ; une map nil devient {} (la colonne est NOT NULL).
|
|
func (c Contacts) Value() (driver.Value, error) {
|
|
if c == nil {
|
|
return "{}", nil
|
|
}
|
|
b, err := json.Marshal(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// Scan lit le JSON renvoyé par la base ; NULL donne une map vide.
|
|
func (c *Contacts) Scan(src any) error {
|
|
var raw []byte
|
|
switch v := src.(type) {
|
|
case nil:
|
|
*c = Contacts{}
|
|
return nil
|
|
case []byte:
|
|
raw = v
|
|
case string:
|
|
raw = []byte(v)
|
|
default:
|
|
return fmt.Errorf("orders: cannot scan %T into Contacts", src)
|
|
}
|
|
out := Contacts{}
|
|
if len(raw) > 0 {
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
return fmt.Errorf("orders: invalid contacts JSON: %w", err)
|
|
}
|
|
}
|
|
*c = out
|
|
return nil
|
|
}
|
|
|
|
func (i InfoSup) Value() (driver.Value, error) {
|
|
if i == nil {
|
|
return "{}", nil
|
|
}
|
|
|
|
b, err := json.Marshal(i)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func (i *InfoSup) Scan(src any) error {
|
|
var raw []byte
|
|
switch v := src.(type) {
|
|
case nil:
|
|
*i = InfoSup{}
|
|
return nil
|
|
case []byte:
|
|
raw = v
|
|
case string:
|
|
raw = []byte(v)
|
|
default:
|
|
return fmt.Errorf("orders: cannot scan %T into Contacts", src)
|
|
}
|
|
out := InfoSup{}
|
|
if len(raw) > 0 {
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
return fmt.Errorf("orders: invalid contacts JSON: %w", err)
|
|
}
|
|
}
|
|
*i = out
|
|
return nil
|
|
}
|