Merge pull request #49 from rede5/codex/investigate-shipping-data-save-error
Improve shipping settings validation (backend + frontend)
This commit is contained in:
commit
14eb6c61c8
2 changed files with 30 additions and 3 deletions
|
|
@ -102,15 +102,15 @@ func (h *Handler) UpsertShippingSettings(w http.ResponseWriter, r *http.Request)
|
|||
return
|
||||
}
|
||||
if methodType != domain.ShippingMethodPickup {
|
||||
if method.MaxRadiusKm <= 0 {
|
||||
writeError(w, http.StatusBadRequest, errors.New("max_radius_km must be > 0 for delivery methods"))
|
||||
if method.Active && method.MaxRadiusKm <= 0 {
|
||||
writeError(w, http.StatusBadRequest, errors.New("max_radius_km must be > 0 for active delivery methods"))
|
||||
return
|
||||
}
|
||||
if method.MinFeeCents < 0 || method.PricePerKmCents < 0 {
|
||||
writeError(w, http.StatusBadRequest, errors.New("delivery pricing must be >= 0"))
|
||||
return
|
||||
}
|
||||
if method.FreeShippingThresholdCents != nil && *method.FreeShippingThresholdCents <= 0 {
|
||||
if method.Active && method.FreeShippingThresholdCents != nil && *method.FreeShippingThresholdCents <= 0 {
|
||||
writeError(w, http.StatusBadRequest, errors.New("free_shipping_threshold_cents must be > 0"))
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,6 +269,33 @@ export function CompanyPage() {
|
|||
|
||||
const saveShippingSettings = async () => {
|
||||
if (!company) return
|
||||
const pickupMethod = shippingDraft.find((method) => method.type === 'pickup')
|
||||
if (pickupMethod?.active) {
|
||||
if (!pickupMethod.pickup_address.trim() || !pickupMethod.pickup_hours.trim()) {
|
||||
setShippingError('Informe o endereço e o horário de retirada para ativar a retirada no local.')
|
||||
return
|
||||
}
|
||||
}
|
||||
const invalidDelivery = shippingDraft.find(
|
||||
(method) =>
|
||||
method.active &&
|
||||
method.type !== 'pickup' &&
|
||||
(!method.max_radius_km || method.max_radius_km <= 0)
|
||||
)
|
||||
if (invalidDelivery) {
|
||||
setShippingError('Informe um raio máximo válido para métodos de entrega ativos.')
|
||||
return
|
||||
}
|
||||
const invalidFreeShipping = shippingDraft.find((method) => {
|
||||
if (!method.active || !method.free_shipping_threshold_reais.trim()) {
|
||||
return false
|
||||
}
|
||||
return parseNumber(method.free_shipping_threshold_reais) <= 0
|
||||
})
|
||||
if (invalidFreeShipping) {
|
||||
setShippingError('Informe um valor válido para o frete grátis quando o método estiver ativo.')
|
||||
return
|
||||
}
|
||||
try {
|
||||
setShippingLoading(true)
|
||||
const payload = {
|
||||
|
|
|
|||
Loading…
Reference in a new issue