package io.plumery.inventoryitem.api.denormalizer.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import io.plumery.core.ActionHandler;
import io.plumery.inventoryitem.api.core.EventEnvelope;
import io.plumery.inventoryitem.api.core.InventoryItemListItem;
import io.plumery.inventoryitem.api.denormalizer.Constant;
import io.plumery.inventoryitem.api.denormalizer.hazelcast.HazelcastManaged;
import io.plumery.inventoryitem.core.events.InventoryItemDeactivated;
import io.plumery.inventoryitem.core.events.InventoryItemRenamed;
import org.apache.kafka.streams.processor.AbstractProcessor;

public class InventoryItemDeactivatedHandler extends AbstractProcessor<String, EventEnvelope>
        implements ActionHandler<InventoryItemDeactivated> {
    private final ObjectMapper mapper;
    private final HazelcastInstance hazelcastInstance;

    public InventoryItemDeactivatedHandler() {
        this.mapper = new ObjectMapper();
        this.hazelcastInstance = HazelcastManaged.getInstance();
    }

    @Override
    public void process(String key, EventEnvelope value) {
        handle(deserializeEvent(value));

        context().forward(key, value);
        context().commit();
    }

    @Override
    public void handle(InventoryItemDeactivated event) {
        IMap<String, InventoryItemListItem> inventoryItems = getInventoryItemsMap();

        String id = event.id.toString();

        InventoryItemListItem item = inventoryItems.get(id);
        if (item != null) {
            item.version = event.version;
            item.active = false;
        }

        inventoryItems.put(id, item);
    }

    private InventoryItemDeactivated deserializeEvent(EventEnvelope value) {
        InventoryItemDeactivated event = mapper.convertValue(value.eventData, InventoryItemDeactivated.class);
        return event;
    }

    private IMap<String, InventoryItemListItem> getInventoryItemsMap() {
        return hazelcastInstance.getMap(Constant.INVENTORY_ITEMS_MAP);
    }
}

@@ -1472,25 +1472,24 @@
 ring();%0A
-%0A
         
 Inventor
@@ -1484,210 +1484,32 @@
     
-InventoryItemListItem item = inventoryItems.get(id);%0A        if (item != null) %7B%0A            item.version = event.version;%0A            item.active = false;%0A        %7D%0A%0A        inventoryItems.put(id, item
+inventoryItems.delete(id
 );%0A 

Remove the item from the list when the item is deactivated