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>
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package checkoutfields
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
func toPayload(f *CheckoutField) settingsPayload {
|
|
return settingsPayload{
|
|
Phone: f.CheckoutPhone,
|
|
Address: f.CheckoutAddress,
|
|
Snap: f.CheckoutSnap,
|
|
Insta: f.CheckoutInsta,
|
|
Telegram: f.CheckoutTelegram,
|
|
SimpleX: f.CheckoutSimpleX,
|
|
Whatsapp: f.CheckoutWhatsapp,
|
|
Signal: f.CheckoutSignal,
|
|
Notes: f.CheckoutNotes,
|
|
Hour: f.CheckoutHour,
|
|
}
|
|
}
|
|
|
|
func (p settingsPayload) toModel() *CheckoutField {
|
|
return &CheckoutField{
|
|
CheckoutPhone: p.Phone,
|
|
CheckoutAddress: p.Address,
|
|
CheckoutSnap: p.Snap,
|
|
CheckoutInsta: p.Insta,
|
|
CheckoutTelegram: p.Telegram,
|
|
CheckoutSimpleX: p.SimpleX,
|
|
CheckoutWhatsapp: p.Whatsapp,
|
|
CheckoutSignal: p.Signal,
|
|
CheckoutNotes: p.Notes,
|
|
CheckoutHour: p.Hour,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) fail(c *gin.Context, err error, msg string) {
|
|
if errors.Is(err, ErrNotFound) {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "checkout fields not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
|
|
}
|
|
|
|
// Get : lecture publique, utilisée par le storefront pour savoir quels champs
|
|
// afficher au checkout.
|
|
func (h *Handler) Get(c *gin.Context) {
|
|
f, err := h.service.Get(c.Request.Context())
|
|
if err != nil {
|
|
h.fail(c, err, "failed to load checkout fields")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toPayload(f))
|
|
}
|
|
|
|
// Update : écriture admin de tous les réglages d'un coup.
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
var req settingsPayload
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
|
return
|
|
}
|
|
f, err := h.service.Update(c.Request.Context(), req.toModel())
|
|
if err != nil {
|
|
h.fail(c, err, "failed to update checkout fields")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toPayload(f))
|
|
}
|