fixes
This commit is contained in:
@@ -15,15 +15,17 @@ import (
|
||||
//
|
||||
// Behavior:
|
||||
// - Removes the cart line whose local cart line Id == payload.Id
|
||||
// - Cascades: also removes any line whose ParentId points at a removed line
|
||||
// (transitively), so removing a parent removes its child sub-articles
|
||||
// - If no such line exists returns an error
|
||||
// - Recalculates cart totals (WithTotals)
|
||||
// - Releases reservations for every removed line and recalculates totals
|
||||
//
|
||||
// Notes:
|
||||
// - This removes only the line item; any deliveries referencing the removed
|
||||
// - This removes only the line items; any deliveries referencing a removed
|
||||
// item are NOT automatically adjusted (mirrors prior logic). If future
|
||||
// semantics require pruning delivery.item_ids you can extend this handler.
|
||||
// - If multiple lines somehow shared the same Id (should not happen), only
|
||||
// the first match would be removed—data integrity relies on unique line Ids.
|
||||
// - If multiple lines somehow shared the same Id (should not happen), all
|
||||
// matches are removed—data integrity relies on unique line Ids.
|
||||
|
||||
func (c *CartMutationContext) RemoveItem(g *CartGrain, m *messages.RemoveItem) error {
|
||||
if m == nil {
|
||||
@@ -32,26 +34,46 @@ func (c *CartMutationContext) RemoveItem(g *CartGrain, m *messages.RemoveItem) e
|
||||
|
||||
targetID := uint32(m.Id)
|
||||
|
||||
index := -1
|
||||
for i, it := range g.Items {
|
||||
found := false
|
||||
for _, it := range g.Items {
|
||||
if it.Id == targetID {
|
||||
index = i
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if index == -1 {
|
||||
if !found {
|
||||
return fmt.Errorf("RemoveItem: item id %d not found", m.Id)
|
||||
}
|
||||
|
||||
item := g.Items[index]
|
||||
if item.ReservationEndTime != nil && item.ReservationEndTime.After(time.Now()) {
|
||||
err := c.ReleaseItem(context.Background(), g.Id, item.Sku, item.StoreId)
|
||||
if err != nil {
|
||||
log.Printf("unable to release item reservation")
|
||||
// Collect the target and, transitively, any children pointing at a removed
|
||||
// line. Loops until no further descendants are found (handles nesting).
|
||||
remove := map[uint32]bool{targetID: true}
|
||||
for {
|
||||
grew := false
|
||||
for _, it := range g.Items {
|
||||
if it.ParentId != nil && remove[*it.ParentId] && !remove[it.Id] {
|
||||
remove[it.Id] = true
|
||||
grew = true
|
||||
}
|
||||
}
|
||||
if !grew {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
g.Items = append(g.Items[:index], g.Items[index+1:]...)
|
||||
kept := g.Items[:0]
|
||||
for _, it := range g.Items {
|
||||
if remove[it.Id] {
|
||||
if it.ReservationEndTime != nil && it.ReservationEndTime.After(time.Now()) {
|
||||
if err := c.ReleaseItem(context.Background(), g.Id, it.Sku, it.StoreId); err != nil {
|
||||
log.Printf("unable to release reservation for item %d: %v", it.Id, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
kept = append(kept, it)
|
||||
}
|
||||
g.Items = kept
|
||||
g.UpdateTotals()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user