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>
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package orders_test
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
"backend/internal/modules/orders"
|
|
)
|
|
|
|
// Régression : un champ de type map sans Valuer/Scanner faisait échouer
|
|
// l'analyse du modèle par GORM au premier accès à la base, alors que
|
|
// `go build` passait. Chaque colonne JSONB d'Order est vérifiée ici.
|
|
func TestOrderModelHasInfoSupColumn(t *testing.T) {
|
|
s, err := schema.Parse(&orders.Order{}, &sync.Map{}, schema.NamingStrategy{})
|
|
if err != nil {
|
|
t.Fatalf("schema.Parse(Order) error = %v", err)
|
|
}
|
|
f := s.LookUpField("info_sup")
|
|
if f == nil {
|
|
t.Fatalf("colonne info_sup absente (colonnes: %v)", s.DBNames)
|
|
}
|
|
if got := string(f.DataType); got != "jsonb" {
|
|
t.Errorf("info_sup : type %q, attendu jsonb", got)
|
|
}
|
|
}
|
|
|
|
func TestInfoSup_RoundTrip(t *testing.T) {
|
|
in := orders.InfoSup{orders.ContactAddress: "12 rue de Paris", orders.ContactHour: "18:30"}
|
|
v, err := in.Value()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out orders.InfoSup
|
|
if err := out.Scan(v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(out) != 2 || out[orders.ContactAddress] != "12 rue de Paris" || out[orders.ContactHour] != "18:30" {
|
|
t.Errorf("relu = %v, attendu %v", out, in)
|
|
}
|
|
}
|
|
|
|
// La base renvoie du JSONB en []byte (pgx) ou en string selon le driver.
|
|
func TestInfoSup_ScanAcceptsBytesAndString(t *testing.T) {
|
|
for name, src := range map[string]any{
|
|
"bytes": []byte(`{"hour":"09:00"}`),
|
|
"string": `{"hour":"09:00"}`,
|
|
} {
|
|
var out orders.InfoSup
|
|
if err := out.Scan(src); err != nil {
|
|
t.Errorf("%s : %v", name, err)
|
|
continue
|
|
}
|
|
if out[orders.ContactHour] != "09:00" {
|
|
t.Errorf("%s : relu = %v", name, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInfoSup_NilAndNullAreEmpty(t *testing.T) {
|
|
var nilMap orders.InfoSup
|
|
v, err := nilMap.Value()
|
|
if err != nil || v != "{}" {
|
|
t.Errorf("Value(nil) = %v, %v ; attendu \"{}\"", v, err)
|
|
}
|
|
var out orders.InfoSup
|
|
if err := out.Scan(nil); err != nil || out == nil || len(out) != 0 {
|
|
t.Errorf("Scan(nil) = %v, %v ; attendu une map vide non nil", out, err)
|
|
}
|
|
// Une map vide non nil s'écrit aussi {} (colonne NOT NULL).
|
|
if v, err := (orders.InfoSup{}).Value(); err != nil || v != "{}" {
|
|
t.Errorf("Value(vide) = %v, %v ; attendu \"{}\"", v, err)
|
|
}
|
|
}
|
|
|
|
func TestInfoSup_ScanRejectsInvalidInput(t *testing.T) {
|
|
var out orders.InfoSup
|
|
if err := out.Scan([]byte(`{`)); err == nil {
|
|
t.Error("Scan d'un JSON invalide : erreur attendue")
|
|
}
|
|
if err := out.Scan(42); err == nil {
|
|
t.Error("Scan d'un type inattendu : erreur attendue")
|
|
}
|
|
}
|
|
|
|
// Contacts et InfoSup ne doivent pas partager d'état : écrire l'un ne change pas l'autre.
|
|
func TestContactsAndInfoSup_AreIndependent(t *testing.T) {
|
|
o := orders.Order{
|
|
Contacts: orders.Contacts{orders.ContactWhatsapp: "+33612345678"},
|
|
InfoSup: orders.InfoSup{orders.ContactHour: "18:30"},
|
|
}
|
|
if _, ok := o.InfoSup[orders.ContactWhatsapp]; ok {
|
|
t.Error("InfoSup contient une clé de Contacts")
|
|
}
|
|
if _, ok := o.Contacts[orders.ContactHour]; ok {
|
|
t.Error("Contacts contient une clé d'InfoSup")
|
|
}
|
|
}
|