50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package cart
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
messages "git.tornberg.me/go-cart-actor/pkg/messages"
|
|
)
|
|
|
|
// mutation_remove_delivery.go
|
|
//
|
|
// Registers the RemoveDelivery mutation.
|
|
//
|
|
// Behavior:
|
|
// - Removes the delivery entry whose Id == payload.Id.
|
|
// - If not found, returns an error.
|
|
// - Cart totals are recalculated (WithTotals) after removal.
|
|
// - Items previously associated with that delivery simply become "without delivery";
|
|
// subsequent delivery mutations can reassign them.
|
|
//
|
|
// Differences vs legacy:
|
|
// - Legacy logic decremented TotalPrice explicitly before recalculating.
|
|
// Here we rely solely on UpdateTotals() to recompute from remaining
|
|
// deliveries and items (simpler / single source of truth).
|
|
//
|
|
// Future considerations:
|
|
// - If delivery pricing logic changes (e.g., dynamic taxes per delivery),
|
|
// UpdateTotals() may need enhancement to incorporate delivery tax properly.
|
|
|
|
func RemoveDelivery(g *CartGrain, m *messages.RemoveDelivery) error {
|
|
if m == nil {
|
|
return fmt.Errorf("RemoveDelivery: nil payload")
|
|
}
|
|
targetID := uint32(m.Id)
|
|
index := -1
|
|
for i, d := range g.Deliveries {
|
|
if d.Id == targetID {
|
|
index = i
|
|
break
|
|
}
|
|
}
|
|
if index == -1 {
|
|
return fmt.Errorf("RemoveDelivery: delivery id %d not found", m.Id)
|
|
}
|
|
|
|
// Remove delivery (order not preserved beyond necessity)
|
|
g.Deliveries = append(g.Deliveries[:index], g.Deliveries[index+1:]...)
|
|
g.UpdateTotals()
|
|
return nil
|
|
}
|