feat(checkout): let the admin choose the details asked at checkout
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>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
178652ef14
commit
f9bfdc9d3c
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"backend/internal/modules/checkoutfields"
|
||||
"backend/internal/modules/orders"
|
||||
"backend/internal/modules/pricing"
|
||||
"backend/internal/modules/products"
|
||||
@@ -101,6 +102,22 @@ func (f *fakePrices) PriceForQuantity(_ context.Context, tierID uuid.UUID, multi
|
||||
return t.PriceCents * multiplier, t, nil
|
||||
}
|
||||
|
||||
// fakeSettings sert des réglages de checkout fixes. La valeur par défaut
|
||||
// (aucune option activée) ne demande rien d'autre que le nom, ce qui laisse
|
||||
// les tests existants inchangés.
|
||||
type fakeSettings struct {
|
||||
fields *checkoutfields.CheckoutField
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *fakeSettings) Get(context.Context) (*checkoutfields.CheckoutField, error) {
|
||||
if s.err != nil {
|
||||
return nil, s.err
|
||||
}
|
||||
cp := *s.fields
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
type fakeNotifier struct {
|
||||
newOrderCalls []string
|
||||
}
|
||||
@@ -117,6 +134,7 @@ type testFixture struct {
|
||||
units *fakeUnits
|
||||
prices *fakePrices
|
||||
notifier *fakeNotifier
|
||||
settings *fakeSettings
|
||||
|
||||
productID uuid.UUID
|
||||
unitID uuid.UUID
|
||||
@@ -140,6 +158,7 @@ func newFixture() *testFixture {
|
||||
tierID: {ID: tierID, ProductID: productID, UnitID: unitID, Quantity: 5, PriceCents: 4500},
|
||||
}},
|
||||
notifier: &fakeNotifier{},
|
||||
settings: &fakeSettings{fields: &checkoutfields.CheckoutField{ID: 1}},
|
||||
productID: productID,
|
||||
unitID: unitID,
|
||||
tierID: tierID,
|
||||
@@ -147,7 +166,7 @@ func newFixture() *testFixture {
|
||||
}
|
||||
|
||||
func (f *testFixture) service() *orders.Service {
|
||||
return orders.NewService(f.repo, f.products, f.units, f.prices, f.notifier)
|
||||
return orders.NewService(f.repo, f.products, f.units, f.prices, f.notifier, f.settings)
|
||||
}
|
||||
|
||||
func TestService_Create_ComputesTotalAndNotifies(t *testing.T) {
|
||||
@@ -155,8 +174,7 @@ func TestService_Create_ComputesTotalAndNotifies(t *testing.T) {
|
||||
svc := f.service()
|
||||
|
||||
order, items, err := svc.Create(context.Background(), orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
CustomerEmail: "alice@example.com",
|
||||
CustomerName: "Alice",
|
||||
Items: []orders.ItemInput{
|
||||
{ProductID: f.productID, PriceTierID: f.tierID, Multiplier: 2},
|
||||
},
|
||||
@@ -179,7 +197,7 @@ func TestService_Create_RejectsEmptyOrder(t *testing.T) {
|
||||
f := newFixture()
|
||||
svc := f.service()
|
||||
|
||||
_, _, err := svc.Create(context.Background(), orders.CreateInput{CustomerName: "Alice", CustomerEmail: "a@example.com"})
|
||||
_, _, err := svc.Create(context.Background(), orders.CreateInput{CustomerName: "Alice"})
|
||||
if !errors.Is(err, orders.ErrEmptyOrder) {
|
||||
t.Fatalf("Create() error = %v, want ErrEmptyOrder", err)
|
||||
}
|
||||
@@ -193,8 +211,7 @@ func TestService_Create_RejectsTierProductMismatch(t *testing.T) {
|
||||
f.products.byID[otherProduct] = &products.Product{ID: otherProduct, Name: "Other", IsActive: true}
|
||||
|
||||
_, _, err := svc.Create(context.Background(), orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
CustomerEmail: "alice@example.com",
|
||||
CustomerName: "Alice",
|
||||
Items: []orders.ItemInput{
|
||||
// tierID actually belongs to f.productID, not otherProduct.
|
||||
{ProductID: otherProduct, PriceTierID: f.tierID, Multiplier: 1},
|
||||
@@ -211,9 +228,8 @@ func TestService_List_ReturnsAllOrders(t *testing.T) {
|
||||
|
||||
for range 2 {
|
||||
if _, _, err := svc.Create(context.Background(), orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
CustomerEmail: "alice@example.com",
|
||||
Items: []orders.ItemInput{{ProductID: f.productID, PriceTierID: f.tierID, Multiplier: 1}},
|
||||
CustomerName: "Alice",
|
||||
Items: []orders.ItemInput{{ProductID: f.productID, PriceTierID: f.tierID, Multiplier: 1}},
|
||||
}); err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user