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
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -46,10 +47,11 @@ type Service struct {
|
||||
units UnitFinder
|
||||
prices PriceResolver
|
||||
notifier OrderNotifier
|
||||
settings CheckoutSettings
|
||||
}
|
||||
|
||||
func NewService(repo Repository, products ProductFinder, units UnitFinder, prices PriceResolver, notifier OrderNotifier) *Service {
|
||||
return &Service{repo: repo, products: products, units: units, prices: prices, notifier: notifier}
|
||||
func NewService(repo Repository, products ProductFinder, units UnitFinder, prices PriceResolver, notifier OrderNotifier, settings CheckoutSettings) *Service {
|
||||
return &Service{repo: repo, products: products, units: units, prices: prices, notifier: notifier, settings: settings}
|
||||
}
|
||||
|
||||
type ItemInput struct {
|
||||
@@ -59,12 +61,12 @@ type ItemInput struct {
|
||||
}
|
||||
|
||||
type CreateInput struct {
|
||||
CustomerID *uuid.UUID
|
||||
CustomerName string
|
||||
CustomerEmail string
|
||||
CustomerPhone string
|
||||
Notes string
|
||||
Items []ItemInput
|
||||
CustomerID *uuid.UUID
|
||||
CustomerName string
|
||||
Contacts Contacts
|
||||
InfoSup InfoSup
|
||||
Notes string
|
||||
Items []ItemInput
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, in CreateInput) (*Order, []*OrderItem, error) {
|
||||
@@ -72,13 +74,23 @@ func (s *Service) Create(ctx context.Context, in CreateInput) (*Order, []*OrderI
|
||||
return nil, nil, ErrEmptyOrder
|
||||
}
|
||||
|
||||
// Les règles du formulaire (champs demandés, obligatoires) sont relues en
|
||||
// base à chaque commande : le client ne les définit jamais.
|
||||
checkout, err := s.settings.Get(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("load checkout settings: %w", err)
|
||||
}
|
||||
if err := applyCheckoutSettings(checkout, &in); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
order := &Order{
|
||||
ID: uuid.New(),
|
||||
CustomerID: in.CustomerID,
|
||||
CustomerName: in.CustomerName,
|
||||
CustomerEmail: in.CustomerEmail,
|
||||
CustomerPhone: in.CustomerPhone,
|
||||
Notes: in.Notes,
|
||||
ID: uuid.New(),
|
||||
CustomerID: in.CustomerID,
|
||||
CustomerName: in.CustomerName,
|
||||
Contacts: in.Contacts,
|
||||
InfoSup: in.InfoSup,
|
||||
Notes: in.Notes,
|
||||
}
|
||||
|
||||
items := make([]*OrderItem, 0, len(in.Items))
|
||||
@@ -146,7 +158,9 @@ func (s *Service) ListByCustomer(ctx context.Context, customerID uuid.UUID) ([]*
|
||||
|
||||
func summarizeNewOrder(order *Order, items []*OrderItem) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "New order from %s (%s)\n", order.CustomerName, order.CustomerEmail)
|
||||
fmt.Fprintf(&b, "New order from %s\n", order.CustomerName)
|
||||
writeSorted(&b, order.Contacts)
|
||||
writeSorted(&b, order.InfoSup)
|
||||
for _, item := range items {
|
||||
fmt.Fprintf(&b, "- %dx %s (%.2f %s) = %.2f\n",
|
||||
item.Multiplier, item.ProductName, item.TierQuantity, item.UnitSymbol, centsToUnits(item.TotalCents))
|
||||
@@ -158,3 +172,15 @@ func summarizeNewOrder(order *Order, items []*OrderItem) string {
|
||||
func centsToUnits(cents int64) float64 {
|
||||
return float64(cents) / 100
|
||||
}
|
||||
|
||||
// writeSorted ajoute "clé: valeur" par ligne, triées pour une notification stable.
|
||||
func writeSorted(b *strings.Builder, m map[string]string) {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
fmt.Fprintf(b, "%s: %s\n", k, m[k])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user